From 380a9ca6150c8cab3b9e064adce0965080bfd232 Mon Sep 17 00:00:00 2001 From: Danilo Tuler Date: Fri, 1 Sep 2023 14:00:15 -0300 Subject: [PATCH] Improve router exception handling --- .changeset/hungry-buttons-shop.md | 5 +++++ .changeset/silly-bananas-reflect.md | 5 +++++ packages/router/package.json | 1 + packages/router/src/index.ts | 27 +++++++++++++++------------ packages/tsconfig/base.json | 2 +- pnpm-lock.yaml | 3 +++ 6 files changed, 30 insertions(+), 13 deletions(-) create mode 100644 .changeset/hungry-buttons-shop.md create mode 100644 .changeset/silly-bananas-reflect.md diff --git a/.changeset/hungry-buttons-shop.md b/.changeset/hungry-buttons-shop.md new file mode 100644 index 0000000..8f6db5e --- /dev/null +++ b/.changeset/hungry-buttons-shop.md @@ -0,0 +1,5 @@ +--- +"@deroll/tsconfig": major +--- + +change target to ES2022 diff --git a/.changeset/silly-bananas-reflect.md b/.changeset/silly-bananas-reflect.md new file mode 100644 index 0000000..124acfe --- /dev/null +++ b/.changeset/silly-bananas-reflect.md @@ -0,0 +1,5 @@ +--- +"@deroll/router": patch +--- + +improving error handling by showing route raising exception diff --git a/packages/router/package.json b/packages/router/package.json index 4b44271..7970a71 100644 --- a/packages/router/package.json +++ b/packages/router/package.json @@ -25,6 +25,7 @@ }, "devDependencies": { "@deroll/tsconfig": "workspace:*", + "@types/node": "^20", "eslint": "^8", "eslint-config-deroll": "workspace:*", "npm-run-all": "^4", diff --git a/packages/router/src/index.ts b/packages/router/src/index.ts index 03da9b1..0acb32f 100644 --- a/packages/router/src/index.ts +++ b/packages/router/src/index.ts @@ -39,23 +39,26 @@ export class Router { for (const route of this.routes) { const match = route.matcher(url); if (match) { - return route.handler(match, route); + try { + return route.handler(match, route); + } catch (e) { + throw new Error(`Error handling route ${url}`, { + cause: e, + }); + } } } return undefined; } - public async handler(data: InspectRequestData) { - try { - const url = bytesToString(toBytes(data.payload)); - const result = this.handle(url); - if (result) { - await this.options.app.createReport({ - payload: toHex(stringToBytes(result)), - }); - } - } catch (e) { - console.error(e); + public async handler(data: InspectRequestData): Promise { + const url = bytesToString(toBytes(data.payload)); + const result = this.handle(url); + if (result) { + // create single report with handler result + await this.options.app.createReport({ + payload: toHex(stringToBytes(result)), + }); } } } diff --git a/packages/tsconfig/base.json b/packages/tsconfig/base.json index 066f260..707dc0e 100644 --- a/packages/tsconfig/base.json +++ b/packages/tsconfig/base.json @@ -15,7 +15,7 @@ "preserveWatchOutput": true, "skipLibCheck": true, "strict": true, - "target": "ES2020" + "target": "ES2022" }, "exclude": ["node_modules"] } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index d537479..0fd152e 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -153,6 +153,9 @@ importers: '@deroll/tsconfig': specifier: workspace:* version: link:../tsconfig + '@types/node': + specifier: ^20 + version: 20.5.7 eslint: specifier: ^8 version: 8.48.0