Solo me preguntaba si podemos volver a escribir los datos que configuramos a través del método setWriteString() al responder a una llamada API entrante. Por ejemplo, supongamos que el código de recurso de descanso con guión es el siguiente:
(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);Para los errores, puede usar los objetos de error de API REST con secuencias de comandos. Lo que debería restablecer el flujo de salida.
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);También podría ser útil obtener el mensaje de error de GlideRecord
gr.getLastErrorMessage(); // something like aborted by businessrule or acl, etc...Para los detalles de su mensaje de error.