Skip to content

Commit

Permalink
Let errors provide their own status.
Browse files Browse the repository at this point in the history
When calling `ctx.throw`, you're allowed to provide an error object and a
status code. The status code is later set as the `status` property of the error
object. If no status code is provided, it defaults to 500. However, this
happens even if the error object already had a `status` property.

This commit allows an error's pre-existing `status` property to be used in
conjunction with `ctx.throw`.

If the status code is below 500, the error message will be exposed to the user
in the HTTP response. It would be nice to have some Error subclasses that
always have the same status code, because then we could just write
`ctx.throw(new WhateverError())`, and define which 4xx error code we want in
the definition of `WhateverError` itself. If, for example, an
`AuthenticationError` is always meant to go along with a 401, then it would be
nice to just have that knowledge in the class definition.
  • Loading branch information
New Now Nohow committed Mar 7, 2014
1 parent c96459b commit 0a223f2
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion lib/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ var proto = module.exports = {
}

var err = msg instanceof Error ? msg : new Error(msg);
err.status = status || 500;
err.status = status || err.status || 500;
err.expose = err.status < 500;
throw err;
},
Expand Down

0 comments on commit 0a223f2

Please sign in to comment.