Error values and inspection.
The Error type is the argument to throw, parameter of catch.
The Error type is opaque.
Error value resulting from async computations
Error code to classify different kinds of user and system errors:
type ErrorCode = {
// Fatal error.
#system_fatal;
// Transient error.
#system_transient;
// Destination invalid.
#destination_invalid;
// Canister error (e.g., trap, no response).
#canister_error;
// Explicit reject by canister code.
#canister_reject;
// Response unknown; system stopped waiting for it (e.g., timed out, or system under high load).
#system_unknown;
// Future error code (with unrecognized numeric code).
#future : Nat32;
// Error issuing inter-canister call
// (indicating destination queue full or freezing threshold crossed).
#call_error : { err_code : Nat32 }
};public func reject(message : Text) : ErrorCreate an error from the message with the code #canister_reject.
Example:
import Error "mo:core/Error";
Error.reject("Example error") // can be used as throw argumentpublic func code(self : Error) : ErrorCodeReturns the code of an error.
Example:
import Error "mo:core/Error";
let error = Error.reject("Example error");
Error.code(error) // #canister_rejectpublic func message(self : Error) : TextReturns the message of an error.
Example:
import Error "mo:core/Error";
let error = Error.reject("Example error");
Error.message(error) // "Example error"public func isCleanReject(self : Error) : BoolChecks if the error is a clean reject. A clean reject means that there must be no state changes on the callee side.
public func isRetryPossible(self : Error) : BoolReturns whether retrying to send a message may result in success.
Example:
import Error "mo:core/Error";
import Debug "mo:core/Debug";
persistent actor {
type CallableActor = actor {
call : () -> async ()
};
public func example(callableActor : CallableActor) {
try {
await (with timeout = 3) callableActor.call();
}
catch e {
if (Error.isRetryPossible e) {
Debug.print(Error.message e);
}
}
}
}