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

How can I handle global errors like express? #184

Closed
TheVegeta opened this issue Aug 17, 2022 · 6 comments
Closed

How can I handle global errors like express? #184

TheVegeta opened this issue Aug 17, 2022 · 6 comments

Comments

@TheVegeta
Copy link

I want to handle global errors like express, but I can not find any way to handle global errors in the current version of polka.

// example in express 
app.use((err, req, res, next) => {
  console.error(err.stack)
  res.status(500).send('Something broke!')
})

All I can find is this thread, but it is not supported in recent versions of polka since we can not create a custom server with http.

#12 (comment)

Is it possible to handle global errors like express?

@lukeed
Copy link
Owner

lukeed commented Aug 17, 2022

options.onError

and you can still use custom server too, see options.server

@lukeed lukeed closed this as not planned Won't fix, can't repro, duplicate, stale Aug 17, 2022
@TheVegeta
Copy link
Author

Hello, thanks for your reply.

The main issue here is that it is catching my errors but the server is crashing and I can not find any way to prevent server crashes.

I also attempted to run this code, but it still generates errors and crashes the server.
#12 (comment)

If you are wondering about my code, it is here but I can not hit the main route and it will always give me an error hanling route in response.
https://github.com/TheVegeta/esbuild-polka-starter/blob/main/src/index.js

If I am doing something wrong, please correct me.

@lukeed
Copy link
Owner

lukeed commented Aug 17, 2022

  1. use polka@next
  2. youre doing app.handler = handleErr, which is NOT the same thing as the comment from Why no throwing from middleware? #12 – because here is your handleErr code:
export const handleErr = (req, res, err) => {
  res.statusCode = 500;
  res.setHeader("content-type", "application/json").end(
    jsonStringify({
      success: false,
      msg: "something went wrong on server",
      data: {},
    })
  );
};

This means every route is going to reply with a 500 "something went wrong on server" response because your handleErr isn't wrapping original route handlers... it's replacing them with a forced 500 error.

@TheVegeta
Copy link
Author

Unfortunately, I can not figure out how to implement this on my own, so it would be great if you could add an example of how to handle errors and send error responses with custom json.

If you could add an example, it would be much help.

@lukeed
Copy link
Owner

lukeed commented Aug 17, 2022

12 is an example. You copied it incorrectly

@TheVegeta
Copy link
Author

Thank you for pointing it out. And for your reply, I figured it out and implemented it in the code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants