Skip to content

Convert a SuperError instance into JSON notation safe for clients.

Notifications You must be signed in to change notification settings

TyphosLabs/super-errors-json

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

super-errors-json

Convert a SuperError instance into JSON that is safe for clients.

Example:

var Errors = require('super-errors')();
require('super-errors-json')(Errors);

var json = Errors.json(new Errors.NotifyUser('Test...'));

The json variable should now be a string that looks like:

{"name":"NotifyUser","message":"Test...","status_code":500}

SuperErrors.json(error, [json_obj], map, exclude)

  • error (Error): The error to serialize.
  • json_obj (boolean): Whether to return a JSON string (default) or return a JSON-serializable object (true)
  • map (Object): Map of fields to be included and how they are to be renamed
  • exclude (Object): Fields that should be excluded from the object

The map argument can be used to map where fields go and defaults to:

{
    "client_safe_message": 'message',
    "errors.client_safe_message": 'errors',
    "field": 'field',
    "fields.client_safe_message": 'fields',
    "name": 'name',
    "status_code": 'status_code'
}

This map will give you the name, field, status_code and client_safe_message of the error. All additional errors get converted to strings and placed in an array and fields become an object of fields.

The exclude argument can be used to mark fields you don't want included in resulting object.

About

Convert a SuperError instance into JSON notation safe for clients.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published