What’s the equivalent in Rust of a throw Error.reject in Motoko? panic!?
e.g. a Motoko actor
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) {};
};
};
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.
#[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)
}
}
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.