@teakit/errors v0.1.2
@teakit/errors
Errors for Teakit
@teakit/errors provide two kinds of errors that is Error and Exception.
- Exception is system error that will log an error and throw exception, but it will be catched by onerror plugin.
- Error is business error that will transform it to response.
Install
$ npm i @teakit/errors --save
Usage
Create an Error
const { TeaError, TeaException } = require('@teakit/errors');
let err = new TeaError('tea error');
console.log(TeaError.getType(err)); // ERROR
Create an Exception
err = new TeaException('tea exception');
console.log(TeaException.getType(err)); // EXCEPTION
You can import an error from an normal error object
err = new Error('normal error');
console.log(TeaError.getType(err)); // BUILTIN
err = TeaError.from(err);
console.log(TeaError.getType(err)); // ERROR
Customize Error
Error can be extendable.
const { TeaBaseError } = require('@teakit/errors');
class CustomError extends TeaBaseError {
constructor(message) {
super({ message, code: 'CUSTOM_CODE' });
}
}
or using typescript you can customize ErrorOptions.
import { TeaBaseError, ErrorOptions } from '@teakit/errors';
class CustomErrorOptions extends ErrorOptions {
public data: object;
}
class CustomError extends TeaBaseError<CustomErrorOptions> {
public data: object;
protected options: CustomErrorOptions;
constructor(options?: CustomErrorOptions) {
super(options);
this.data = this.options.data;
}
}
HTTP Errors
HTTP Errors is BUILTIN errors that transform 400 ~ 500 status code to error objects. HttpError extends TeaBaseError providing two properties which is status
and headers
;
const { ForbiddenError } = require('@teakit/errors');
const err = new ForbiddenError('your request is forbidden');
console.log(err.status); // 403
Support short name too:
const { E403 } = require('@teakit/errors');
const err = new E403('your request is forbidden');
console.log(err.status); // 403
FrameworkBaseError
FrameworkBaseError is for tea framework/plugin developer to throw framework error.it can format by FrameworkErrorFormater
FrameworkBaseError extends TeaBaseError providing three properties which is module
、serialNumber
and errorContext
FrameworkBaseError could not be used directly, framework/plugin should extends like this
const { FrameworkBaseError } = require('@teakit/errors');
class TeaMysqlError extends FrameworkBaseError {
// module should be implement
get module() {
return 'TEA_MYSQL';
}
}
const err = new TeaMysqlError('error message', '01', { traceId: 'xxx' });
console.log(err.module); // TEA_MYSQL
console.log(err.serialNumber); // 01
console.log(err.code); // TEA_MYSQL_01
console.log(err.errorContext); // { traceId: 'xxx' }
FrameworkErrorFormater
FrameworkErrorFormater will append a faq guide url in error message.this would be helpful when developer encountered a framework error
the faq guide url format: ${faqPrefix}/${err.module}#${err.serialNumber}
, faqPrefix
is https://teakit.org/faq
by default. can be extendable or set process.env.TEA_FRAMEWORK_ERR_FAQ_PERFIX
to override it.
const { FrameworkErrorFormater } = require('@teakit/errors');
class CustomErrorFormatter extends FrameworkErrorFormater {
static faqPrefix = 'http://www.custom.com/faq';
}
.format(err)
format error to message, it will not effect origin error
const { FrameworkBaseError, FrameworkErrorFormater } = require('@teakit/errors');
class TeaMysqlError extends FrameworkBaseError {
// module should be implement
get module() {
return 'TEA_MYSQL';
}
}
const message = FrameworkErrorFormater.format(new TeaMysqlError('error message', '01'));
console.log(message);
// => message format like this
framework.TeaMysqlError: error message [https://teakit.org/faq/TEA_MYSQL#01]
...stack
...
code: "TEA_MYSQL_01"
serialNumber: "01"
errorContext:
pid: 66568
hostname: xxx
// extends
class CustomErrorFormatter extends FrameworkErrorFormater {
static faqPrefix = 'http://www.custom.com/faq';
}
const message = CustomErrorFormatter.format(new TeaMysqlError('error message', '01'));
console.log(message);
// =>
framework.TeaMysqlError: error message [http://www.custom.com/faq/TEA_MYSQL#01]
...
.formatError(err)
append faq guide url to err.message
const { FrameworkBaseError, FrameworkErrorFormater } = require('@teakit/errors');
class TeaMysqlError extends FrameworkBaseError {
// module should be implement
get module() {
return 'TEA_MYSQL';
}
}
const err = FrameworkErrorFormater.formatError(new TeaMysqlError('error message', '01'));
console.log(err.message); // error message [https://teakit.org/faq/TEA_MYSQL#01]
Available Errors
BaseError
|- TeaBaseError
| |- TeaError
| |- HttpError
| | |- NotFoundError, alias to E404
| | `- ...
| |- FrameworkBaseError
| `- CustomError
`- TeaBaseException
|- TeaException
`- CustomException