Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add "unhandledException" life-cycle event #1199

Merged
merged 1 commit into from
May 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions src/sharedOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export interface LifeCycleEventsMap<ResponseType> {
'request:end': (request: MockedRequest) => void
'response:mocked': (response: ResponseType, requestId: string) => void
'response:bypass': (response: ResponseType, requestId: string) => void
unhandledException: (error: Error, request: MockedRequest) => void
}

export type LifeCycleEventEmitter<ResponseType> = Pick<
Expand Down
20 changes: 15 additions & 5 deletions src/utils/handleRequest.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { until } from '@open-draft/until'
import { StrictEventEmitter } from 'strict-event-emitter'
import { MockedRequest, RequestHandler } from '../handlers/RequestHandler'
import { ServerLifecycleEventsMap } from '../node/glossary'
Expand Down Expand Up @@ -65,11 +66,20 @@ export async function handleRequest<
}

// Resolve a mocked response from the list of request handlers.
const lookupResult = await getResponse(
request,
handlers,
handleRequestOptions?.resolutionContext,
)
const [lookupError, lookupResult] = await until(() => {
return getResponse(
request,
handlers,
handleRequestOptions?.resolutionContext,
)
})

if (lookupError) {
// Allow developers to react to unhandled exceptions in request handlers.
emitter.emit('unhandledException', lookupError, request)
MartinJaskulla marked this conversation as resolved.
Show resolved Hide resolved
throw lookupError
}

const { handler, response } = lookupResult

// When there's no handler for the request, consider it unhandled.
Expand Down
19 changes: 19 additions & 0 deletions test/msw-api/setup-server/life-cycle-events/on.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ beforeAll(async () => {
rest.post(httpServer.http.makeUrl('/no-response'), () => {
return
}),
rest.get(httpServer.http.makeUrl('/unhandled-exception'), () => {
throw new Error('Unhandled resolver error')
}),
)
server.listen()

Expand Down Expand Up @@ -62,6 +65,12 @@ beforeAll(async () => {
listener(`[response:bypass] ${res.body} ${requestId}`)
})

server.events.on('unhandledException', (error, req) => {
listener(
`[unhandledException] ${req.method} ${req.url.href} ${req.id} ${error.message}`,
)
})

// Supress "Expected a mocking resolver function to return a mocked response"
// warnings. Using intentional explicit empty resolver.
jest.spyOn(global.console, 'warn').mockImplementation()
Expand Down Expand Up @@ -156,6 +165,16 @@ test('emits events for an unhandled request', async () => {
)
})

test('emits unhandled exceptions in the request handler', async () => {
const url = httpServer.http.makeUrl('/unhandled-exception')
await fetch(url).catch(() => undefined)
const requestId = getRequestId(listener)

expect(listener).toHaveBeenCalledWith(
`[unhandledException] GET ${url} ${requestId} Unhandled resolver error`,
)
})

test('stops emitting events once the server is stopped', async () => {
server.close()
await fetch(httpServer.http.makeUrl('/user'))
Expand Down
9 changes: 9 additions & 0 deletions test/msw-api/setup-worker/life-cycle-events/on.mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ const worker = setupWorker(
rest.post('/no-response', () => {
return
}),
rest.get('/unhandled-exception', () => {
throw new Error('Unhandled resolver error')
}),
)

worker.events.on('request:start', (req) => {
Expand Down Expand Up @@ -37,6 +40,12 @@ worker.events.on('response:bypass', async (res, requestId) => {
console.warn(`[response:bypass] ${body} ${requestId}`)
})

worker.events.on('unhandledException', (error, req) => {
console.warn(
`[unhandledException] ${req.method} ${req.url.href} ${req.id} ${error.message}`,
)
})

worker.start({
onUnhandledRequest: 'bypass',
})
Expand Down
11 changes: 11 additions & 0 deletions test/msw-api/setup-worker/life-cycle-events/on.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,17 @@ test('emits events for an unhandled request', async () => {
])
})

test('emits unhandled exceptions in the request handler', async () => {
const runtime = await createRuntime()
const url = runtime.makeUrl('/unhandled-exception')
await runtime.request(url)
const requestId = getRequestId(runtime.consoleSpy)

expect(runtime.consoleSpy.get('warning')).toContain(
`[unhandledException] GET ${url} ${requestId} Unhandled resolver error`,
)
})

test('stops emitting events once the worker is stopped', async () => {
const runtime = await createRuntime()

Expand Down