GeezerVison is showing github.com — `setupKoaErrorHandler` doesn't preserve the error status code and message in the response · Issue #12517 · getsentry/sentry-javascript
setupKoaErrorHandler doesn't preserve the error status code and message in the response #12517
DescriptionIs there an existing issue for this?
How do you use Sentry?
Sentry Saas (sentry.io)
Which SDK are you using?
@sentry/node
SDK Version
8.9.2
Framework Version
"@sentry/node": "^8.9.2"
Link to Sentry event
No response
SDK Setup
init({
dsn: '___DSN',
tracesSampleRate: 0.1,
profilesSampleRate: 1.0,
normalizeDepth: 11,
integrations: [nodeProfilingIntegration],
environment: EnvVariablesUtility.NODE_ENV,
enabled: EnvVariablesUtility.NODE_ENV !== 'local',
beforeSend(event, hint) {
const error: any = hint.originalException;
if (error && error.expose) {
return null;
}
return event;
},
});
Steps to Reproduce
My middleware:
ctx.status = 201;
ctx.assert(isEmail.default(email), 400, 'The email is invalid');
Expected Result
Status code to be 400 with body text 'The email is invalid'
Actual Result
Status code is 201, body text is 'Created'
However, if I place setupKoaErrorHandler after I have defined all my middlewares, I get
Status code 400, no body Activitychanged the title [-]`setupKoaErrorHandler` doesn't preserve the error status code and message[/-] [+]`setupKoaErrorHandler` doesn't preserve the error status code and message in the response[/+] on Jun 17, 2024AbhiPrasad commented on Jun 17, 2024 Hey @maximedupre! The quick fix here is def to make sure we rethrow errors. Currently we swallow all errors, which makes ctx.assert and similar strategies break 😢.
We also will add some filtering so that only 5xx errors are captured by Sentry, you can opt-in to capturing 3xx and 4xx errors if you want! maximedupre commented on Jun 18, 2024 Thanks for the response @AbhiPrasad.
Currently we swallow all errors, which makes ctx.assert and similar strategies break 😢.
ctx.assert, ctx.throw, or just throwing an error breaks. In other words, it's impossible to respond with an appropriate error response 😅
Just to be clear, there is currently no workaround except removing setupKoaErrorHandler? AbhiPrasad commented on Jun 18, 2024 maximedupre commented on Jun 18, 2024 Not sure what you mean. I already have a custom error handler middleware:
async (ctx: Koa.Context, next: Next) => {
try {
await next();
} catch (e: any) {
if (e.status === 400 || e.status === 401) {
const error: any = createError(e.status, e.message);
error.headers = ctx.response.header;
error.expose = e.expose;
throw error;
} else {
throw e;
}
}
};
Removing it or leaving it place has no effect, the sentry koa error handler doesn't consider the error's status code or message. AbhiPrasad commented on Jun 18, 2024 Meaning you remove setupKoaErrorHandler and add a Sentry.captureException to your error handling middleware yourself.
async (ctx: Koa.Context, next: Next) => {
try {
await next();
} catch (e: any) {
if (e.status === 400 || e.status === 401) {
const error: any = createError(e.status, e.message);
error.headers = ctx.response.header;
error.expose = e.expose;
throw error;
} else {
+ Sentry.captureException(error);
throw e;
}
}
};added a commit that references this issue on Jun 24, 2024This issue will close once commit 41c51e9 is merged into the 'develop' branch.41c51e9
MetadataMetadataStatusShow more project fields
DevelopmentNo branches or pull requestsIssue actions
|