Skip to main content
Version: 2.7

Handling Errors and Exceptions

All exceptions thrown in your bricks are handled by the Olympe runtime like you do in DRAW. Actually the brick Throw Error does just that!

There are several ways to throw exceptions:

import { ActionBrick, ErrorFlow } from 'olympe';
/* ... */
update($, inputs, [forwardEvent, setErrorFlow]) {
// Option 1: throw JS error (no filtering possible in DRAW as no error code is defined)
throw new Error(message);

// Option 2: throw ErrorFlow (adds a code for filtering in DRAW)
throw ErrorFlow.create(message, code);

// Option 3: use the BrickContext (for Promises and async code)
$.throw(new Error(message)); // no filtering possible!
$.throw(message); // no filtering possible!
$.throw(ErrorFlow.create(message, code));

// Option 4: explicitly setErrorFlow (legacy)
setErrorFlow(ErrorFlow.create(message, code));
return; // We must explicitly break the flow and leave the 'update' function
}

Option 1: throw JavaScript error

JavaScript errors are caught by the Olympe runtime and are available in the brick's output error flow. This typically applies when the JavaScript runtime throws an error, e.g. when calling a method on undefined.

Option 2: throw ErrorFlow

When using synchronous code or async - await syntax that preserves the JavaScript execution stack, the following form will be caught by the runtime with the right context.

throw ErrorFlow.create(msg, code);

This syntax breaks the execution flow, like any exception would do.

Option 3: $.throw(ErrorFlow)

The form $.throw(ErrorFlow.create(message, code)) must be used when using Promises (asynchronous flow) in order to maintain the context stack and allow the exception to be caught in DRAW:

// Example 1: throw a standard JavaScript error
myPromise.catch(e => $.throw(e))

// Example 2: throw an ErrorFlow with an error code
Promise.all([query1, query2]).catch(err => $.throw(ErrorFlow.create(err.message, 15));

So using the Promise.then().catch() syntax, you must always use $.throw() to ensure that the error is properly handled.

Option 4: setErrorFlow

The last form works explicitly sets the error flow using the setErrorFlow callback. When using that form, you must explicitly break the flow and exit the update method.

setErrorFlow(ErrorFlow.create(message, code));
return; // We must explicitly break the flow and leave the 'update' function