@eggjs/errors 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (109) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +258 -0
  3. package/dist/base.d.ts +22 -0
  4. package/dist/base.js +35 -0
  5. package/dist/base_error.d.ts +9 -0
  6. package/dist/base_error.js +13 -0
  7. package/dist/base_exception.d.ts +9 -0
  8. package/dist/base_exception.js +13 -0
  9. package/dist/error.d.ts +9 -0
  10. package/dist/error.js +14 -0
  11. package/dist/error_options.d.ts +8 -0
  12. package/dist/error_options.js +8 -0
  13. package/dist/error_type.d.ts +18 -0
  14. package/dist/error_type.js +9 -0
  15. package/dist/exception.d.ts +9 -0
  16. package/dist/exception.js +14 -0
  17. package/dist/framework/formatter.d.ts +12 -0
  18. package/dist/framework/formatter.js +28 -0
  19. package/dist/framework/framework_base_error.d.ts +15 -0
  20. package/dist/framework/framework_base_error.js +37 -0
  21. package/dist/http/400.d.ts +8 -0
  22. package/dist/http/400.js +18 -0
  23. package/dist/http/401.d.ts +8 -0
  24. package/dist/http/401.js +18 -0
  25. package/dist/http/402.d.ts +8 -0
  26. package/dist/http/402.js +18 -0
  27. package/dist/http/403.d.ts +8 -0
  28. package/dist/http/403.js +18 -0
  29. package/dist/http/404.d.ts +8 -0
  30. package/dist/http/404.js +18 -0
  31. package/dist/http/405.d.ts +8 -0
  32. package/dist/http/405.js +18 -0
  33. package/dist/http/406.d.ts +8 -0
  34. package/dist/http/406.js +18 -0
  35. package/dist/http/407.d.ts +8 -0
  36. package/dist/http/407.js +18 -0
  37. package/dist/http/408.d.ts +8 -0
  38. package/dist/http/408.js +18 -0
  39. package/dist/http/409.d.ts +8 -0
  40. package/dist/http/409.js +18 -0
  41. package/dist/http/410.d.ts +8 -0
  42. package/dist/http/410.js +18 -0
  43. package/dist/http/411.d.ts +8 -0
  44. package/dist/http/411.js +18 -0
  45. package/dist/http/412.d.ts +8 -0
  46. package/dist/http/412.js +18 -0
  47. package/dist/http/413.d.ts +8 -0
  48. package/dist/http/413.js +18 -0
  49. package/dist/http/414.d.ts +8 -0
  50. package/dist/http/414.js +18 -0
  51. package/dist/http/415.d.ts +8 -0
  52. package/dist/http/415.js +18 -0
  53. package/dist/http/416.d.ts +8 -0
  54. package/dist/http/416.js +18 -0
  55. package/dist/http/417.d.ts +8 -0
  56. package/dist/http/417.js +18 -0
  57. package/dist/http/418.d.ts +8 -0
  58. package/dist/http/418.js +18 -0
  59. package/dist/http/421.d.ts +8 -0
  60. package/dist/http/421.js +18 -0
  61. package/dist/http/422.d.ts +8 -0
  62. package/dist/http/422.js +18 -0
  63. package/dist/http/423.d.ts +8 -0
  64. package/dist/http/423.js +18 -0
  65. package/dist/http/424.d.ts +8 -0
  66. package/dist/http/424.js +18 -0
  67. package/dist/http/425.d.ts +8 -0
  68. package/dist/http/425.js +18 -0
  69. package/dist/http/426.d.ts +8 -0
  70. package/dist/http/426.js +18 -0
  71. package/dist/http/428.d.ts +8 -0
  72. package/dist/http/428.js +18 -0
  73. package/dist/http/429.d.ts +8 -0
  74. package/dist/http/429.js +18 -0
  75. package/dist/http/431.d.ts +8 -0
  76. package/dist/http/431.js +18 -0
  77. package/dist/http/451.d.ts +8 -0
  78. package/dist/http/451.js +18 -0
  79. package/dist/http/500.d.ts +8 -0
  80. package/dist/http/500.js +18 -0
  81. package/dist/http/501.d.ts +8 -0
  82. package/dist/http/501.js +18 -0
  83. package/dist/http/502.d.ts +8 -0
  84. package/dist/http/502.js +18 -0
  85. package/dist/http/503.d.ts +8 -0
  86. package/dist/http/503.js +18 -0
  87. package/dist/http/504.d.ts +8 -0
  88. package/dist/http/504.js +18 -0
  89. package/dist/http/505.d.ts +8 -0
  90. package/dist/http/505.js +18 -0
  91. package/dist/http/506.d.ts +8 -0
  92. package/dist/http/506.js +18 -0
  93. package/dist/http/507.d.ts +8 -0
  94. package/dist/http/507.js +18 -0
  95. package/dist/http/508.d.ts +8 -0
  96. package/dist/http/508.js +18 -0
  97. package/dist/http/509.d.ts +8 -0
  98. package/dist/http/509.js +18 -0
  99. package/dist/http/510.d.ts +8 -0
  100. package/dist/http/510.js +18 -0
  101. package/dist/http/511.d.ts +8 -0
  102. package/dist/http/511.js +18 -0
  103. package/dist/http/http_error.d.ts +13 -0
  104. package/dist/http/http_error.js +16 -0
  105. package/dist/http/http_error_options.d.ts +10 -0
  106. package/dist/http/http_header.d.ts +6 -0
  107. package/dist/index.d.ts +51 -0
  108. package/dist/index.js +52 -0
  109. package/package.json +52 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2017-present Alibaba Group Holding Limited and other contributors.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,258 @@
