eratum
v2.2.0
Published
Module defining wrapper over Error class.
Downloads
7
Maintainers
Readme
Eratum
Installation
npm install --save eratum
Usage
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
String
Unique string identifier for this error type. Must be capitalized snake case (/^[A-Z_]+$/). Generated from name when usingregisterError
. - message
String
Human readable message explaining error. Generated by EJS template and parameters. Inherited from Error. - cause
any
Previous Error generating this one. (optional, defaultnull
) - origin
String
Human readable hint about thrower. Should be capitalized snake case (/^[A-Z_]*$/). (optional, default''
) - parameters
Object
Object storing extra parameters while producing this Eratum (including required attributes).
- tag
- Static properties
- origin
String
Prefix for all generated errors. - isStackEnabled
boolean
Define default option for get function.
- origin
- Functions
- get Serilize error in JSON ready object.
- Parameters
- isStackEnabled
boolean
Define 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
String
Unique string identifier for this error type. Must be capitalized snake case (/^[A-Z_]+$/). - message
String
Human readable message explaining error. - cause
any
Previous Error generating this one. - origin
String
Human readable hint about thrower. Should be capitalized snake case (/^[A-Z_]*$/).
- tag
registerError
Register error by name
- Parameters
- name
String
Unique string name for this error type. Must be camel case (/^[a-z][a-zA-Z]*$/). - template
String
EJS 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.doesntExist
if missing parameter - Throw
Eratum.invalidType
if type parameter missmatch - Throw
Eratum.invalidFormat
if format parameter missmatch - Throw
Eratum.exist
if 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.doesntExist
if missing parameters or required attributes. - Throw
Eratum.invalidType
if 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