-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
fix(onerror): avoid koa-onerror template reads #5927
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
31b6ae2
fix(onerror): avoid koa-onerror template reads
killagu a69713e
test(onerror): cover local error installer
killagu 7971918
fix(onerror): complete local handler parity
killagu a11182a
fix(onerror): preserve error response cookies
killagu 40c8dde
test(onerror): cover production fallback bodies
killagu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,163 @@ | ||
| import http from 'node:http'; | ||
| import { debuglog, inspect } from 'node:util'; | ||
|
|
||
| const debug = debuglog('egg-onerror'); | ||
|
|
||
| export type OnerrorError = Error & { | ||
| status: number; | ||
| code?: string; | ||
| headers?: Record<string, string>; | ||
| expose?: boolean; | ||
| }; | ||
|
|
||
| export type OnerrorHandler = (err: OnerrorError, ctx: any) => void; | ||
|
|
||
| export interface OnerrorOptions { | ||
| text?: OnerrorHandler; | ||
| json?: OnerrorHandler; | ||
| html?: OnerrorHandler; | ||
| all?: OnerrorHandler; | ||
| js?: OnerrorHandler; | ||
| redirect?: string | null; | ||
| accepts?: (...args: string[]) => string; | ||
| } | ||
|
|
||
| const defaultOptions: OnerrorOptions = { | ||
| text, | ||
| json, | ||
| html, | ||
| }; | ||
|
|
||
| export function onerror(app: any, options?: OnerrorOptions): any { | ||
| options = { ...defaultOptions, ...options }; | ||
|
|
||
| app.context.onerror = function (err: any) { | ||
| debug('onerror: %s', err); | ||
| if (err == null) return; | ||
|
|
||
| if (typeof this.req?.resume === 'function') { | ||
| this.req.resume(); | ||
| debug('resume the req stream'); | ||
| } | ||
|
|
||
| if (!(err instanceof Error)) { | ||
| debug('err is not an instance of Error'); | ||
| let errMsg = err; | ||
| if (typeof err === 'object') { | ||
| try { | ||
| errMsg = JSON.stringify(err); | ||
| } catch (e) { | ||
| debug('stringify error: %s', e); | ||
| errMsg = inspect(err); | ||
| } | ||
| } | ||
| const newError = new Error('non-error thrown: ' + errMsg); | ||
| if (err) { | ||
| if (err.name) newError.name = err.name; | ||
| if (err.message) newError.message = err.message; | ||
| if (err.stack) newError.stack = err.stack; | ||
| if (err.status) Reflect.set(newError, 'status', err.status); | ||
| if (err.headers) Reflect.set(newError, 'headers', err.headers); | ||
| } | ||
| err = newError; | ||
| debug('wrap err: %s', err); | ||
| } | ||
|
|
||
| const headerSent = this.headerSent || !this.writable; | ||
| if (headerSent) { | ||
| debug('headerSent is true'); | ||
| err.headerSent = true; | ||
| } | ||
|
|
||
| this.app.emit('error', err, this); | ||
| if (headerSent) return; | ||
|
|
||
| if (err.code === 'ENOENT') { | ||
| err.status = 404; | ||
| } | ||
| if (typeof err.status !== 'number' || !http.STATUS_CODES[err.status]) { | ||
| err.status = 500; | ||
| } | ||
| this.status = err.status; | ||
|
|
||
|
killagu marked this conversation as resolved.
|
||
| clearResponseHeaders(this); | ||
| if (err.headers) { | ||
| this.set(err.headers); | ||
| } | ||
| let type: string; | ||
| if (options.accepts) { | ||
| type = options.accepts.call(this, 'html', 'text', 'json', 'js'); | ||
| } else { | ||
| type = this.accepts('html', 'text', 'json', 'js'); | ||
| } | ||
| debug('accepts type: %s', type); | ||
| type = type || 'text'; | ||
| if (options.all) { | ||
| options.all.call(this, err, this); | ||
| } else if (options.redirect && type !== 'json') { | ||
| this.redirect(options.redirect); | ||
| } else { | ||
| const handler = getHandler(options, type); | ||
| handler?.call(this, err, this); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| this.type = type; | ||
| } | ||
|
|
||
| if (type === 'json' && typeof this.body !== 'string') { | ||
| this.body = JSON.stringify(this.body); | ||
| } | ||
| debug('end the response, body: %s', this.body); | ||
| this.res.end(this.body); | ||
| }; | ||
|
|
||
| return app; | ||
| } | ||
|
|
||
| function getHandler(options: OnerrorOptions, type: string): OnerrorHandler | undefined { | ||
| if (type === 'html' || type === 'text' || type === 'json' || type === 'js') { | ||
| return options[type]; | ||
| } | ||
| } | ||
|
|
||
| function isDev(): boolean { | ||
| return !process.env.NODE_ENV || process.env.NODE_ENV === 'development'; | ||
| } | ||
|
|
||
| function text(err: OnerrorError, ctx: any): void { | ||
| ctx.body = (isDev() || err.expose) && err.message ? err.message : http.STATUS_CODES[ctx.status]; | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| function json(err: OnerrorError, ctx: any): void { | ||
| const message = (isDev() || err.expose) && err.message ? err.message : http.STATUS_CODES[ctx.status]; | ||
| ctx.body = { error: message }; | ||
| } | ||
|
|
||
| function html(err: OnerrorError, ctx: any): void { | ||
| const message = (isDev() || err.expose) && err.message ? err.message : http.STATUS_CODES[ctx.status]; | ||
| ctx.body = `<h2>${escapeHtml(String(err.status))} ${escapeHtml(String(message))}</h2>`; | ||
| ctx.type = 'html'; | ||
| } | ||
|
|
||
| function escapeHtml(value: string): string { | ||
| return value.replace(/[&<>"']/g, (char) => { | ||
| switch (char) { | ||
| case '&': | ||
| return '&'; | ||
| case '<': | ||
| return '<'; | ||
| case '>': | ||
| return '>'; | ||
| case '"': | ||
| return '"'; | ||
| default: | ||
| return '''; | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| function clearResponseHeaders(ctx: any): void { | ||
| const headers = ctx.response?.header ?? ctx.response?.headers ?? ctx.res.getHeaders?.() ?? {}; | ||
| for (const name of Object.keys(headers)) { | ||
| if (name.toLowerCase() === 'set-cookie') continue; | ||
| ctx.res.removeHeader(name); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.