0

Just wondering if we are able to re-write data that we was set via the setWriteString() method while responding to an inbound api call. For example, let's say the scripted rest resource code is as follows:

(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {

    var body = request.body.data;
    /* do something with request data
    ..
    ..
    ..
    
    then start preparing the response
    */
    var writer = response.getStreamWriter();
    try
    {
        response.setContentType('application/json');
        response.setStatus(200);        
        writer.writeString("{\"results\":[");
        var inc = new GlideRecord('incident');
        inc.query();
        while(inc.next()){
            var obj = {};
            obj.id = inc.getValue('number');
            obj.sys_id = inc.getUniqueValue();
            writer.writeString(global.JSON.stringify(obj));
            
            if (inc.hasNext()) {
                writer.writeString(",");
            }
        }
        writer.writeString("]}");
    }
    catch (ex)
    {
        // let's say exception was thrown on the 3rd iteration while gliding the incident table
        // oh no...an exception..so we need to write something else to the stream
        // is it possible to erase/remove everything that was added to the stream up until the exception occured?
        // so that the response will contain details only about the error?
        
        // something like below:
        response.setContentType('application/json');
        response.setStatus(500);
        writer.writeString("{\"error\":\"Something went wrong\"}"); // this isn't working btw...the stream contained both the content generated in "try" block as well as the "catch" block
        
        // this should not contain anything related to whatever was written from the earlier iterations....
    }

})(request, response);
asprin
  • 9,579
  • 12
  • 66
  • 119
  • Is there a reason why you save the response in a string before sending it, rather than building it in the stream writer? – Dries Meerman Oct 15 '21 at 20:21
  • The docs say to use streamwriter if the response is not fixed and is also stated to be much better in performance as compared to object serialization. – asprin Oct 16 '21 at 15:27

1 Answers1

0

For the errors you can use the Scripted REST API Error objects. Which should reset the output stream.
https://developer.servicenow.com/dev.do#!/learn/courses/paris/app_store_learnv2_rest_paris_rest_integrations/app_store_learnv2_rest_paris_scripted_rest_apis/app_store_learnv2_rest_paris_scripted_rest_api_error_objects

(function run(request, response) {      
  try {
   ...
  } catch (e) {
      var myError = new sn_ws_err.ServiceError();
      myError.setStatus(500);
      myError.setMessage('Something went wrong');
      myError.setDetail('Error while retrieving your data: ' + e);
      return myError;
  }
})(request,response);

It might also be useful to get the error message from the GlideRecord

gr.getLastErrorMessage(); 
// something like aborted by businessrule or acl, etc...

For the details of your error message.

Dries Meerman
  • 96
  • 1
  • 6