Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Implementing Error.name for thrown errors #463

Closed
wants to merge 3 commits into from

3 participants

@tylerconover

Hi, is this what you had in mind?

Anything I can do to help, just let me know.

#451

main.js
@@ -310,7 +315,7 @@ Request.prototype.init = function (options) {
if(!self.headers['content-length'] && !self.headers['Content-Length'])
self.headers['content-length'] = length
} else {
- throw new Error('Argument error, options.body.')
+ throw{name:'ArgumentError', message:'Argument error, options.body.'}
@mikeal Owner
mikeal added a note

this will throw an object rather than and instance of Error, which won't capture the stack trace.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
main.js
@@ -351,7 +360,7 @@ Request.prototype.init = function (options) {
}
self.once('pipe', function (src) {
- if (self.ntick && self._started) throw new Error("You cannot pipe to this stream after the outbound request has started.")
+ if (self.ntick && self._started) throw{name:'PipeError', message:'You cannot pipe to this stream after the outbound request has started.'}
@mikeal Owner
mikeal added a note

same as above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
main.js
@@ -879,7 +888,7 @@ Request.prototype.multipart = function (multipart) {
self.headers['content-type'] = self.headers['content-type'].split(';')[0] + '; boundary=' + self.boundary
}
- if (!multipart.forEach) throw new Error('Argument error, options.multipart.')
+ if (!multipart.forEach) throw{name:'ArgumentError', message:'Argument error, options.multipart.'}
@mikeal Owner
mikeal added a note

same as above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
main.js
@@ -887,7 +896,7 @@ Request.prototype.multipart = function (multipart) {
multipart.forEach(function (part) {
var body = part.body
- if(body == null) throw Error('Body attribute missing in multipart.')
+ if(body == null) throw{name:'ArgumentError', message:'Body attribute missing in multipart.'}
@mikeal Owner
mikeal added a note

same as above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@mikeal
Owner

this doesn't merge cleanly.

added some line comments.

@tylerconover

Sorry about that, I see the stack trace shows all of the lines that were called, instead of the one. I'll update it.

@tylerconover

Okay, it provides a stack trace. Lets try again :) Thanks.

@ejeklint

You forgot throw error after line 899. :)

@mikeal
Owner

no longer merges cleanly :(

@mikeal mikeal closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 5, 2013
  1. Implement Error.name for thrown errors

    Tyler Conover authored
  2. Updates for Implementing Error.name

    Tyler Conover authored
Commits on Mar 18, 2013
  1. Fix missing throw error on 900

    Tyler Conover authored
This page is out of date. Refresh to see the latest.
Showing with 56 additions and 15 deletions.
  1. +54 −15 main.js
  2. +2 −0  tunnel.js
View
69 main.js
@@ -139,7 +139,9 @@ Request.prototype.init = function (options) {
if (!self.uri) {
// this will throw if unhandled but is handleable when in a redirect
- return self.emit('error', new Error("options.uri is a required argument"))
+ var error = new Error('options.uri is a required argument')
+ error.name = 'ArgumentError'
+ return self.emit('error', error)
} else {
if (typeof self.uri == "string") self.uri = url.parse(self.uri)
}
@@ -168,14 +170,15 @@ Request.prototype.init = function (options) {
// Invalid URI: it may generate lot of bad errors, like "TypeError: Cannot call method 'indexOf' of undefined" in CookieJar
// Detect and reject it as soon as possible
var faultyUri = url.format(self.uri)
- var message = 'Invalid URI "' + faultyUri + '"'
+ var error = new Error('Invalid URI "' + faultyUri + '"')
if (Object.keys(options).length === 0) {
// No option ? This can be the sign of a redirect
// As this is a case where the user cannot do anything (he didn't call request directly with this URL)
// he should be warned that it can be caused by a redirection (can save some hair)
- message += '. This can be caused by a crappy redirection.'
+ error.message += '. This can be caused by a crappy redirection.'
}
- self.emit('error', new Error(message))
+ error.name = 'URIError'
+ self.emit('error', error)
return // This error was fatal
}
@@ -229,10 +232,12 @@ Request.prototype.init = function (options) {
clearTimeout(self.timeoutTimer)
self.timeoutTimer = null
}
+ error.name = 'ClientError'
self.emit('error', error)
}
self._parserErrorHandler = function (error) {
+ error.name = 'ParseError'
if (this.res) {
if (this.res.request) {
this.res.request.emit('error', error)
@@ -310,7 +315,9 @@ Request.prototype.init = function (options) {
if(!self.headers['content-length'] && !self.headers['Content-Length'])
self.headers['content-length'] = length
} else {
- throw new Error('Argument error, options.body.')
+ var error = new Error('Argument error, options.body.')
+ error.name = 'ArgumentError'
+ throw error
}
}
@@ -320,7 +327,11 @@ Request.prototype.init = function (options) {
;
self.httpModule = httpModules[protocol] || defaultModules[protocol]
- if (!self.httpModule) return this.emit('error', new Error("Invalid protocol"))
+ if (!self.httpModule) {
+ var error = new Error('Invalid protocol')
+ error.name = 'URIError'
+ return this.emit('error', error)
+ }
if (options.ca) self.ca = options.ca
@@ -351,7 +362,11 @@ Request.prototype.init = function (options) {
}
self.once('pipe', function (src) {
- if (self.ntick && self._started) throw new Error("You cannot pipe to this stream after the outbound request has started.")
+ if (self.ntick && self._started) {
+ var error = new Error('You cannot pipe to this stream after the outbound request has started.')
+ error.name = 'PipeError'
+ throw error
+ }
self.src = src
if (isReadStream(src)) {
if (!self.headers['content-type'] && !self.headers['Content-Type'])
@@ -879,7 +894,11 @@ Request.prototype.multipart = function (multipart) {
self.headers['content-type'] = self.headers['content-type'].split(';')[0] + '; boundary=' + self.boundary
}
- if (!multipart.forEach) throw new Error('Argument error, options.multipart.')
+ if (!multipart.forEach) {
+ var error = new Error('Argument error, options.multipart.')
+ error.name = 'ArgumentError'
+ throw error
+ }
if (self.preambleCRLF) {
self.body.push(new Buffer('\r\n'))
@@ -887,7 +906,11 @@ Request.prototype.multipart = function (multipart) {
multipart.forEach(function (part) {
var body = part.body
- if(body == null) throw Error('Body attribute missing in multipart.')
+ if(body == null) {
+ var error = new Error('Body attribute missing in multipart.')
+ error.name = 'ArgumentError'
+ throw error
+ }
delete part.body
var preamble = '--' + self.boundary + '\r\n'
Object.keys(part).forEach(function (key) {
@@ -926,7 +949,9 @@ function getHeader(name, headers) {
}
Request.prototype.auth = function (user, pass, sendImmediately) {
if (typeof user !== 'string' || typeof pass !== 'string') {
- throw new Error('auth() received invalid user or password')
+ var error = new Error('auth() received invalid user or password')
+ error.name = 'ArgumentError'
+ throw error
}
this._user = user
this._pass = pass
@@ -1053,9 +1078,13 @@ Request.prototype.jar = function (jar) {
Request.prototype.pipe = function (dest, opts) {
if (this.response) {
if (this._destdata) {
- throw new Error("You cannot pipe after data has been emitted from the response.")
+ var error = new Error('You cannot pipe after data has been emitted from the response.')
+ error.name = 'PipeError'
+ throw error
} else if (this._ended) {
- throw new Error("You cannot pipe after the response has been ended.")
+ var error = new Error('You cannot pipe after the response has been ended.')
+ error.name = 'PipeError'
+ throw error
} else {
stream.Stream.prototype.pipe.call(this, dest, opts)
this.pipeDest(dest)
@@ -1103,7 +1132,11 @@ function initParams(uri, options, callback) {
}
function request (uri, options, callback) {
- if (typeof uri === 'undefined') throw new Error('undefined is not a valid uri or options object.')
+ if (typeof uri === 'undefined') {
+ var error = new Error('undefined is not a valid uri or options object.')
+ error.name = 'URIError'
+ throw error
+ }
if ((typeof options === 'function') && !callback) callback = options
if (options && typeof options === 'object') {
options.uri = uri
@@ -1189,7 +1222,9 @@ request.head = function (uri, options, callback) {
params.options.requestBodyStream ||
(params.options.json && typeof params.options.json !== 'boolean') ||
params.options.multipart) {
- throw new Error("HTTP HEAD requests MUST NOT include a request body.")
+ var error = new Error('HTTP HEAD requests MUST NOT include a request body.')
+ error.name = 'ArgumentError'
+ throw error
}
return request(params.uri || null, params.options, params.callback)
}
@@ -1206,7 +1241,11 @@ request.jar = function () {
}
request.cookie = function (str) {
if (str && str.uri) str = str.uri
- if (typeof str !== 'string') throw new Error("The cookie function only accepts STRING as param")
+ if (typeof str !== 'string') {
+ var error = new Error('The cookie function only accepts STRING as param')
+ error.name = 'ArgumentError'
+ throw error
+ }
return new Cookie(str)
}
View
2  tunnel.js
@@ -147,6 +147,7 @@ TunnelingAgent.prototype.createSocket = function createSocket(options, cb) {
debug('tunneling socket could not be established, statusCode=%d', res.statusCode)
var error = new Error('tunneling socket could not be established, ' + 'statusCode=' + res.statusCode)
error.code = 'ECONNRESET'
+ error.name = 'ConnectionError'
options.request.emit('error', error)
self.removeSocket(placeholder)
}
@@ -158,6 +159,7 @@ TunnelingAgent.prototype.createSocket = function createSocket(options, cb) {
debug('tunneling socket could not be established, cause=%s\n', cause.message, cause.stack)
var error = new Error('tunneling socket could not be established, ' + 'cause=' + cause.message)
error.code = 'ECONNRESET'
+ error.name = 'ConnectionError'
options.request.emit('error', error)
self.removeSocket(placeholder)
}
Something went wrong with that request. Please try again.