Comments (9)
if (err) throw err
// and
return this.status = 404
should be enough?
from koa.
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.
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.
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.
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.
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.
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.
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.
actually this is still useful
from koa.
Related Issues (20)
- [fix] ctx.request.origin ignores proxy flag (X-Forwarded-Proto) HOT 2
- Issue with reassigning ctx.query and type preservation HOT 2
- [feat] send json type content when call ctx.throw HOT 1
- [fix] Has callback() changed return type? Now error from eslint HOT 5
- [feat] Could a listen() shortcut function be added also for http2? HOT 3
- [fix] Some things cannot be found in the database where condition search, unless the where condition search ID HOT 3
- koa 2.14.7 No generics,I want to be able to add generics HOT 1
- [feat] when will koa 3.0 be released? HOT 3
- Koa main website example code snippets disappear HOT 4
- [feat] Objects as middleware HOT 6
- [feat] Support Web (WHATWG) stream, Blob and Response HOT 2
- Consider alternatives to http-assert (including none at all) HOT 4
- [fix] Send response as buffer HOT 4
- async issue on app.listen(...) HOT 4
- [fix] I can not get query parameters about Korean HOT 3
- [feat] Spanish translation for docs
- Routing sucks, moving back to express. HOT 1
- [fix] ctx value persists to next request after mutating ctx object HOT 3
- if "\@" in url ,the redirect result doesn't match the host of the new URL() HOT 2
- [feat] Replace npm dependency "only"
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from koa.