Open briancray opened 5 years ago
Would be a nice feature!
This feels like a must-have. Commenting on this issue to link #812, which is also about error handling and also contains a PR.
Either solution would work for me; I just want (Sentry) error logging to work.
Is there any alternative to catch Sentry errors?
Trying to use the sentry error handler before the middleware:
import sirv from 'sirv';
import polka from 'polka';
import compression from 'compression';
// eslint-disable-next-line import/no-unresolved
import * as sapper from '@sapper/server';
import * as Sentry from "@sentry/node";
if (process.env.SENTRY_DSN) {
console.log('sentry init', process.env.SENTRY_DSN);
Sentry.init({
dsn: process.env.SENTRY_DSN,
});
}
const { PORT, NODE_ENV } = process.env;
const dev = NODE_ENV === 'development';
polka() // You can also use Express
.use(
Sentry.Handlers.requestHandler(),
compression({ threshold: 0 }),
sirv('static', { dev }),
Sentry.Handlers.errorHandler(),
sapper.middleware(),
)
.listen(PORT, (err) => {
// eslint-disable-next-line no-console
if (err) console.log('error', err);
});
But this to anonymous weird errors.
If no workaround is available, then yes, the feature is quite a must have.
I came up with a simple fix for anyone who is interested/needs something to hold you over until SvelteKit.
I added the status code to the response sent during handle_page()
, so when its passed back through middleware you can check response.statusCode inside the middleware function, e.g. if (500 === response.statusCode)
equals true when a 500 error has been thrown.
I decided to fork sapper for myself since they might not do any more work here anyway. This is the only change I've made to my fork. Feel free to copy.
@bamadesigner How can I check responseCode inside sapper.middleware() function? Can you show me an example? And this can go within server.js, correct?
@vish01 It goes inside the sapper middleware, as per these docs: https://sapper.svelte.dev/docs#Seeding_session_data
Here is a modification of their example:
// src/server.js
polka()
.use(
// ...
sapper.middleware({
// customize the session
session: ((req, res) => {
// My sapper fork makes sure this statusCode is defined.
if (500 === response.statusCode) {
return undefined;
}
return {
user: req.user
}
})
})
)
@bamadesigner Just tried that, for any server error, I get 200 as statusCode in the response.
@vish01 And you're using my sapper fork? or you modified sapper core in the same way I modified my fork? https://github.com/bamadesigner/sapper/pull/1/files
@bamadesigner I modified sapper code the same way you did. I had to restart the server, but now it works. Thanks for your help! :)
Is your feature request related to a problem? Please describe. I have a error handler in my express app that sends server errors to Sentry and does some custom error handling. Sapper's error handler catches all server errors and doesn't pass them through to my default.
Describe the solution you'd like To pass through sapper server route errors to pass through to my error handling middleware
Describe alternatives you've considered None
How important is this feature to you? Error handling is muy importante
Additional context No extra context