sapper icon indicating copy to clipboard operation
sapper copied to clipboard

sapper.middleware catches all server errors, would rather pass through

Open briancray opened this issue 6 years ago • 9 comments
trafficstars

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

briancray avatar Nov 18 '19 15:11 briancray

Would be a nice feature!

ghost avatar May 07 '20 17:05 ghost

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.

koenkivits avatar Jun 01 '20 10:06 koenkivits

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.

soullivaneuh avatar Nov 12 '20 17:11 soullivaneuh

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.

https://github.com/bamadesigner/sapper/pull/1/files

bamadesigner avatar May 16 '21 17:05 bamadesigner

@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 avatar May 21 '21 19:05 vish01

@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 avatar May 25 '21 23:05 bamadesigner

@bamadesigner Just tried that, for any server error, I get 200 as statusCode in the response. image

vish01 avatar May 26 '21 13:05 vish01

@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 avatar May 26 '21 13:05 bamadesigner

@bamadesigner I modified sapper code the same way you did. I had to restart the server, but now it works. Thanks for your help! :)

vish01 avatar May 26 '21 13:05 vish01