Skip to content
Logo

Error Handling

Local and Remote Machines

Many of the API methods can raise a MachineError exception that provides visibility to the error code and a description.

import { , ,  } from "@cartesi/machine";
 
try {
    const  = ("./snapshot");
    .();
} catch (: unknown) {
    if ( instanceof ) {
        switch (.) {
            case .:
                .("Invalid argument", .);
                break;
            default:
                .(., .);
                break;
        }
    }
}

Executing the code above without a Cartesi machine stored at ./snapshot produces the following output:

-11 unable to open './snapshot/config.json' for reading: No such file or directory

Rollups Machine

The advance and inspect methods throw RollupsFatalError if the machine halts with a fatal unrecoverable error, or throw a RollupsInputRejectedError if the machine halts with a manual input rejection.

Users should always wrap the advance and inspect calls in a try/catch block, and handle the errors accordingly by checking the error type like in the example below.

import {
    ,
    ,
    ,
} from "@cartesi/machine";
 
const  = ("snapshot");
 
const  = new ([0x00]); // !not a valid advance input
try {
    for (const  of .()) {
        switch (.) {
            case "output":
                .();
                break;
            case "report":
                .();
                break;
        }
    }
} catch (: unknown) {
    if ( instanceof ) {
        .("input rejected");
    } else if ( instanceof ) {
        .("input raised exception", .);
    }
}
 
.();