Error handling
Proper error handling is one of the most important aspects of creating successful integrations. For example, the external system that you're trying to integrate with might be down, overloaded, or contain a bug that causes it to suddenly return unexpected error responses. When writing a package, you should decide if and how to handle various types of errors.
As a baseline defense, we recommend that you always set the "Send error emails" checkbox in the package settings, so that an email notification is sent out when a package fails due to an unexpected exception.
It is also possible to deal with errors in the package script itself. This allows you to, for example:
- Recover from certain errors and continue package execution as normal
- Collect errors when processing many records and add a note with an error report to a Xurrent request
- Ignore some errors completely (be careful when doing this)
This can be achieved by using exception handling statements via the JavaScript keywords throw, try, catch and
finally. These concepts are explained in detail in
the MDN Control flow and error handling reference,
so we won't repeat them here. Instead, we will limit ourselves to a quick example that illustrates the keywords.
const list = [
{ id: 1, value: 'First' },
{ id: 2, value: null },
{ id: 3, value: 'Third' }
];
try {
let res = checkValue(list[1]);
log("Result", res);
} catch (e) {
logError("Something went wrong", e);
} finally {
log("Logged at the end");
}
function checkValue(element) {
if (element.id === 2) {
if (element.value) {
return true;
} else {
throw new Error("Wrong value");
}
}
}
When you log an Error object, as in the "Something went wrong" line in the above example, the automator will also include a stacktrace in the package log to help you locate where the error occurred.
If you don't handle an error (that is: when you don't catch it) and let it fail the package, the automator will automatically log the error in the package log.