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;
// Response unknown due to missed deadline.
#system_unknown;
// Destination invalid.
#destination_invalid;
// Explicit reject by canister code.
#canister_reject;
// Canister trapped.
#canister_error;
// 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:base/Error";
Error.reject("Example error") // can be used as throw argumentpublic func code(error : Error) : ErrorCodeReturns the code of an error.
Example:
import Error "mo:base/Error";
let error = Error.reject("Example error");
Error.code(error) // #canister_rejectpublic func message(error : Error) : TextReturns the message of an error.
Example:
import Error "mo:base/Error";
let error = Error.reject("Example error");
Error.message(error) // "Example error"public func isRetryPossible(error : Error) : BoolReturns whether retrying to send a message may result in success.
Example:
import { message; isRetryPossible } "mo:base/Error";
import { print } "mo:base/Debug";
try await (with timeout = 3) Actor.call(arg)
catch e { if (isRetryPossible e) print(message e) }