2.2.0 • Published 3 years ago
eratum v2.2.0
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. - messageStringHuman readable message explaining error. Generated by EJS template and parameters. Inherited from Error. - causeanyPrevious Error generating this one. (optional, defaultnull) - originStringHuman readable hint about thrower. Should be capitalized snake case (/^A-Z_*$/). (optional, default'') - parametersObjectObject storing extra parameters while producing this Eratum (including required attributes). - Static properties
- origin
StringPrefix for all generated errors. - isStackEnabledbooleanDefine default option for get function. - Functions
- get Serilize error in JSON ready object.
- Parameters
- isStackEnabled
booleanDefine if stack is includes in returns. (optional, defaultEratum.isStackEnabled) - ReturnsIEratum
IEratum
Interface defining properties of Eratum class
- Properties
- tag
StringUnique string identifier for this error type. Must be capitalized snake case (/^A-Z_+$/). - messageStringHuman readable message explaining error. - causeanyPrevious Error generating this one. - originStringHuman readable hint about thrower. Should be capitalized snake case (/^A-Z_*$/).
registerError
Register error by name
- Parameters
- name
StringUnique string name for this error type. Must be camel case (/^a-z*$/). - templateStringEJS 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.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.causeany- parameters.originString** - parameters...requiredAttrs**Stringable - 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'') - 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