简体   繁体   中英

How to throw a custom error from a RESTlet in NetSuite?

I'm trying to throw custom error from my RESTlet and I'm trying to make it similar to NetSuite's error format. I want a client to see HTTP-code 400. When I use scripts v1 I just do this:

throw nlapiCreateError('MY_ERROR_CODE', 'My message');

I get such an answer in this case (and this is my goal):

{
    "error":
    {
        "code": "MY_ERROR_CODE",
        "message": "My message"
    }
}

But when I use scripts v2 I do this:

    throw error.create({
        name: 'MY_ERROR_CODE',
        message: 'My message'});

And I get this:

{
    "error":
    {
        "code": "JS_EXCEPTION",
        "message": "{"type":"error.SuiteScriptError","name":"MY_ERROR_CODE","message":"My message","stack":["createError(N/error)","prevalidations(SuiteScripts/RESTlet.js:152)","doPost(SuiteScripts/RESTlet.js:86)","createError(N/error)"],"cause":{"name":"MY_ERROR_CODE","message":"My message"},"id":""}"
    }
}

Can anyone tell me how to get answers like while using v1 if I use v2?

I don't have enough rep to reply to answers. So as a follow on from Prajakta's solution, instead of creating the error object and then throwing it, referencing the message field. You can create and throw at the same time as per below:

 throw error.create({
   name: 'FAILURE',
   message: 'Error details here',
   notifyOff: false
}).message;

Seems like you're out of luck, since the help documentation doesn't explain beyond what you've already tried.

That said, you could go into the NetSuite debugger (debugger.netsuite.com) and explore the error object before it's thrown. Maybe there are undocumented properties you can set. It's probably worth a try and usually a good learning experience to explore NetSuite internals.

You could also do the same for the v1 thrown error, and compare how they differ. You might even be able to construct your own JS error objects from scratch!

If you inspect the result code after throwing the error you should see it is actually 400. Even though you have a an error Netsuite still returns a response body with the error details. What code are you getting?

What if you try to add the following?

 function createError(eTitle, eMessage) { var errorObj = error.create({ name: eTitle, message: eMessage, notifyOff: true, }) throw errorObj.name + ": " + errorObj.message }

Check with the below code, this will work!

var err = error.create({name: 'MY_ERROR_CODE', message: 'My Message'})

err.toString = function(){return err.message};

throw err;

You can use throw myCustomError.message instead of throw myCustomError

Below is reference code block. Let know if this helps


var myCustomError = error.create({
            name: 'FAILURE',
            message: 'Error details here',
            notifyOff: false
});


throw myCustomError.message;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM