diff --git a/framework/framework/package.json b/framework/framework/package.json index 9ac1ab435..db2f76be2 100644 --- a/framework/framework/package.json +++ b/framework/framework/package.json @@ -1,6 +1,6 @@ { "name": "@hydrooj/framework", - "version": "0.3.1", + "version": "0.3.2", "main": "index.ts", "license": "MIT", "repository": "https://github.com/hydro-dev/Hydro", diff --git a/framework/framework/server.ts b/framework/framework/server.ts index 05d8d448e..02c321f42 100644 --- a/framework/framework/server.ts +++ b/framework/framework/server.ts @@ -14,7 +14,7 @@ import Schema from 'schemastery'; import { Shorty } from 'shorty.js'; import { WebSocket, WebSocketServer } from 'ws'; import { - Counter, errorMessage, isClass, Logger, parseMemoryMB, + Counter, errorMessage, isClass, Logger, parseMemoryMB, unwrapAggregateError, } from '@hydrooj/utils/lib/utils'; import base from './base'; import * as decorators from './decorators'; @@ -56,10 +56,6 @@ async function forkContextWithScope(ctx: CordisContext) { }; } -function unwrapAggregateError(error: any) { - return error instanceof AggregateError ? error.errors[0] : error; -} - export interface HydroRequest { method: string; host: string; @@ -259,6 +255,7 @@ export class Handler extends HandlerCommon { } async onerror(error: HydroError) { + error = unwrapAggregateError(error); error.msg ||= () => error.message; console.error(`Error on user request: ${error.msg()}\n`, error); if (error instanceof UserFacingError && !process.env.DEV) error.stack = ''; diff --git a/framework/utils/lib/utils.ts b/framework/utils/lib/utils.ts index ba8cc6ad5..ee2d2ee26 100644 --- a/framework/utils/lib/utils.ts +++ b/framework/utils/lib/utils.ts @@ -170,8 +170,15 @@ export const Time = { }, }; +export function unwrapAggregateError(error: any): any { + return error instanceof Error && Array.isArray((error as any).errors) && (error as any).errors.length + ? unwrapAggregateError((error as any).errors[0]) + : error; +} + export function errorMessage(err: Error | string) { - const t = typeof err === 'string' ? err : err.stack; + err = unwrapAggregateError(err); + const t = typeof err === 'string' ? err : err.stack || err.message || String(err); const lines = t.split('\n') .filter((i) => !i.includes(' (node:') && !i.includes('(internal')); let cursor = 1; diff --git a/framework/utils/package.json b/framework/utils/package.json index 7e5410c10..2a452aa4d 100644 --- a/framework/utils/package.json +++ b/framework/utils/package.json @@ -1,6 +1,6 @@ { "name": "@hydrooj/utils", - "version": "1.5.7", + "version": "1.5.8", "description": "hydrooj utils", "type": "module", "main": "lib/utils.ts",