Skip to content

Commit

Permalink
Refactor onerror handler
Browse files Browse the repository at this point in the history
  • Loading branch information
otiai10 committed Sep 3, 2023
1 parent 1197b6a commit d587eab
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
21 changes: 17 additions & 4 deletions src/router.ts
Expand Up @@ -33,6 +33,16 @@ export class Router<T extends chrome.events.Event<any>, U = Record<string, unkno
this.notfound = callback
}

// Error handler
private error: HandlerOf<ExtractCallback<T>> = async function (this: { route: Resolved<U> }, ...args) {
return { status: 500, message: `Handler for request "${this.route[ActionKey]}" throw an error` }
}

// onError can overwire behavior for 500-ish error
public onError (callback: HandlerOf<ExtractCallback<T>>): void {
this.error = callback
}

private readonly routes: {
exact: Array<RouteMatcher<HandlerOf<ExtractCallback<T>>>>
regex: Array<RouteMatcher<HandlerOf<ExtractCallback<T>>>>
Expand Down Expand Up @@ -80,11 +90,14 @@ export class Router<T extends chrome.events.Event<any>, U = Record<string, unkno
this.resolver(...args).then(route => {
const fn = this.findHandler(route[ActionKey])
const res = fn(...args)
if (res instanceof Promise) void res.then(sendResponse)
else sendResponse(res)
if (res instanceof Promise) {
res.then(sendResponse).catch(err => this.error.bind({
route, error: err
})(...args).then(sendResponse))
} else sendResponse(res)
}).catch(err => {
console.error(err)
sendResponse({ [ActionKey]: '__notfound__', ...args }) // TODO: Handle error case
const fn = this.error.bind({ route: { [ActionKey]: '__router_error__', error: err } })
void fn(...args).then(sendResponse)
})
return true
}) as ExtractCallback<T>
Expand Down
18 changes: 18 additions & 0 deletions tests/spec/router.spec.ts
Expand Up @@ -70,4 +70,22 @@ describe('Router', () => {
expect(sendResponse).toBeCalledWith({ status: 5004, message: 'See you yesterday ;)' })
})
})

describe('onError', () => {
it('should overwrite a handler for error', async () => {
const r = new Router<chrome.runtime.ExtensionMessageEvent, { status: number }>()
const onerror = jest.fn().mockName('callback').mockImplementation(async function (this: any) {
return { message: 'See you yesterday ;)', status: 500, error: this.error }
})
const sendResponse = jest.fn().mockName('sendResponse')
r.onError(onerror)
r.on('/problem', async function () {
throw new Error('Something wrong')
})
r.listener()({ action: '/problem' }, {}, sendResponse)
await new Promise(resolve => setTimeout(resolve, 0))
expect(onerror).toBeCalled()
expect(sendResponse).toBeCalledWith({ status: 500, message: 'See you yesterday ;)', error: new Error('Something wrong') })
})
})
})

0 comments on commit d587eab

Please sign in to comment.