Gathering detailed insights and metrics for egg-errors
Gathering detailed insights and metrics for egg-errors
Gathering detailed insights and metrics for egg-errors
Gathering detailed insights and metrics for egg-errors
npm install egg-errors
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
32 Stars
32 Commits
3 Forks
13 Watching
1 Branches
24 Contributors
Updated on 30 Jun 2024
TypeScript (96.79%)
JavaScript (3.21%)
Cumulative downloads
Total Downloads
Last day
-1%
3,926
Compared to previous day
Last week
-1.1%
25,691
Compared to previous week
Last month
42.2%
141,090
Compared to previous month
Last year
-58.3%
1,195,200
Compared to previous year
Errors for Egg.js
egg-errors provide two kinds of errors that is Error and Exception.
1$ npm i egg-errors --save
Create an Error
1const { EggError, EggException } = require('egg-errors');
2let err = new EggError('egg error');
3console.log(EggError.getType(err)); // ERROR
Create an Exception
1err = new EggException('egg exception');
2console.log(EggException.getType(err)); // EXCEPTION
You can import an error from an normal error object
1err = new Error('normal error');
2console.log(EggError.getType(err)); // BUILTIN
3err = EggError.from(err);
4console.log(EggError.getType(err)); // ERROR
Error can be extendable.
1const { EggBaseError } = require('egg-errors'); 2 3class CustomError extends EggBaseError { 4 constructor(message) { 5 super({ message, code: 'CUSTOM_CODE' }); 6 } 7 }
or using typescript you can customize ErrorOptions.
1import { EggBaseError, ErrorOptions } from 'egg-errors'; 2 3class CustomErrorOptions extends ErrorOptions { 4 public data: object; 5} 6class CustomError extends EggBaseError<CustomErrorOptions> { 7 public data: object; 8 protected options: CustomErrorOptions; 9 10 constructor(options?: CustomErrorOptions) { 11 super(options); 12 this.data = this.options.data; 13 } 14}
Recommend use message instead of options in user land that it can be easily understood by developer, see http error.
HTTP Errors is BUILTIN errors that transform 400 ~ 500 status code to error objects. HttpError extends EggBaseError providing two properties which is status
and headers
;
1const { ForbiddenError } = require('egg-errors'); 2const err = new ForbiddenError('your request is forbidden'); 3console.log(err.status); // 403
Support short name too:
1const { E403 } = require('egg-errors'); 2const err = new E403('your request is forbidden'); 3console.log(err.status); // 403
FrameworkBaseError is for egg framework/plugin developer to throw framework error.it can format by FrameworkErrorFormater
FrameworkBaseError extends EggBaseError providing three properties which is module
、serialNumber
and errorContext
FrameworkBaseError could not be used directly, framework/plugin should extends like this
1const { FrameworkBaseError } = require('egg-errors'); 2 3class EggMysqlError extends FrameworkBaseError { 4 // module should be implement 5 get module() { 6 return 'EGG_MYSQL'; 7 } 8} 9 10const err = new EggMysqlError('error message', '01', { traceId: 'xxx' }); 11console.log(err.module); // EGG_MYSQL 12console.log(err.serialNumber); // 01 13console.log(err.code); // EGG_MYSQL_01 14console.log(err.errorContext); // { traceId: 'xxx' }
use the static method .create(message: string, serialNumber: string | number, errorContext?: any)
to new a frameworkError and format it convenient
1const { FrameworkBaseError } = require('egg-errors'); 2 3class EggMysqlError extends FrameworkBaseError { 4 // module should be implement 5 get module() { 6 return 'EGG_MYSQL'; 7 } 8} 9 10const err = EggMysqlError.create('error message', '01', { traceId: 'xxx' }); 11console.log(err.message); 12// => 13framework.EggMysqlError: error message [ https://eggjs.org/zh-cn/faq/EGG_MYSQL/01 ]
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://eggjs.org/zh-cn/faq
by default. can be extendable or set process.env.EGG_FRAMEWORK_ERR_FAQ_PERFIX
to override it.
1const { FrameworkErrorFormater } = require('egg-errors'); 2 3class CustomErrorFormatter extends FrameworkErrorFormater { 4 static faqPrefix = 'http://www.custom.com/faq'; 5}
format error to message, it will not effect origin error
1const { FrameworkBaseError, FrameworkErrorFormater } = require('egg-errors'); 2 3class EggMysqlError extends FrameworkBaseError { 4 // module should be implement 5 get module() { 6 return 'EGG_MYSQL'; 7 } 8} 9 10const message = FrameworkErrorFormater.format(new EggMysqlError('error message', '01')); 11console.log(message); 12// => message format like this 13framework.EggMysqlError: error message [ https://eggjs.org/zh-cn/faq/EGG_MYSQL/01 ] 14...stack 15... 16code: "EGG_MYSQL_01" 17serialNumber: "01" 18errorContext: 19pid: 66568 20hostname: xxx 21 22 23// extends 24class CustomErrorFormatter extends FrameworkErrorFormater { 25 static faqPrefix = 'http://www.custom.com/faq'; 26} 27const message = CustomErrorFormatter.format(new EggMysqlError('error message', '01')); 28console.log(message); 29// => 30framework.EggMysqlError: error message [ http://www.custom.com/faq/EGG_MYSQL/01 ] 31...
append faq guide url to err.message
1const { FrameworkBaseError, FrameworkErrorFormater } = require('egg-errors'); 2 3class EggMysqlError extends FrameworkBaseError { 4 // module should be implement 5 get module() { 6 return 'EGG_MYSQL'; 7 } 8} 9 10const err = FrameworkErrorFormater.formatError(new EggMysqlError('error message', '01')); 11console.log(err.message); // error message [ https://eggjs.org/zh-cn/faq/EGG_MYSQL/01 ]
BaseError
|- EggBaseError
| |- EggError
| |- HttpError
| | |- NotFoundError, alias to E404
| | `- ...
| |- FrameworkBaseError
| `- CustomError
`- EggBaseException
|- EggException
`- CustomException
Please open an issue here.
popomore | mansonchor | fengmk2 | beliefgp | sm2017 |
---|
This project follows the git-contributor spec, auto updated at Tue Feb 22 2022 11:32:47 GMT+0800
.
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
SAST tool detected but not run on all commits
Details
Reason
Found 9/30 approved changesets -- score normalized to 3
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
security policy file not detected
Details
Score
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More