Available error objects

Multiple error objects are available in scripted REST API scripts to report error information to requesting clients.

Error namespace

All scripted REST API error objects use the sn_ws_err namespace. When instantiating a new error object, use this namespace, such as in var error = new sn_ws_err.BadRequestError().

Predefined errors

Predefined error objects allow you to easily communicate an error based on standard HTTP error codes. You can set an optional error message in the object constructor.
Table 1. Predefined errors
Error Object Description Status Code
BadRequestError Indicates an error in the request, such as incorrect syntax. 400
NotFoundError Indicates a requested resource is not available. 404
NotAcceptableError Indicates the Accept header value passed in the request is incompatible with the web service. 406
ConflictError Indicates that there is a conflict in the request, such as multiple conflicting updates. 409
UnsuppotedMediaTypeError Indicates the request media type is not supported by the web service. 415
(function run(request, response) { 
  // request is bad
  return new sn_ws_err.BadRequestError(‘My error message’);
})(request,response);

Customizable ServiceError

The ServiceError object allows you to define your own error response. You can set the status code, error message, and error details using a ServiceError object. Use the ServiceError object when none of the predefined errors meet your needs.

The ServiceError object exposes these functions for setting values:
Table 2. Functions
Function Description Default value
setStatus(Number status) Use this function to set the numeric status code for the error. 500
setMessage(String message) Use this function to set a short error message. An empty string
setDetail(String detail) Use this function to set the detailed error description. An empty string
(function run(request, response) {      
  var myError = new sn_ws_err.ServiceError();
  myError.setStatus(410);
  myError.setMessage(‘My error Message’);
  myError.setDetail(‘Here are the details about this error’);
  return myError;
})(request,response);