Git Product home page Git Product logo

Comments (9)

juliangruber avatar juliangruber commented on March 29, 2024
if (err) throw err
// and
return this.status = 404

should be enough?

from koa.

jonathanong avatar jonathanong commented on March 29, 2024

i want to delegate it to the error handler though (custom error pages, stack traces, etc.). maybe i just haven't understood the entire koa philosophy yet, though.

from koa.

juliangruber avatar juliangruber commented on March 29, 2024

When you throw an error in a co handler it should be catched by koa and cause an error page to be shown. When you want to set a custom status code that's not really an error.

from koa.

jonathanong avatar jonathanong commented on March 29, 2024

so by your definition, errors are only 5xx? 4xx status codes are errors to me too, and both should, by default, be handled by the default error handler. or what do you mean by "custom status code"?

from koa.

juliangruber avatar juliangruber commented on March 29, 2024

there are business logic errors and application errors:

  • A 5xx means that something went wrong in your application, and you need to fix that.
  • A 4xx means that the user did something wrong and you usually don't need to do anything about it (except maybe when because of a dead link).

So they need to be treated differently, e.g. there's no need for a stack trace when encountering a 404.

from koa.

jonathanong avatar jonathanong commented on March 29, 2024

where do you place your 4xx error handler then with you do this.status = 4xx? as a middleware?

and what about when plugins throw a 4xx error, for example a body parsing function throws a 413 Request Entity Too Large? if you don't want the default error handler to catch it, then you might have to do something like

try {
  var body = yield this.parseBody
} catch (err) {
  if (err.status && err.status >= 400 && err.status < 500) 
    return this.status = err.status
  else
    return this.error(err)
}

which is really annoying

from koa.

tj avatar tj commented on March 29, 2024

I have an issue to support err.status, as far as generating the error goes I wouldn't mind this.error(code|statusString), then if you want to special-case just catch up stream

from koa.

tj avatar tj commented on March 29, 2024

actually changed my mind on this for now, since early-return is effectively the same thing. Brings up a bit of a good point however because things like the logger middleware need to try/catch so they always work. Been a while since I've actually been able to use exceptions! haha

from koa.

tj avatar tj commented on March 29, 2024

actually this is still useful

from koa.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.