Create an Error Based on a Condition

The following sample shows how to conditionally create and throw a custom error.

Note:

This sample script uses the require function so that you can copy it into the SuiteScript Debugger and test it. You must use the define function in an entry point script (the script you attach to a script record and deploy). For more information, see SuiteScript 2.x Script Basics and SuiteScript 2.x Script Types.

          /**
 * @NApiVersion 2.x
 */

// This script conditionally creates and throws an error.
require(['N/error'], function(error) {
    function showError() {
        var someVariable = false;

        if (!someVariable) {
            var myCustomError = error.create({
                name: 'WRONG_PARAMETER_TYPE',
                message: 'Wrong parameter type selected.',
                notifyOff: false
            });

            // This will write 'Error: WRONG_PARAMETER_TYPE Wrong parameter type selected' to the log
            log.error('Error: ' + myCustomError.name , myCustomError.message);
            throw myCustomError;
        }
    }
    showError();
}); 

        

General Notices