How to show the custom error message without stack trace using suitescript 2.0 in netsuite

橙三吉。 提交于 2019-12-22 07:57:16

问题


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.


回答1:


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:


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.



来源:https://stackoverflow.com/questions/38632350/how-to-show-the-custom-error-message-without-stack-trace-using-suitescript-2-0-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!