How to send exact errors in REST API inbound without passing the hard coded string value

Sharique Azim
Mega Sage

Can some one kindly instruct me ,how to send error in responses,whenever you get any bad request WITHOUT giving the same string message?

For example:

{
  "error": {
    "message": "The undefined value has no properties.",
    "detail": "ConversionError: The undefined value has no properties. (sys_ws_operation.e76abaa11b0a6300de0efcccdd4bcb4d.operation_script; line 23)"
  },
  "status": "failure"
}


 Now,incase this has some other error reasons, i would like to send in the exact message and details. and not some generic message.

Please note: i  have gone through Scripted REST API error objects and related docs before raising this post.

 

1 ACCEPTED SOLUTION

Sharique Azim
Mega Sage

I found out a way to send out the error message.  Doesnt works directy like in Docs(most probably due to scope issues)

 

For those intrested:

var bdy=request.body.data;

try{

//some logic}

catch(e){

var err=JSON.stringify(e);
var req_bdy =JSON.stringify(bdy);

response.setContentType('application/json');
var resp=response.getStreamWriter(),
hdrs = {};

hdrs['Content-Type'] = 'application/json';
response.setStatus(400);
response.setHeaders(hdrs);
resp.writeString("{\"results\":[");
resp.writeString(err);
resp.writeString("]}");
gs.info('POST BAD request:'+req_bdy.toString()+'\nError:'+err);

}

 

 

For some reasons, i was not able to use object serializing method here.

View solution in original post

6 REPLIES 6

Alikutty A
Tera Sage

Hello,

You could add your script in a try catch block and fetch errors and send it back in your response. This will give you the exact error message associated with the script execution.

try{

//Your script here

}catch(error e){

//Setup required error message or format

response.error = "Caught error: "+e; 

}

 

Doesnt works.

 

most probably response.error is not a function but response.setError is.

siva_
Giga Guru

Hello Shariq,

Just out of curiosity what i wanted to know is the reason you are looking for exact errors instead of customized errors which will be user friendly for the end users with the oob Service Error object 

Thanks,

Siva 

Customized also means knowing what was the  issue/error sending response accordingly.