What is sn_ws_err API and how to implement it's methods?

ShouryaD
Tera Contributor

Hello, 

 

Can anyone explain what is sn_ws_err API and how to implement it's methods?

1 REPLY 1

Matthew_13
Mega Sage

Hi Buddy,

sn_ws_err is a ServiceNow server-side utility used in Scripted REST APIs to return proper HTTP errors in a clean, consistent way. It helps you send the correct status code 400, 401, 404, etc.... along with a meaningful error message instead of manually building responses.

You typically use it inside a Scripted REST resource, not client-side.

Common you can use it:

  • Set an error on the response (most common):

response.setError(new sn_ws_err.NotFoundError('Record not found'));
return;
  • Throws an error:

throw new sn_ws_err.BadRequestError('Missing required parameter');

Common error types include:

  • BadRequestError (400)

  • UnauthorizedError (401 )

  • ForbiddenError (403)

  • NotFoundError (404)

If you need a custom status or message, you can use like ServiceError:

var err = new sn_ws_err.ServiceError();
err.setStatus(409);
err.setMessage('Conflict');
err.setDetail('Record already exists');
return err;

Basically, sn_ws_err is the recommended way to handle errors in Scripted REST APIs so consumers get clear, standard HTTP responses instead of generic failures.

 

@ShouryaD  - Please mark Accepted Solution and Thumbs Up if you find Helpful 🙂

MJG