Skip to content

Latest commit

 

History

History
58 lines (46 loc) · 1.44 KB

Debug.md

File metadata and controls

58 lines (46 loc) · 1.44 KB

Debug

Utility functions for debugging.

Import from the base library to use this module.

import Debug "mo:base/Debug";

Function print

func print(text : Text)

Prints text to output stream.

NOTE: The output is placed in the replica log. When running on mainnet, this function has no effect.

Debug.print "Hello New World!";
Debug.print(debug_show(4)) // Often used with `debug_show` to convert values to Text

Function trap

func trap(errorMessage : Text) : None

trap(t) traps execution with a user-provided diagnostic message.

The caller of a future whose execution called trap(t) will observe the trap as an Error value, thrown at await, with code #canister_error and message m. Here m is a more descriptive Text message derived from the provided t. See example for more details.

NOTE: Other execution environments that cannot handle traps may only propagate the trap and terminate execution, with or without some descriptive message.

import Debug "mo:base/Debug";
import Error "mo:base/Error";

actor {
  func fail() : async () {
    Debug.trap("user provided error message");
  };

  public func foo() : async () {
    try {
      await fail();
    } catch e {
      let code = Error.code(e); // evaluates to #canister_error
      let message = Error.message(e); // contains user provided error message
    }
  };
}