super-errors-json
v1.0.0
Published
Convert a SuperError instance into JSON notation safe for clients.
Downloads
2
Maintainers
Readme
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.