How to access the Scripted REST API Error Objects in the Scripted REST Resource?

Bharath24
Tera Expert
var err= new sn_ws_err.NotFoundError('The resource you requested is not part of the API.');
gs.info(JSON.stringify(err))



In the above example, I want to access the "err" object in the api to log the error response. But it is 
empty object 

{} for gs.info(JSON.stringify(err))

[object NotFoundError] for gs.info(err);

But I want the it to be in the below format:

Bharath24_0-1744701371797.png

Is it possible to access it before the response is sent?



2 ACCEPTED SOLUTIONS

Shivalika
Mega Sage

Hello @Bharath24 

 

This cannot be printed before you have run it. Obviously it will give you empty object, because it is just an empty object as it says. The key value pairs are assigned after it is run. Otherwise what will it assign ?

 

You cannot tap the responseBody, you need to set another method to call this method, and then store the response body and then print it. This cannot be tapped during configuration, only after execution. 

 

Kindly mark my answer as helpful and accept solution if it helped you in anyway. This will help me be recognized for the efforts and also move this questions from unsolved to solved bucket. 

 

Regards,

 

Shivalika 

 

My LinkedIn - https://www.linkedin.com/in/shivalika-gupta-540346194

 

My youtube - https://youtube.com/playlist?list=PLsHuNzTdkE5Cn4PyS7HdV0Vg8JsfdgQlA&si=0WynLcOwNeEISQCY

View solution in original post

Ankur Bawiskar
Tera Patron
Tera Patron

@Bharath24 

it looks to me that it comes from platform level and is handled at the API interaction so you won't be able to access that. these are coming from sn_ws_err namespace

You can create your own custom error objects if required, check this link

Scripted REST API Error Objects 

You are already getting the correct JSON in response, this is from the developer site

AnkurBawiskar_0-1744703821716.png

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

2 REPLIES 2

Shivalika
Mega Sage

Hello @Bharath24 

 

This cannot be printed before you have run it. Obviously it will give you empty object, because it is just an empty object as it says. The key value pairs are assigned after it is run. Otherwise what will it assign ?

 

You cannot tap the responseBody, you need to set another method to call this method, and then store the response body and then print it. This cannot be tapped during configuration, only after execution. 

 

Kindly mark my answer as helpful and accept solution if it helped you in anyway. This will help me be recognized for the efforts and also move this questions from unsolved to solved bucket. 

 

Regards,

 

Shivalika 

 

My LinkedIn - https://www.linkedin.com/in/shivalika-gupta-540346194

 

My youtube - https://youtube.com/playlist?list=PLsHuNzTdkE5Cn4PyS7HdV0Vg8JsfdgQlA&si=0WynLcOwNeEISQCY

Ankur Bawiskar
Tera Patron
Tera Patron

@Bharath24 

it looks to me that it comes from platform level and is handled at the API interaction so you won't be able to access that. these are coming from sn_ws_err namespace

You can create your own custom error objects if required, check this link

Scripted REST API Error Objects 

You are already getting the correct JSON in response, this is from the developer site

AnkurBawiskar_0-1744703821716.png

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader