2.2.0 • Published 1 year ago
eratum v2.2.0
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
. - messageString
Human readable message explaining error. Generated by EJS template and parameters. Inherited from Error. - causeany
Previous Error generating this one. (optional, defaultnull
) - originString
Human readable hint about thrower. Should be capitalized snake case (/^A-Z_*$/). (optional, default''
) - parametersObject
Object storing extra parameters while producing this Eratum (including required attributes). - Static properties
- origin
String
Prefix for all generated errors. - isStackEnabledboolean
Define default option for get function. - Functions
- get Serilize error in JSON ready object.
- Parameters
- isStackEnabled
boolean
Define if stack is includes in returns. (optional, defaultEratum.isStackEnabled
) - ReturnsIEratum
IEratum
Interface defining properties of Eratum class
- Properties
- tag
String
Unique string identifier for this error type. Must be capitalized snake case (/^A-Z_+$/). - messageString
Human readable message explaining error. - causeany
Previous Error generating this one. - originString
Human readable hint about thrower. Should be capitalized snake case (/^A-Z_*$/).
registerError
Register error by name
- Parameters
- name
String
Unique string name for this error type. Must be camel case (/^a-z*$/). - templateString
EJS template to build error message. (optional, default''
) - requiredAttrsString[]
Required attributes for previous EJS template. Rendering fail if those attributes are undefined. (optional, default[]
) - 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.causeany
- parameters.originString
** - parameters...requiredAttrs**Stringable
- 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''
) - unexpectedError
- Tag
UNEXPECTED_ERROR
- Parameters - reason (optional, default''
) - programingFault
- Tag
PROGRAMING_FAULT
- Parameters - reason (optional, default''
) - notYetImplemented
- Tag
NOT_YET_IMPLEMENTED
- Parameters - name - reason (optional, default''
) - initialized
- Tag
INITIALIZED
- Parameters - name - notInitialized
- Tag
NOT_INITIALIZED
- Parameters - name - invalid
- Tag
INVALID
- Parameters - name - reason (optional, default''
) - invalidType
- Tag
INVALID_TYPE
- Parameters - name - actualType - expectedType - invalidFormat
- Tag
INVALID_FORMAT
- Parameters - name - value - format - exist
- Tag
EXIST
- Parameters - name - doesntExist
- Tag
DOESNT_EXIST
- Parameters - name - equal
- Tag
EQUAL
- Parameters - name - value - notEqual
- Tag
NOT_EQUAL
- Parameters - name - actualValue - expectedValue - included
- Tag
INCLUDED
- Parameters - name - value - forbiddenValues - notIncluded
- Tag
NOT_INCLUDED
- Parameters - name - value - possibleValues