1
+ # egg-errors
2
+
3
+ [![NPM version][npm-image]][npm-url]
4
+ [![Node.js CI](https://github.com/eggjs/egg-errors/actions/workflows/nodejs.yml/badge.svg)](https://github.com/eggjs/egg-errors/actions/workflows/nodejs.yml)
5
+ [![Test coverage][codecov-image]][codecov-url]
6
+ [![Known Vulnerabilities][snyk-image]][snyk-url]
7
+ [![npm download][download-image]][download-url]
8
+
9
+ [npm-image]: https://img.shields.io/npm/v/egg-errors.svg?style=flat-square
10
+ [npm-url]: https://npmjs.org/package/egg-errors
11
+ [codecov-image]: https://codecov.io/gh/eggjs/egg-errors/branch/master/graph/badge.svg
12
+ [codecov-url]: https://codecov.io/gh/eggjs/egg-errors
13
+ [snyk-image]: https://snyk.io/test/npm/egg-errors/badge.svg?style=flat-square
14
+ [snyk-url]: https://snyk.io/test/npm/egg-errors
15
+ [download-image]: https://img.shields.io/npm/dm/egg-errors.svg?style=flat-square
16
+ [download-url]: https://npmjs.org/package/egg-errors
17
+
18
+ Errors for [Egg.js](https://eggjs.org)
19
+
20
+ egg-errors provide two kinds of errors that is Error and Exception.
21
+
22
+ - Exception is system error that egg will log an error and throw exception, but it will be catched by onerror plugin.
23
+ - Error is business error that egg will transform it to response.
24
+
25
+ ## Install
26
+
27
+ ```bash
28
+ $ npm i egg-errors --save
29
+ ```
30
+
31
+ ## Usage
32
+
33
+ Create an Error
34
+
35
+ ```js
36
+ const { EggError, EggException } = require('egg-errors');
37
+ let err = new EggError('egg error');
38
+ console.log(EggError.getType(err)); // ERROR
39
+ ```
40
+
41
+ Create an Exception
42
+
43
+ ```js
44
+ err = new EggException('egg exception');
45
+ console.log(EggException.getType(err)); // EXCEPTION
46
+ ```
47
+
48
+ You can import an error from an normal error object
49
+
50
+ ```js
51
+ err = new Error('normal error');
52
+ console.log(EggError.getType(err)); // BUILTIN
53
+ err = EggError.from(err);
54
+ console.log(EggError.getType(err)); // ERROR
55
+ ```
56
+
57
+ ### Customize Error
58
+
59
+ Error can be extendable.
60
+
61
+ ```js
62
+ const { EggBaseError } = require('egg-errors');
63
+
64
+ class CustomError extends EggBaseError {
65
+ constructor(message) {
66
+ super({ message, code: 'CUSTOM_CODE' });
67
+ }
68
+ }
69
+ ```
70
+
71
+ or using typescript you can customize ErrorOptions.
72
+
73
+ ```js
74
+ import { EggBaseError, ErrorOptions } from 'egg-errors';
75
+
76
+ class CustomErrorOptions extends ErrorOptions {
77
+ public data: object;
78
+ }
79
+ class CustomError extends EggBaseError<CustomErrorOptions> {
80
+ public data: object;
81
+ protected options: CustomErrorOptions;
82
+
83
+ constructor(options?: CustomErrorOptions) {
84
+ super(options);
85
+ this.data = this.options.data;
86
+ }
87
+ }
88
+ ```
89
+
90
+ Recommend use message instead of options in user land that it can be easily understood by developer, see [http error](https://github.com/eggjs/egg-errors/blob/master/lib/http/400.ts).
91
+
92
+ ### HTTP Errors
93
+
94
+ 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`;
95
+
96
+ ```js
97
+ const { ForbiddenError } = require('egg-errors');
98
+ const err = new ForbiddenError('your request is forbidden');
99
+ console.log(err.status); // 403
100
+ ```
101
+
102
+ Support short name too:
103
+
104
+ ```js
105
+ const { E403 } = require('egg-errors');
106
+ const err = new E403('your request is forbidden');
107
+ console.log(err.status); // 403
108
+ ```
109
+
110
+ ### FrameworkBaseError
111
+
112
+ FrameworkBaseError is for egg framework/plugin developer to throw framework error.it can format by FrameworkErrorFormater
113
+
114
+ FrameworkBaseError extends EggBaseError providing three properties which is `module`、`serialNumber` and `errorContext`
115
+
116
+ FrameworkBaseError could not be used directly, framework/plugin should extends like this
117
+
118
+ ```js
119
+ const { FrameworkBaseError } = require('egg-errors');
120
+
121
+ class EggMysqlError extends FrameworkBaseError {
122
+ // module should be implement
123
+ get module() {
124
+ return 'EGG_MYSQL';
125
+ }
126
+ }
127
+
128
+ const err = new EggMysqlError('error message', '01', { traceId: 'xxx' });
129
+ console.log(err.module); // EGG_MYSQL
130
+ console.log(err.serialNumber); // 01
131
+ console.log(err.code); // EGG_MYSQL_01
132
+ console.log(err.errorContext); // { traceId: 'xxx' }
133
+ ```
134
+
135
+ #### create frameworkError with formater
136
+
137
+ use the static method `.create(message: string, serialNumber: string | number, errorContext?: any)` to new a frameworkError and format it convenient
138
+
139
+ ```js
140
+ const { FrameworkBaseError } = require('egg-errors');
141
+
142
+ class EggMysqlError extends FrameworkBaseError {
143
+ // module should be implement
144
+ get module() {
145
+ return 'EGG_MYSQL';
146
+ }
147
+ }
148
+
149
+ const err = EggMysqlError.create('error message', '01', { traceId: 'xxx' });
150
+ console.log(err.message);
151
+ // =>
152
+ framework.EggMysqlError: error message [ https://eggjs.org/zh-cn/faq/EGG_MYSQL/01 ]
153
+ ```
154
+
155
+ ### FrameworkErrorFormater
156
+
157
+ FrameworkErrorFormater will append a faq guide url in error message.this would be helpful when developer encountered a framework error
158
+
159
+ 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.
160
+
161
+ ```js
162
+ const { FrameworkErrorFormater } = require('egg-errors');
163
+
164
+ class CustomErrorFormatter extends FrameworkErrorFormater {
165
+ static faqPrefix = 'http://www.custom.com/faq';
166
+ }
167
+ ```
168
+
169
+ #### .format(err)
170
+
171
+ format error to message, it will not effect origin error
172
+
173
+ ```js
174
+ const { FrameworkBaseError, FrameworkErrorFormater } = require('egg-errors');
175
+
176
+ class EggMysqlError extends FrameworkBaseError {
177
+ // module should be implement
178
+ get module() {
179
+ return 'EGG_MYSQL';
180
+ }
181
+ }
182
+
183
+ const message = FrameworkErrorFormater.format(new EggMysqlError('error message', '01'));
184
+ console.log(message);
185
+ // => message format like this
186
+ framework.EggMysqlError: error message [ https://eggjs.org/zh-cn/faq/EGG_MYSQL/01 ]
187
+ ...stack
188
+ ...
189
+ code: "EGG_MYSQL_01"
190
+ serialNumber: "01"
191
+ errorContext:
192
+ pid: 66568
193
+ hostname: xxx
194
+
195
+
196
+ // extends
197
+ class CustomErrorFormatter extends FrameworkErrorFormater {
198
+ static faqPrefix = 'http://www.custom.com/faq';
199
+ }
200
+ const message = CustomErrorFormatter.format(new EggMysqlError('error message', '01'));
201
+ console.log(message);
202
+ // =>
203
+ framework.EggMysqlError: error message [ http://www.custom.com/faq/EGG_MYSQL/01 ]
204
+ ...
205
+ ```
206
+
207
+ #### .formatError(err)
208
+
209
+ append faq guide url to err.message
210
+
211
+ ```js
212
+ const { FrameworkBaseError, FrameworkErrorFormater } = require('egg-errors');
213
+
214
+ class EggMysqlError extends FrameworkBaseError {
215
+ // module should be implement
216
+ get module() {
217
+ return 'EGG_MYSQL';
218
+ }
219
+ }
220
+
221
+ const err = FrameworkErrorFormater.formatError(new EggMysqlError('error message', '01'));
222
+ console.log(err.message); // error message [ https://eggjs.org/zh-cn/faq/EGG_MYSQL/01 ]
223
+ ```
224
+
225
+ ### Available Errors
226
+
227
+ ```
228
+ BaseError
229
+ |- EggBaseError
230
+ | |- EggError
231
+ | |- HttpError
232
+ | | |- NotFoundError, alias to E404
233
+ | | `- ...
234
+ | |- FrameworkBaseError
235
+ | `- CustomError
236
+ `- EggBaseException
237
+ |- EggException
238
+ `- CustomException
239
+ ```
240
+
241
+ ## Questions & Suggestions
242
+
243
+ Please open an issue [here](https://github.com/eggjs/egg/issues?q=is%3Aissue+is%3Aopen+sort%3Aupdated-desc).
244
+
245
+ ## License
246
+
247
+ [MIT](LICENSE)
248
+
249
+ <!-- GITCONTRIBUTOR_START -->
250
+
251
+ ## Contributors
252
+
253
+ | [<img src="https://avatars.githubusercontent.com/u/360661?v=4" width="100px;"/><br/><sub><b>popomore</b></sub>](https://github.com/popomore)<br/> | [<img src="https://avatars.githubusercontent.com/u/2160731?v=4" width="100px;"/><br/><sub><b>mansonchor</b></sub>](https://github.com/mansonchor)<br/> | [<img src="https://avatars.githubusercontent.com/u/156269?v=4" width="100px;"/><br/><sub><b>fengmk2</b></sub>](https://github.com/fengmk2)<br/> | [<img src="https://avatars.githubusercontent.com/u/12657964?v=4" width="100px;"/><br/><sub><b>beliefgp</b></sub>](https://github.com/beliefgp)<br/> | [<img src="https://avatars.githubusercontent.com/u/19644997?v=4" width="100px;"/><br/><sub><b>sm2017</b></sub>](https://github.com/sm2017)<br/> |
254
+ | :-----------------------------------------------------------------------------------------------------------------------------------------------: | :----------------------------------------------------------------------------------------------------------------------------------------------------: | :---------------------------------------------------------------------------------------------------------------------------------------------: | :-------------------------------------------------------------------------------------------------------------------------------------------------: | :---------------------------------------------------------------------------------------------------------------------------------------------: |
255
+
256
+ This project follows the git-contributor [spec](https://github.com/xudafeng/git-contributor), auto updated at `Tue Feb 22 2022 11:32:47 GMT+0800`.
257
+
258
+ <!-- GITCONTRIBUTOR_END -->
package/dist/base.d.ts ADDED
@@ -0,0 +1,22 @@
1
+ import { ErrorOptions } from "./error_options.js";
2
+ import { ErrorType } from "./error_type.js";
3
+
4
+ //#region src/base.d.ts
5
+ declare const TYPE: symbol;
6
+ declare class BaseError<T extends ErrorOptions> extends Error {
7
+ [TYPE]?: ErrorType | undefined;
8
+ [key: string]: any;
9
+ static getType(err: Error): ErrorType;
10
+ /**
11
+ * Create a new instance of the error class from an existing error
12
+ * @param err - The error to create a new instance from
13
+ * @param args - The arguments to pass to the constructor
14
+ * @returns A new instance of the error class
15
+ */
16
+ static from<S extends new (...args: any) => InstanceType<typeof BaseError>, P extends ConstructorParameters<S>>(this: S, err: Error, ...args: P | undefined[]): InstanceType<S>;
17
+ code: string;
18
+ protected options: T;
19
+ constructor(options?: T);
20
+ }
21
+ //#endregion
22
+ export { BaseError };
package/dist/base.js ADDED
@@ -0,0 +1,35 @@
1
+ import { ErrorType } from "./error_type.js";
2
+
3
+ //#region src/base.ts
4
+ const TYPE = Symbol.for("BaseError#type");
5
+ var BaseError = class extends Error {
6
+ [TYPE];
7
+ static getType(err) {
8
+ return err[TYPE] ?? ErrorType.BUILTIN;
9
+ }
10
+ /**
11
+ * Create a new instance of the error class from an existing error
12
+ * @param err - The error to create a new instance from
13
+ * @param args - The arguments to pass to the constructor
14
+ * @returns A new instance of the error class
15
+ */
16
+ static from(err, ...args) {
17
+ const newErr = new this(...args);
18
+ newErr.message = err.message;
19
+ newErr.stack = err.stack;
20
+ for (const key of Object.keys(err)) newErr[key] = err[key];
21
+ return newErr;
22
+ }
23
+ code;
24
+ options;
25
+ constructor(options) {
26
+ super();
27
+ this.options = options ?? {};
28
+ this.message = this.options.message ?? "";
29
+ this.code = this.options.code ?? "";
30
+ this.name = this.constructor.name;
31
+ }
32
+ };
33
+
34
+ //#endregion
35
+ export { BaseError, TYPE };
@@ -0,0 +1,9 @@
1
+ import { ErrorOptions } from "./error_options.js";
2
+ import { BaseError } from "./base.js";
3
+
4
+ //#region src/base_error.d.ts
5
+ declare class EggBaseError<T extends ErrorOptions> extends BaseError<T> {
6
+ constructor(options?: T);
7
+ }
8
+ //#endregion
9
+ export { EggBaseError };
@@ -0,0 +1,13 @@
1
+ import { ErrorType } from "./error_type.js";
2
+ import { BaseError, TYPE } from "./base.js";
3
+
4
+ //#region src/base_error.ts
5
+ var EggBaseError = class extends BaseError {
6
+ constructor(options) {
7
+ super(options);
8
+ this[TYPE] = ErrorType.ERROR;
9
+ }
10
+ };
11
+
12
+ //#endregion
13
+ export { EggBaseError };
@@ -0,0 +1,9 @@
1
+ import { ErrorOptions } from "./error_options.js";
2
+ import { BaseError } from "./base.js";
3
+
4
+ //#region src/base_exception.d.ts
5
+ declare class EggBaseException<T extends ErrorOptions> extends BaseError<T> {
6
+ constructor(options?: T);
7
+ }
8
+ //#endregion
9
+ export { EggBaseException };
@@ -0,0 +1,13 @@
1
+ import { ErrorType } from "./error_type.js";
2
+ import { BaseError, TYPE } from "./base.js";
3
+
4
+ //#region src/base_exception.ts
5
+ var EggBaseException = class extends BaseError {
6
+ constructor(options) {
7
+ super(options);
8
+ this[TYPE] = ErrorType.EXCEPTION;
9
+ }
10
+ };
11
+
12
+ //#endregion
13
+ export { EggBaseException };
@@ -0,0 +1,9 @@
1
+ import { ErrorOptions } from "./error_options.js";
2
+ import { EggBaseError } from "./base_error.js";
3
+
4
+ //#region src/error.d.ts
5
+ declare class EggError extends EggBaseError<ErrorOptions> {
6
+ constructor(message?: string);
7
+ }
8
+ //#endregion
9
+ export { EggError };
package/dist/error.js ADDED
@@ -0,0 +1,14 @@
1
+ import { EggBaseError } from "./base_error.js";
2
+
3
+ //#region src/error.ts
4
+ var EggError = class extends EggBaseError {
5
+ constructor(message) {
6
+ super({
7
+ code: "EGG_ERROR",
8
+ message: message ?? ""
9
+ });
10
+ }
11
+ };
12
+
13
+ //#endregion
14
+ export { EggError };
@@ -0,0 +1,8 @@
1
+ //#region src/error_options.d.ts
2
+ declare class ErrorOptions {
3
+ code?: string;
4
+ message: string;
5
+ [key: string]: any;
6
+ }
7
+ //#endregion
8
+ export { ErrorOptions };
@@ -0,0 +1,8 @@
1
+ //#region src/error_options.ts
2
+ var ErrorOptions = class {
3
+ code;
4
+ message;
5
+ };
6
+
7
+ //#endregion
8
+ export { ErrorOptions };
@@ -0,0 +1,18 @@
1
+ //#region src/error_type.d.ts
2
+ declare const ErrorType: {
3
+ /**
4
+ * Built-in Error
5
+ */
6
+ readonly BUILTIN: "BUILTIN";
7
+ /**
8
+ * Egg Error
9
+ */
10
+ readonly ERROR: "ERROR";
11
+ /**
12
+ * Egg Exception
13
+ */
14
+ readonly EXCEPTION: "EXCEPTION";
15
+ };
16
+ type ErrorType = (typeof ErrorType)[keyof typeof ErrorType];
17
+ //#endregion
18
+ export { ErrorType };
@@ -0,0 +1,9 @@
1
+ //#region src/error_type.ts
2
+ const ErrorType = {
3
+ BUILTIN: "BUILTIN",
4
+ ERROR: "ERROR",
5
+ EXCEPTION: "EXCEPTION"
6
+ };
7
+
8
+ //#endregion
9
+ export { ErrorType };
@@ -0,0 +1,9 @@
1
+ import { ErrorOptions } from "./error_options.js";
2
+ import { EggBaseException } from "./base_exception.js";
3
+
4
+ //#region src/exception.d.ts
5
+ declare class EggException extends EggBaseException<ErrorOptions> {
6
+ constructor(message?: string);
7
+ }
8
+ //#endregion
9
+ export { EggException };
@@ -0,0 +1,14 @@
1
+ import { EggBaseException } from "./base_exception.js";
2
+
3
+ //#region src/exception.ts
4
+ var EggException = class extends EggBaseException {
5
+ constructor(message) {
6
+ super({
7
+ code: "EGG_EXCEPTION",
8
+ message: message ?? ""
9
+ });
10
+ }
11
+ };
12
+
13
+ //#endregion
14
+ export { EggException };
@@ -0,0 +1,12 @@
1
+ //#region src/framework/formatter.d.ts
2
+ declare class FrameworkErrorFormater {
3
+ protected static faqPrefix: string;
4
+ /**
5
+ * Custom framework error FAQ prefix
6
+ */
7
+ private static faqPrefixEnv;
8
+ static format(err: Error): string;
9
+ static formatError<T extends Error>(err: T): T;
10
+ }
11
+ //#endregion
12
+ export { FrameworkErrorFormater };
@@ -0,0 +1,28 @@
1
+ import { FrameworkBaseError } from "./framework_base_error.js";
2
+ import util from "node:util";
3
+ import os from "node:os";
4
+
5
+ //#region src/framework/formatter.ts
6
+ const hostname = os.hostname();
7
+ var FrameworkErrorFormater = class {
8
+ static faqPrefix = "https://eggjs.org/zh-cn/faq";
9
+ /**
10
+ * Custom framework error FAQ prefix
11
+ */
12
+ static faqPrefixEnv = process.env.EGG_FRAMEWORK_ERR_FAQ_PERFIX;
13
+ static format(err) {
14
+ const faqPrefix = this.faqPrefixEnv ?? this.faqPrefix;
15
+ let errMessage = err.message;
16
+ if (FrameworkBaseError.isFrameworkError(err) && !errMessage.includes(faqPrefix)) errMessage += ` [ ${faqPrefix}/${err.code} ]`;
17
+ const errStack = err.stack || "no_stack";
18
+ return util.format("framework.%s: %s\n%s\ncode: %s\nserialNumber: %s\nerrorContext: %j\npid: %s\nhostname: %s\n", err.name, errMessage, errStack.substring(errStack.indexOf("\n") + 1), err.code, err.serialNumber, err.errorContext, process.pid, hostname);
19
+ }
20
+ static formatError(err) {
21
+ const faqPrefix = this.faqPrefixEnv ?? this.faqPrefix;
22
+ if (FrameworkBaseError.isFrameworkError(err) && !err.message.includes(faqPrefix)) err.message += ` [ ${faqPrefix}/${err.code} ]`;
23
+ return err;
24
+ }
25
+ };
26
+
27
+ //#endregion
28
+ export { FrameworkErrorFormater };
@@ -0,0 +1,15 @@
1
+ import { ErrorOptions } from "../error_options.js";
2
+ import { EggBaseError } from "../base_error.js";
3
+
4
+ //#region src/framework/framework_base_error.d.ts
5
+ declare const FRAMEWORK_ERROR_SYMBOL: unique symbol;
6
+ declare class FrameworkBaseError extends EggBaseError<ErrorOptions> {
7
+ readonly serialNumber: string;
8
+ readonly errorContext?: any;
9
+ get module(): string;
10
+ constructor(message: string, serialNumber: string | number, errorContext?: any);
11
+ static create(message: string, serialNumber: string | number, errorContext?: any): FrameworkBaseError;
12
+ static isFrameworkError(err: Error): err is FrameworkBaseError;
13
+ }
14
+ //#endregion
15
+ export { FRAMEWORK_ERROR_SYMBOL, FrameworkBaseError };
@@ -0,0 +1,37 @@
1
+ import { EggBaseError } from "../base_error.js";
2
+ import { FrameworkErrorFormater } from "./formatter.js";
3
+ import assert from "node:assert";
4
+
5
+ //#region src/framework/framework_base_error.ts
6
+ const FRAMEWORK_ERROR_SYMBOL = Symbol.for("FrameworkBaseError");
7
+ var FrameworkBaseError = class extends EggBaseError {
8
+ serialNumber;
9
+ errorContext;
10
+ get module() {
11
+ throw new Error("module should be implement");
12
+ }
13
+ constructor(message, serialNumber, errorContext) {
14
+ super({
15
+ message,
16
+ serialNumber,
17
+ errorContext
18
+ });
19
+ assert(message, "message is required");
20
+ assert(serialNumber, "serialNumber is required");
21
+ this.serialNumber = `${serialNumber}`;
22
+ this.errorContext = errorContext ?? "";
23
+ this.code = `${this.module}_${this.serialNumber}`;
24
+ this[FRAMEWORK_ERROR_SYMBOL] = true;
25
+ }
26
+ static create(message, serialNumber, errorContext) {
27
+ const err = FrameworkErrorFormater.formatError(new this(message, serialNumber, errorContext));
28
+ Error.captureStackTrace(err, this.create);
29
+ return err;
30
+ }
31
+ static isFrameworkError(err) {
32
+ return err[FRAMEWORK_ERROR_SYMBOL] === true;
33
+ }
34
+ };
35
+
36
+ //#endregion
37
+ export { FRAMEWORK_ERROR_SYMBOL, FrameworkBaseError };
@@ -0,0 +1,8 @@
1
+ import { HttpError } from "./http_error.js";
2
+
3
+ //#region src/http/400.d.ts
4
+ declare class BadRequestError extends HttpError {
5
+ constructor(message?: string);
6
+ }
7
+ //#endregion
8
+ export { BadRequestError };
@@ -0,0 +1,18 @@
1
+ import { HttpError } from "./http_error.js";
2
+
3
+ //#region src/http/400.ts
4
+ var BadRequestError = class extends HttpError {
5
+ constructor(message) {
6
+ const status = 400;
7
+ const code = "BAD_REQUEST";
8
+ message = message ?? "Bad Request";
9
+ super({
10
+ code,
11
+ message,
12
+ status
13
+ });
14
+ }
15
+ };
16
+
17
+ //#endregion
18
+ export { BadRequestError };
@@ -0,0 +1,8 @@
1
+ import { HttpError } from "./http_error.js";
2
+
3
+ //#region src/http/401.d.ts
4
+ declare class UnauthorizedError extends HttpError {
5
+ constructor(message?: string);
6
+ }
7
+ //#endregion
8
+ export { UnauthorizedError };
@@ -0,0 +1,18 @@
1
+ import { HttpError } from "./http_error.js";
2
+
3
+ //#region src/http/401.ts
4
+ var UnauthorizedError = class extends HttpError {
5
+ constructor(message) {
6
+ const status = 401;
7
+ const code = "UNAUTHORIZED";
8
+ message = message ?? "Unauthorized";
9
+ super({
10
+ code,
11
+ message,
12
+ status
13
+ });
14
+ }
15
+ };
16
+
17
+ //#endregion
18
+ export { UnauthorizedError };
@@ -0,0 +1,8 @@
1
+ import { HttpError } from "./http_error.js";
2
+
3
+ //#region src/http/402.d.ts
4
+ declare class PaymentRequiredError extends HttpError {
5
+ constructor(message?: string);
6
+ }
7
+ //#endregion
8
+ export { PaymentRequiredError };
@@ -0,0 +1,18 @@
1
+ import { HttpError } from "./http_error.js";
2
+
3
+ //#region src/http/402.ts
4
+ var PaymentRequiredError = class extends HttpError {
5
+ constructor(message) {
6
+ const status = 402;
7
+ const code = "PAYMENT_REQUIRED";
8
+ message = message ?? "Payment Required";
9
+ super({
10
+ code,
11
+ message,
12
+ status
13
+ });
14
+ }
15
+ };
16
+
17
+ //#endregion
18
+ export { PaymentRequiredError };