4
votes

I want to show the custom error message with out stack trace to user using "suitescript 2.0"version. In workflow the custom error message is showing without stack trace but in Suite Script the "ERROR MESSAGE " is showing with the stack trace.

ERROR WITH STACK TRACE: {"type":"error.SuiteScriptError","name":"MISSING_CONTRACT_LINE","message":"Please enter atleast one Contract Line item to save a contract.","stack":["createError(N/error)","beforeSubmit(SuiteScripts/Ex_UE_Contract_2.0.js:117)","createError(N/error)"],"cause":{"name":"MISSING_CONTRACT_LINE","message":"Please enter atleast one Contract Line item to save a contract."},"id":""}

I want to show the custom error message without stack trace like this: "name":"MISSING_CONTRACT_LINE","message":"Please enter atleast one Contract Line item to save a contract."

my Code:

     throw error.create({
         name: 'MISSING_CONTRACT_LINE',
         message: 'Please enter atleast one Contract Line item to save a contract.'
     });

is there any possible way to achieve this?

thanks in advance.

2
There's currently a lengthy discussion about this going on in the NetSuite User Group forum as well; I haven't seen any other recommended solution there either. usergroup.netsuite.com/users/forum/platform-areas/customization/… - erictgrubaugh
Where you able to find any other solutions to this problem? The selected answer below doesn't appear to work. - angrycrab
See my solution here answered on another thread. - Koby Pichkhadze

2 Answers

5
votes

The default implementation of N/error's SuiteScriptError#toString() method is to call JSON.stringify(this), however, the method could overridden per instance to handle cases where the raw error message is intended to be displayed to users via throwing the error out of the script. For example:

var err = error.create({name: 'NO_JSON', message: 'This should not be displayed as JSON!'})
err.toString = function(){return err.message};
throw err;

Alternatively, it is possible just to throw a String, however, intervening catch blocks would lose the benefit of accessing other properties of the Error, for example, Error#stack or Error#name.

2
votes

Suite Script Version 2.0:

First define the 'N/error' Module at the top of your function.

var errorObj = error.create({
                code: 'Custom Error Message Without JSON',
                message: 'Custom Error Message Without JSON'
            });

            throw errorObj.code + '\n\n' + errorObj.message;
            return false;

If you doesn't want the Error CODE to be displayed just use the errorObj.message it will display only the Message.