Spanish Total.js
English
Documentation For AI agents
Contents

Errors and logging

Report errors to the designer, send failed data to an error output, and write logs.

.md GitHub
On this page

A component reports a problem in two ways: to the person who watches the designer, and to the next nodes of the flow, so the design can handle it.

This page builds JSON parse, which turns JSON text into an object.

Report an error to the designer

instance.throw(error) adds the error to the errors of the FlowStream. The designer shows the number under Errors in the status bar, and the FlowStream keeps the last 10 errors:

instance.throw('Invalid JSON: ' + e.message);

Inside instance.message, $.throw(error) does the same and also stores the error in $.error.

Send failed data to an error output

An error output lets the user decide what happens with data that failed: log it, retry, or answer the request with an error. Declare it like any output:

exports.outputs = [{ id: 'output', name: 'Object' }, { id: 'error', name: 'Error' }];

JSON parse reports the error and sends the text to the Error output:

instance.message = function($) {
    try {
        $.send('output', JSON.parse($.data));
    } catch (e) {
        instance.throw('Invalid JSON: ' + e.message);
        $.send('error', { error: e.message, text: $.data });
    }
};

For the text not json, the Error output sends this, and the designer counts one error:

{ "error": "Unexpected token 'o', \"not json\" is not valid JSON", "text": "not json" }

Errors that you do not catch

When your instance.message throws an exception, Flow catches it and ends the message. The next nodes receive nothing. Catch errors yourself when the user needs to see them in the designer or handle them in the flow.

Write logs

console.log in the server code writes to the output of the Flow process, for example the terminal where you started Flow.

instance.close = function() {
    console.log('Heartbeat ' + instance.id + ' stopped');
};

This line appears in the output of Flow as Heartbeat hb stopped, where hb is the id of the node.

For logs that the user reads on the canvas, send a status instead.

The whole component

<script total>

    exports.id = 'jsonparse';
    exports.name = 'JSON parse';
    exports.icon = 'ti ti-code';
    exports.author = 'Jane Doe';
    exports.version = '1';
    exports.group = 'Demo';
    exports.config = {};
    exports.inputs = [{ id: 'input', name: 'Text' }];
    exports.outputs = [{ id: 'output', name: 'Object' }, { id: 'error', name: 'Error' }];

    exports.make = function(instance, config) {

        instance.message = function($) {
            try {
                $.send('output', JSON.parse($.data));
            } catch (e) {
                instance.throw('Invalid JSON: ' + e.message);
                $.send('error', { error: e.message, text: $.data });
            }
        };

    };

</script>

<readme>
Parses JSON text. Text that is not valid JSON goes to the **Error** output.
</readme>

<body>
    <header>
        <i class="$ICON"></i>$NAME
    </header>
</body>
Made in Europe