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: treat unhandled exceptions in handlers as 500 error responses #2135

Merged
merged 10 commits into from
May 8, 2024
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -137,7 +137,7 @@
"@bundled-es-modules/statuses": "^1.0.1",
"@inquirer/confirm": "^3.0.0",
"@mswjs/cookies": "^1.1.0",
"@mswjs/interceptors": "^0.26.14",
"@mswjs/interceptors": "^0.29.0",
"@open-draft/until": "^2.1.0",
"@types/cookie": "^0.6.0",
"@types/statuses": "^2.0.4",
Expand Down
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

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

4 changes: 4 additions & 0 deletions src/core/utils/internal/devUtils.ts
Expand Up @@ -29,3 +29,7 @@ export const devUtils = {
warn,
error,
}

export class InternalError extends Error {
//
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay now, this is embarrassing.

}
6 changes: 3 additions & 3 deletions src/core/utils/request/onUnhandledRequest.ts
@@ -1,5 +1,5 @@
import { toPublicUrl } from './toPublicUrl'
import { devUtils } from '../internal/devUtils'
import { InternalError, devUtils } from '../internal/devUtils'

export interface UnhandledRequestPrint {
warning(): void
Expand Down Expand Up @@ -33,7 +33,7 @@ export async function onUnhandledRequest(
devUtils.error('Error: %s', unhandledRequestMessage)

// Throw an exception to halt request processing and not perform the original request.
throw new Error(
throw new InternalError(
devUtils.formatMessage(
'Cannot bypass a request when using the "error" strategy for the "onUnhandledRequest" option.',
),
Expand All @@ -49,7 +49,7 @@ export async function onUnhandledRequest(
break

default:
throw new Error(
throw new InternalError(
devUtils.formatMessage(
'Failed to react to an unhandled request: unknown strategy "%s". Please provide one of the supported strategies ("bypass", "warn", "error") or a custom callback function as the value of the "onUnhandledRequest" option.',
strategy,
Expand Down
8 changes: 7 additions & 1 deletion src/node/SetupServerCommonApi.ts
Expand Up @@ -15,7 +15,7 @@ import { SetupApi } from '~/core/SetupApi'
import { handleRequest } from '~/core/utils/handleRequest'
import type { RequestHandler } from '~/core/handlers/RequestHandler'
import { mergeRight } from '~/core/utils/internal/mergeRight'
import { devUtils } from '~/core/utils/internal/devUtils'
import { InternalError, devUtils } from '~/core/utils/internal/devUtils'
import type { SetupServerCommon } from './glossary'

export const DEFAULT_LISTEN_OPTIONS: RequiredDeep<SharedOptions> = {
Expand Down Expand Up @@ -68,6 +68,12 @@ export class SetupServerCommonApi
return
})

this.interceptor.on('unhandledException', ({ error }) => {
if (error instanceof InternalError) {
throw error
}
})

this.interceptor.on(
'response',
({ response, isMockedResponse, request, requestId }) => {
Expand Down