question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

HttpError handling

See original GitHub issue

I have a situation where I need to discriminate between a HttpError raised by Koa (or any middleware) as opposed to any other type of error raised by the application - the latter should never occur if everything is correct, however the former is expected.

At the moment, I have a block of code like so:

import createError from "http-errors"
export async function errorHandler(ctx, next) {
  try {
    await next()
  } catch (err) {
    if (err instanceof createError.HttpError)
      // this type of error is expected, should produce minimal logging
    else
      // create an alert for this type of error, since it should never occur in normal operation
  }
}

This works, but I don’t think it’s ideal. The issue is that my project does not depend on the http-errors library directly; only in so far as these errors are raised by Koa when I call the throw or assert functions in a handler. I would prefer if I could handle this case by interacting with some aspect of Koa - if Koa delegates that to http-errors or some other library, I think that should be an implementation detail for Koa rather than meaning I need to introduce a library for this into my code.

So, this leaves me with two questions:

  1. Is this the appropriate way to handle this situation, or is there some other mechanism I should be using?
  2. If this is the correct way to handle this case, can Koa export the errors in use from other libraries?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

6reactions
micheal-hillcommented, Apr 17, 2018

@jonathanong Because I have several different types of errors being created in different places, each contain a status code. I can potentially identify that an error is not type A or B (where these are non-koa errors) by looking for discriminating properties, but this seems like a poor solution when I don’t care about discriminating between those types of error. I specifically care about the errors raised by koa and need to treat them separately.

0reactions
fl0wcommented, Dec 23, 2019
Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular HTTP Error Handling - TekTutorialsHub
Learn Angular HTTP Error Handling. Use HTTP Interceptor to catch the Errors using the catchError operator. Handle error or re throw it using ......
Read more >
How do I catch a specific HTTP error in Python? - Stack Overflow
Just catch HTTPError , handle it, and if it's not Error 404, simply use raise to re-raise the exception. See the Python tutorial....
Read more >
HTTP Error Handling - Advanced Installer
When a user attempts to connect to a Web site and an HTTP error occurs, a message is sent back to the client...
Read more >
urllib.error — Exception classes raised by urllib.request ...
The reason for this error. It can be a message string or another exception instance. Changed in version 3.3: URLError has been made ......
Read more >
HTTP Errors <httpErrors> - Microsoft Learn
The <httpErrors> element contains a collection of <error> elements, each of which defines an error message that IIS uses to respond to ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found