Skip to main content

core/Error

Error values and inspection.

The Error type is the argument to throw, parameter of catch. The Error type is opaque.

Type Error

type Error = Prim.Types.Error

Error value resulting from async computations

Type ErrorCode

type ErrorCode = Prim.ErrorCode

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 }
};

Function reject

func reject(message : Text) : Error

Create 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 argument

Function code

func code(error : Error) : ErrorCode

Returns the code of an error.

Example:

import Error "mo:core/Error";

let error = Error.reject("Example error");
Error.code(error) // #canister_reject

Function message

func message(error : Error) : Text

Returns the message of an error.

Example:

import Error "mo:core/Error";

let error = Error.reject("Example error");
Error.message(error) // "Example error"

Function isRetryPossible

func isRetryPossible(error : Error) : Bool

Returns 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);
}
}
}
}