Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions plugins/onerror/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"./app": "./src/app.ts",
"./config/config.default": "./src/config/config.default.ts",
"./lib/error_view": "./src/lib/error_view.ts",
"./lib/onerror_page": "./src/lib/onerror_page.ts",
"./lib/utils": "./src/lib/utils.ts",
"./types": "./src/types.ts",
"./package.json": "./package.json"
Expand All @@ -40,6 +41,7 @@
"./app": "./dist/app.js",
"./config/config.default": "./dist/config/config.default.js",
"./lib/error_view": "./dist/lib/error_view.js",
"./lib/onerror_page": "./dist/lib/onerror_page.js",
"./lib/utils": "./dist/lib/utils.js",
"./types": "./dist/types.js",
"./package.json": "./package.json"
Expand Down
3 changes: 2 additions & 1 deletion plugins/onerror/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { onerror, type OnerrorOptions, type OnerrorError } from 'koa-onerror';

import type { OnerrorConfig } from './config/config.default.ts';
import { ErrorView } from './lib/error_view.ts';
import { ONERROR_PAGE_TEMPLATE } from './lib/onerror_page.ts';
import { isProd, detectStatus, detectErrorMessage, accepts } from './lib/utils.ts';

export interface OnerrorErrorWithCode extends OnerrorError {
Expand All @@ -23,7 +24,7 @@ export default class Boot implements ILifecycleBoot {
async didLoad(): Promise<void> {
// logging error
const config = this.app.config.onerror;
const viewTemplate = fs.readFileSync(config.templatePath, 'utf8');
const viewTemplate = config.templatePath ? fs.readFileSync(config.templatePath, 'utf8') : ONERROR_PAGE_TEMPLATE;
Copy link

Copilot AI Apr 14, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because ONERROR_PAGE_TEMPLATE is imported at module load time, the runtime must parse/allocate the large inlined template even when users provide templatePath (and the built-in template is never used). If startup cost matters, consider deferring loading the default template (e.g., via a dynamic import or a getter function that’s only invoked when templatePath is falsy). Tradeoff: dynamic import adds a small async boundary/complexity but avoids paying the parse cost in override scenarios.

Copilot uses AI. Check for mistakes.
const app = this.app;
app.on('error', (err, ctx) => {
if (!ctx) {
Expand Down
8 changes: 4 additions & 4 deletions plugins/onerror/src/config/config.default.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import path from 'node:path';

import type { Context } from 'egg';
import type { OnerrorError, OnerrorOptions } from 'koa-onerror';

Expand All @@ -20,7 +18,9 @@ export interface OnerrorConfig extends OnerrorOptions {
*/
appErrorFilter?: (err: OnerrorError, ctx: Context) => boolean;
/**
* default template path
* Custom template path. If empty, uses the built-in error page template.
*
* Default: `''`
*/
templatePath: string;
}
Expand All @@ -29,6 +29,6 @@ export default {
onerror: {
errorPageUrl: '',
appErrorFilter: undefined,
templatePath: path.join(import.meta.dirname, '../lib/onerror_page.mustache.html'),
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should delete onerror_page.mustache.html file too

templatePath: '',
} as OnerrorConfig,
};
Loading
Loading