# Throw Error.reject equivalent

**URL:** <https://forum.dfinity.org/t/throw-error-reject-equivalent/16076>\
**Category:** Rust\
**Created:** [October 22, 2022, 1:10pm UTC](https://forum.dfinity.org/t/throw-error-reject-equivalent/16076 "2022-10-22T13:10:55Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![peterparker](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@peterparker](https://forum.dfinity.org/u/peterparker)\
**Post date:** [October 22, 2022, 1:10pm UTC](https://forum.dfinity.org/t/throw-error-reject-equivalent/16076/1 "2022-10-22T13:10:56Z")

</div>

What’s the equivalent in Rust of a `throw Error.reject` in Motoko? `panic!`?

e.g. a Motoko actor

```auto
public shared ({caller}) func delete(key : Text, data : DelData) : async () {
    if (Utils.isPrincipalNotEqual(caller, user)) {
      throw Error.reject("User does not have the permission to delete the data.");
    };

    let result : Result.Result<?Data, Text> = store.del(key, data);

    switch (result) {
      case (#err error) {
        throw Error.reject(error);
      };
      case (#ok resultData) {};
    };
  };

```

---

<div class="post-metadata">

**Author:** ![paulyoung](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/paulyoung/32/5251_2.png) [@paulyoung](https://forum.dfinity.org/u/paulyoung)\
**Post date:** [October 22, 2022, 2:03pm UTC](https://forum.dfinity.org/t/throw-error-reject-equivalent/16076/2 "2022-10-22T14:03:26Z")

</div>

I prefer to have my functions return a `Result` and then use `?`, `match`, or things like `.and_then` in the function body.

Using this approach makes testing easier.

When eventually needing to create an unrecoverable error from a `Result` you can do:

```auto
value.expect("User does not have the permission to delete the data.".to_string());

```

or

```auto
value.expect(format!("User {:#?} does not have the permission to delete the data.", user));

```

which is similar to `value.unwrap()` but without the error message, or using `match` and then `panic!` for the `Err` case.

---

<div class="post-metadata">

**Author:** ![paulyoung](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/paulyoung/32/5251_2.png) [@paulyoung](https://forum.dfinity.org/u/paulyoung)\
**Post date:** [October 22, 2022, 2:08pm UTC](https://forum.dfinity.org/t/throw-error-reject-equivalent/16076/3 "2022-10-22T14:08:49Z")

</div>

> [@paulyoung](#):
>
> `value.expect(format!("User {:#?} does not have the permission to delete the data.", user));`

Worth mentioning that this is probably eagerly evaluated and a way around that is to do something like:

```auto
value.ok_or_else(|| format!("User {:#?} does not have the permission to delete the data.", user)).unwrap();

```

---

<div class="post-metadata">

**Author:** ![peterparker](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@peterparker](https://forum.dfinity.org/u/peterparker)\
**Post date:** [October 22, 2022, 2:12pm UTC](https://forum.dfinity.org/t/throw-error-reject-equivalent/16076/4 "2022-10-22T14:12:14Z")

</div>

I for sure like to use `Result` internally within the canister too but for exposed functions I rather like to throw because the frontend can just wrap the call with a try / catch and does not have to process the result. Less verbose on the frontend side in my opinion but of course, mater of taste.

Maybe I should have added this too, I am migrating an existing canister. Therefore I would not like to change the existing declaration - which uses throw - because otherwise I’ll have to update the frontend too.

I used `panic!` so far but not sure it’s the exact conversion.

```auto
#[update]
async fn upload_chunk(chunk: Chunk) -> UploadChunk {
    // TODO: is caller === user

    let result = create_chunk(chunk);

    match result {
        Ok(chunk_id) => { UploadChunk { chunk_id } }
        Err(error) => panic!("{}", error)
    }
}

```

---

<div class="post-metadata">

**Author:** ![paulyoung](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/paulyoung/32/5251_2.png) [@paulyoung](https://forum.dfinity.org/u/paulyoung)\
**Post date:** [October 22, 2022, 2:14pm UTC](https://forum.dfinity.org/t/throw-error-reject-equivalent/16076/5 "2022-10-22T14:14:00Z")

</div>

In that case perhaps you want `ic_cdk::api::trap`

---

<div class="post-metadata">

**Author:** ![peterparker](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@peterparker](https://forum.dfinity.org/u/peterparker)\
**Post date:** [October 22, 2022, 2:20pm UTC](https://forum.dfinity.org/t/throw-error-reject-equivalent/16076/6 "2022-10-22T14:20:43Z")

</div>

> [@paulyoung](#):
>
> ic\_cdk::api::trap

Ah nice, will do that!

* * *

Notes:

- I found this [thread](https://forum.dfinity.org/t/ic-cdk-trap-equivalent-in-motoko/7101) and this [thread](https://forum.dfinity.org/t/can-we-return-error-and-trap/13527) about trap

- there is also a `call::error` according this [thread](https://forum.dfinity.org/t/how-to-trigger-a-canister-reject-code-using-ic-cdk-reject/14060)

In my case, the way I coded the functions - for good and bad - both probably do the job.

---

<div class="post-metadata">

**Author:** ![paulyoung](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/paulyoung/32/5251_2.png) [@paulyoung](https://forum.dfinity.org/u/paulyoung)\
**Post date:** [October 22, 2022, 2:22pm UTC](https://forum.dfinity.org/t/throw-error-reject-equivalent/16076/7 "2022-10-22T14:22:02Z")

</div>

Someone from the Motoko team can probably answer what happens to unhandled exceptions that are thrown if you wanted to try and do the same thing exactly in Rust.

---

<div class="post-metadata">

**Author:** ![peterparker](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@peterparker](https://forum.dfinity.org/u/peterparker)\
**Post date:** [October 22, 2022, 2:23pm UTC](https://forum.dfinity.org/t/throw-error-reject-equivalent/16076/8 "2022-10-22T14:23:34Z")

</div>

I most probably gonna discover that quickly when I’ll run the first tests 😉.

Thanks for the quick answer.
