@eggjs/onerror 4.0.0-beta.20 → 4.0.0-beta.21

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.
@@ -1,30 +0,0 @@
1
- import { OnerrorError, OnerrorOptions } from "koa-onerror";
2
- import { Context } from "egg";
3
-
4
- //#region src/config/config.default.d.ts
5
- interface OnerrorConfig extends OnerrorOptions {
6
- /**
7
- * 5xx error will redirect to ${errorPageUrl}
8
- * won't redirect in local env
9
- *
10
- * Default: `''`
11
- */
12
- errorPageUrl: string | ((err: OnerrorError, ctx: Context) => string);
13
- /**
14
- * will execute `appErrorFilter` when emit an error in `app`
15
- * If `appErrorFilter` return false, egg-onerror won't log this error.
16
- * You can logging in `appErrorFilter` and return false to override the default error logging.
17
- *
18
- * Default: `undefined`
19
- */
20
- appErrorFilter?: (err: OnerrorError, ctx: Context) => boolean;
21
- /**
22
- * default template path
23
- */
24
- templatePath: string;
25
- }
26
- declare const _default: {
27
- onerror: OnerrorConfig;
28
- };
29
- //#endregion
30
- export { OnerrorConfig, _default };
@@ -1,224 +0,0 @@
1
- import { detectErrorMessage } from "./utils-S1o8_vNA.js";
2
- import fs from "node:fs";
3
- import path from "node:path";
4
- import util from "node:util";
5
- import { parse } from "cookie";
6
- import Mustache from "mustache";
7
- import stackTrace from "stack-trace";
8
-
9
- //#region src/lib/error_view.ts
10
- const startingSlashRegex = /\\|\//;
11
- var ErrorView = class {
12
- ctx;
13
- error;
14
- request;
15
- app;
16
- assets;
17
- viewTemplate;
18
- codeContext = 5;
19
- _filterHeaders = ["cookie", "connection"];
20
- constructor(ctx, error, template) {
21
- this.ctx = ctx;
22
- this.error = error;
23
- this.request = ctx.request;
24
- this.app = ctx.app;
25
- this.assets = /* @__PURE__ */ new Map();
26
- this.viewTemplate = template;
27
- }
28
- /**
29
- * get html error page
30
- *
31
- * @return {String} html page
32
- */
33
- toHTML() {
34
- const stack = this.parseError();
35
- const data = this.serializeData(stack, (frame, index) => {
36
- const serializedFrame = this.serializeFrame(frame);
37
- serializedFrame.classes = this.getFrameClasses(frame, index);
38
- return serializedFrame;
39
- });
40
- return this.compileView(this.viewTemplate, {
41
- ...data,
42
- appInfo: this.serializeAppInfo(),
43
- request: this.serializeRequest()
44
- });
45
- }
46
- /**
47
- * compile view
48
- *
49
- * @param {String} tpl - template
50
- * @param {Object} locals - data used by template
51
- */
52
- compileView(tpl, locals) {
53
- return Mustache.render(tpl, locals);
54
- }
55
- /**
56
- * check if the frame is node native file.
57
- *
58
- * @param {Frame} frame - current frame
59
- */
60
- isNode(frame) {
61
- if (frame.isNative()) return true;
62
- const filename = frame.getFileName() || "";
63
- return !path.isAbsolute(filename) && filename[0] !== ".";
64
- }
65
- /**
66
- * check if the frame is app modules.
67
- *
68
- * @param {Object} frame - current frame
69
- */
70
- isApp(frame) {
71
- if (this.isNode(frame)) return false;
72
- return !(frame.getFileName() || "").includes("node_modules" + path.sep);
73
- }
74
- /**
75
- * cache file asserts
76
- *
77
- * @param {String} key - assert key
78
- * @param {String} value - assert content
79
- */
80
- setAssets(key, value) {
81
- this.assets.set(key, value);
82
- }
83
- /**
84
- * get cache file asserts
85
- *
86
- * @param {String} key - assert key
87
- */
88
- getAssets(key) {
89
- return this.assets.get(key);
90
- }
91
- /**
92
- * get frame source
93
- *
94
- * @param {Object} frame - current frame
95
- */
96
- getFrameSource(frame) {
97
- const filename = frame.getFileName();
98
- const lineNumber = frame.getLineNumber();
99
- let contents = this.getAssets(filename);
100
- if (!contents) {
101
- contents = fs.existsSync(filename) ? fs.readFileSync(filename, "utf8") : "";
102
- this.setAssets(filename, contents);
103
- }
104
- const lines = contents.split(/\r?\n/);
105
- return {
106
- pre: lines.slice(Math.max(0, lineNumber - (this.codeContext + 1)), lineNumber - 1),
107
- line: lines[lineNumber - 1],
108
- post: lines.slice(lineNumber, lineNumber + this.codeContext)
109
- };
110
- }
111
- /**
112
- * parse error and return frame stack
113
- */
114
- parseError() {
115
- return stackTrace.parse(this.error).map((frame) => {
116
- if (!this.isNode(frame)) frame.context = this.getFrameSource(frame);
117
- return frame;
118
- });
119
- }
120
- /**
121
- * get stack context
122
- *
123
- * @param {Object} frame - current frame
124
- */
125
- getContext(frame) {
126
- if (!frame.context) return {};
127
- return {
128
- start: frame.getLineNumber() - (frame.context.pre || []).length,
129
- pre: frame.context.pre.join("\n"),
130
- line: frame.context.line,
131
- post: frame.context.post.join("\n")
132
- };
133
- }
134
- /**
135
- * get frame classes, let view identify the frame
136
- *
137
- * @param {any} frame - current frame
138
- * @param {any} index - current index
139
- */
140
- getFrameClasses(frame, index) {
141
- const classes = [];
142
- if (index === 0) classes.push("active");
143
- if (!this.isApp(frame)) classes.push("native-frame");
144
- return classes.join(" ");
145
- }
146
- /**
147
- * serialize frame and return meaningful data
148
- *
149
- * @param {Object} frame - current frame
150
- */
151
- serializeFrame(frame) {
152
- const filename = frame.getFileName();
153
- const relativeFileName = filename.includes(process.cwd()) ? filename.replace(process.cwd(), "").replace(startingSlashRegex, "") : filename;
154
- return {
155
- extname: path.extname(filename).replace(".", ""),
156
- file: relativeFileName,
157
- method: frame.getFunctionName(),
158
- line: frame.getLineNumber(),
159
- column: frame.getColumnNumber(),
160
- context: this.getContext(frame),
161
- classes: ""
162
- };
163
- }
164
- /**
165
- * serialize base data
166
- *
167
- * @param {Object} stack - frame stack
168
- * @param {Function} frameFormatter - frame formatter function
169
- */
170
- serializeData(stack, frameFormatter) {
171
- const code = Reflect.get(this.error, "code") ?? Reflect.get(this.error, "type");
172
- let message = detectErrorMessage(this.ctx, this.error);
173
- if (code) message = `${message} (code: ${code})`;
174
- return {
175
- code,
176
- message,
177
- name: this.error.name,
178
- status: this.error.status,
179
- frames: stack instanceof Array ? stack.filter((frame) => frame.getFileName()).map(frameFormatter) : []
180
- };
181
- }
182
- /**
183
- * serialize request object
184
- */
185
- serializeRequest() {
186
- const headers = [];
187
- Object.keys(this.request.headers).forEach((key) => {
188
- if (this._filterHeaders.includes(key)) return;
189
- headers.push({
190
- key,
191
- value: this.request.headers[key]
192
- });
193
- });
194
- const parsedCookies = parse(this.request.headers.cookie || "");
195
- const cookies = Object.keys(parsedCookies).map((key) => {
196
- return {
197
- key,
198
- value: parsedCookies[key]
199
- };
200
- });
201
- return {
202
- url: this.request.url,
203
- httpVersion: this.request.req.httpVersion,
204
- method: this.request.method,
205
- connection: this.request.headers.connection,
206
- headers,
207
- cookies
208
- };
209
- }
210
- /**
211
- * serialize app info object
212
- */
213
- serializeAppInfo() {
214
- let config = this.app.config;
215
- if ("dumpConfigToObject" in this.app && typeof this.app.dumpConfigToObject === "function") config = this.app.dumpConfigToObject().config.config;
216
- return {
217
- baseDir: this.app.config.baseDir,
218
- config: util.inspect(config)
219
- };
220
- }
221
- };
222
-
223
- //#endregion
224
- export { ErrorView };
@@ -1,8 +0,0 @@
1
- import { OnerrorConfig } from "./config.default-BeF-r3l_.js";
2
-
3
- //#region src/types.d.ts
4
- declare module 'egg' {
5
- interface EggAppConfig {
6
- onerror: OnerrorConfig;
7
- }
8
- }
@@ -1 +0,0 @@
1
- export { };
@@ -1,21 +0,0 @@
1
- //#region src/lib/utils.ts
2
- function detectErrorMessage(ctx, err) {
3
- if (err.status === 400 && err.name === "SyntaxError" && ctx.request.is("application/json", "application/vnd.api+json", "application/csp-report")) return "Problems parsing JSON";
4
- return err.message;
5
- }
6
- function detectStatus(err) {
7
- let status = err.status || 500;
8
- if (status < 200) status = 500;
9
- return status;
10
- }
11
- function accepts(ctx) {
12
- if (ctx.acceptJSON) return "json";
13
- if (ctx.acceptJSONP) return "js";
14
- return "html";
15
- }
16
- function isProd(app) {
17
- return app.config.env !== "local" && app.config.env !== "unittest";
18
- }
19
-
20
- //#endregion
21
- export { accepts, detectErrorMessage, detectStatus, isProd };