rest-api-error-factory
A factory function to create REST API error classes.
Usage
const Koa = const Router = const restAPIErrorFactory = const errors = CLIENT_INVALID_PARAM: message: 'Invalid parameter (%s).' httpStatus: 400 CLIENT_RESOURCE_NOT_FOUND: message: 'Resouce not found.' httpStatus: 404 SERVER_INTERNAL_ERROR: message: 'Server Internal Error (EVENT_ID: %s-%s).' httpStatus: 500 // create a REST API error classconst RESTError = const app = const router = app router app app app
APIs
restAPIErrorFactory(errorDefs)
Creates an error class that contains error definitions you passed.
errorDefs
format:
ERROR_CODE: message: 'MESSAGE' httpStatus: STATUS_CODE ...
Example:
const RESTError =
new RESTError(errorCode, [messageParam1, messageParam2, ...], [extraProperties])
Creates an error instance.
Params:
errorCode
: String. The error code you defined in error definition object.
messageParams1
, messageParam2
, ...: String | Number. The params to format the error message. The message will be formatted by util.format(message, messageParam1, messageParam2, ...)
. See util.format for details.
extraProperties
: Object. Extra properties to set.
Example:
const e = 'CLIENT_INVALID_PARAM' 'id' id: 'foo' console// -> 400 console/*->{ code: 'CLIENT_INVALID_PARAM', message: 'Invalid parameter (id).' id: 'foo'} httpStatus will be excluded*/
new RESTError(json)
Creates an error instance from JSON object.
Params:
json
: JSON object. You can get it from restError.toJSON()
.
Example:
const e = code: 'CLIENT_INVALID_PARAM' message: 'Invalid parameter (id).' id: 'foo'