-
Notifications
You must be signed in to change notification settings - Fork 3
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
Standard error interface #35
Comments
I've recently written an error serializer for our json APIs ( https://gist.github.com/Raynos/91cab673906aebd04eac ) I settled on the interface
The The The The The In production you want to only serialize the The I don't care that much about field names, however you generally want the following information
|
so i really like stripe's error: https://stripe.com/docs/api#errors
|
personally I like .public :) |
How about plain old Something like var err = new Error('blah not found')
err.status = 404
res.end(err) |
Also, as for deciding if the message should be written to the response or not, there is an upcoming |
Imho, it should be an extension of an So here is what I came up with so far: var Error = require('./error')
throw Error('message').http(404)
/*
Error 404: message
at new Error (/tmp/error.js:7:16)
at Error (/tmp/error.js:5:40)
at Object.<anonymous> (/tmp/index.js:6:7)
at Module._compile (module.js:456:26)
*/
// with a possibility of future extensions like:
// throw Error('message').fs('ENOENT') Implementation: module.exports = Error
function Error(message, a, b) {
var err = new global.Error(message, a, b)
err.__proto__ = Error.prototype
return err
}
require('util').inherits(global.Error, Error)
Error.prototype.http = function(status) {
this.status = status
return this
}
Error.prototype.toString = function() {
var ret = 'Error'
if (Number(this.status) >= 200 && Number(this.status) < 600) {
ret += ' ' + this.status
}
ret += ': ' + this.message
return ret
} |
If the resulting object is |
@dougwilson , I need a quick sanity check here: https://gist.github.com/rlidwka/0ad094386f60f93318bf I was looking for a way to subclass Errors properly, but it looks like I just found a better way to subclass everything. |
And here's proposed module: https://github.com/rlidwka/error, let me know if you like the approach. No idea if it'll be useful, but I definitely going to switch to it from my custom |
I have a similar helper for creating errors ( https://github.com/Raynos/error/blob/master/typed.js ). This is actually the |
Both of those methods work :) I wonder which is faster, though. I would think |
It's full-blown example, and you don't really need to use function Class() {
var self = Object.create(Class.prototype)
self.foo = 1
return self
} Benchmarks are surprising though. It's 30% faster on node 0.10 and 11% slower on node 0.11. :D |
should i push for |
god i can't find an appropriate name |
Would be cool if these modules and any related ones have a common error syntax. Would be cool to have
err.code
matching node or something as well as.status
if applicableThe text was updated successfully, but these errors were encountered: