eratum
v2.2.0
Published
Module defining wrapper over Error class.
Maintainers
Readme
Eratum
Installation
npm install --save eratumUsage
Import
// Old way
const { default: Errors, registerError } = require('eratum');// Module way
import Errors, { registerError } from 'eratum';Throw
import Errors from 'eratum';
const error = Errors.notYetImplemented({ name: 'awesomeFeature', reason: 'Planned in v2.3' });
/*
{
tag: 'NOT_YET_IMPLEMENTED',
message: 'NOT_YET_IMPLEMENTED - Feature(awesomeFeature) is not yet implemented.Planned in v2.3'
}
*/
try {
try {
throw Errors.notEqual({ name: 'key.length', actualValue: 16, expectedValue: 32 });
} catch (cause) {
throw Errors.unexpectedError({ reason: 'Cipher fail', origin: 'CRYPTO', cause })
}
} catch (cause) {
throw Errors.internalError({ reason: 'Authentication fail', origin: 'LOGIN', cause })
}
/*
{
tag: 'INTERNAL_ERROR',
message: 'INTERNAL_ERROR - Authentication fail',
origin: 'LOGIN'
cause: {
tag: 'UNEXPECTED_ERROR',
message: 'UNEXPECTED_ERROR - Cipher fail',
origin: 'CRYPTO'
cause: {
tag: 'NOT_EQUAL',
message: 'NOT_EQUAL - key.length(16) is not equal to 32'
},
},
}
*/Extends
import Errors, { registerError } from 'eratum';
registerError('outOfBound', 'Resource(<%= name %>) is out of bound(<%= bound %>)', [ 'name', 'bound' ] );
// Errors.outOfBound.tag === 'OUT_OF_BOUND'
const error = Errors.outOfBound({ name: 'amount', bound: 10 });
// error instanceof Errors.outOfBound.class === true
// error.tag === 'OUT_OF_BOUND'
// Optional parameters
registerError('notYetImplemented', 'Feature(<%- name %>) is not yet implemented.<% if (locals.reason) { %><%- reason %><% } %>', ['name']);Documentation
Eratum
Class extending Error
- Properties
- tag
StringUnique string identifier for this error type. Must be capitalized snake case (/^[A-Z_]+$/). Generated from name when usingregisterError. - message
StringHuman readable message explaining error. Generated by EJS template and parameters. Inherited from Error. - cause
anyPrevious Error generating this one. (optional, defaultnull) - origin
StringHuman readable hint about thrower. Should be capitalized snake case (/^[A-Z_]*$/). (optional, default'') - parameters
ObjectObject storing extra parameters while producing this Eratum (including required attributes).
- tag
- Static properties
- origin
StringPrefix for all generated errors. - isStackEnabled
booleanDefine default option for get function.
- origin
- Functions
- get Serilize error in JSON ready object.
- Parameters
- isStackEnabled
booleanDefine if stack is includes in returns. (optional, defaultEratum.isStackEnabled)
- isStackEnabled
- Returns
IEratum
- Parameters
- get Serilize error in JSON ready object.
IEratum
Interface defining properties of Eratum class
- Properties
- tag
StringUnique string identifier for this error type. Must be capitalized snake case (/^[A-Z_]+$/). - message
StringHuman readable message explaining error. - cause
anyPrevious Error generating this one. - origin
StringHuman readable hint about thrower. Should be capitalized snake case (/^[A-Z_]*$/).
- tag
registerError
Register error by name
- Parameters
- name
StringUnique string name for this error type. Must be camel case (/^[a-z][a-zA-Z]*$/). - template
StringEJS template to build error message. (optional, default'') - requiredAttrs
String[]Required attributes for previous EJS template. Rendering fail if those attributes are undefined. (optional, default[])
- name
- Return
void - Throw
Eratum.doesntExistif missing parameter - Throw
Eratum.invalidTypeif type parameter missmatch - Throw
Eratum.invalidFormatif format parameter missmatch - Throw
Eratum.existif error name is already registered
Error producer
All errors have the same producer signature.
- Parameters
- parameters
EratumOptions - parameters.cause
any - parameters.origin
String**** - parameters[...requiredAttrs]
Stringable
- parameters
- Return
class extending Eratum - Throw
Eratum.doesntExistif missing parameters or required attributes. - Throw
Eratum.invalidTypeif type parameter missmatch
Registered errors
- internalError
- Tag
INTERNAL_ERROR - Parameters
- reason (optional, default
'')
- reason (optional, default
- Tag
- unexpectedError
- Tag
UNEXPECTED_ERROR - Parameters
- reason (optional, default
'')
- reason (optional, default
- Tag
- programingFault
- Tag
PROGRAMING_FAULT - Parameters
- reason (optional, default
'')
- reason (optional, default
- Tag
- notYetImplemented
- Tag
NOT_YET_IMPLEMENTED - Parameters
- name
- reason (optional, default
'')
- Tag
- initialized
- Tag
INITIALIZED - Parameters
- name
- Tag
- notInitialized
- Tag
NOT_INITIALIZED - Parameters
- name
- Tag
- invalid
- Tag
INVALID - Parameters
- name
- reason (optional, default
'')
- Tag
- invalidType
- Tag
INVALID_TYPE - Parameters
- name
- actualType
- expectedType
- Tag
- invalidFormat
- Tag
INVALID_FORMAT - Parameters
- name
- value
- format
- Tag
- exist
- Tag
EXIST - Parameters
- name
- Tag
- doesntExist
- Tag
DOESNT_EXIST - Parameters
- name
- Tag
- equal
- Tag
EQUAL - Parameters
- name
- value
- Tag
- notEqual
- Tag
NOT_EQUAL - Parameters
- name
- actualValue
- expectedValue
- Tag
- included
- Tag
INCLUDED - Parameters
- name
- value
- forbiddenValues
- Tag
- notIncluded
- Tag
NOT_INCLUDED - Parameters
- name
- value
- possibleValues
- Tag
