Skip to content

Commit

Permalink
Improve router exception handling
Browse files Browse the repository at this point in the history
  • Loading branch information
tuler committed Sep 1, 2023
1 parent fc2df22 commit 380a9ca
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changeset/hungry-buttons-shop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@deroll/tsconfig": major
---

change target to ES2022
5 changes: 5 additions & 0 deletions .changeset/silly-bananas-reflect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@deroll/router": patch
---

improving error handling by showing route raising exception
1 change: 1 addition & 0 deletions packages/router/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
},
"devDependencies": {
"@deroll/tsconfig": "workspace:*",
"@types/node": "^20",
"eslint": "^8",
"eslint-config-deroll": "workspace:*",
"npm-run-all": "^4",
Expand Down
27 changes: 15 additions & 12 deletions packages/router/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
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)),
});
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/tsconfig/base.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"preserveWatchOutput": true,
"skipLibCheck": true,
"strict": true,
"target": "ES2020"
"target": "ES2022"
},
"exclude": ["node_modules"]
}
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 380a9ca

Please sign in to comment.