Skip to content
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

Catch errors from the underlying http module #2164

Merged
merged 2 commits into from Apr 12, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 7 additions & 3 deletions .travis.yml
@@ -1,15 +1,19 @@
language: node_js

node_js:
- node
- io.js
- 0.12
- 0.10
sudo: false

after_script: "npm run test-cov && cat ./coverage/lcov.info | codecov && cat ./coverage/lcov.info | coveralls"
after_script:
- npm run test-cov
- cat ./coverage/lcov.info | codecov
- cat ./coverage/lcov.info | coveralls

webhooks:
urls: https://webhooks.gitter.im/e/237280ed4796c19cc626
on_success: change # options: [always|never|change] default: always
on_failure: always # options: [always|never|change] default: always
on_start: false # default: false

sudo: false
11 changes: 9 additions & 2 deletions request.js
Expand Up @@ -749,7 +749,12 @@ Request.prototype.start = function () {

debug('make request', self.uri.href)

self.req = self.httpModule.request(reqOptions)
try {
self.req = self.httpModule.request(reqOptions)
} catch (err) {
self.emit('error', err)
return
}

if (self.timing) {
self.startTime = new Date().getTime()
Expand Down Expand Up @@ -1389,7 +1394,9 @@ Request.prototype.end = function (chunk) {
if (!self._started) {
self.start()
}
self.req.end()
if (self.req) {
self.req.end()
}
}
Request.prototype.pause = function () {
var self = this
Expand Down
15 changes: 15 additions & 0 deletions tests/test-headers.js
Expand Up @@ -162,6 +162,21 @@ tape('undefined headers', function(t) {
})
})

tape('catch invalid characters error', function(t) {
request({
url: s.url + '/headers.json',
headers: {
'test': 'אבגד'
}
}, function(err, res, body) {
t.equal(err.message, 'The header content contains invalid characters')
})
.on('error', function (err) {
t.equal(err.message, 'The header content contains invalid characters')
t.end()
})
})

tape('cleanup', function(t) {
s.close(function() {
t.end()
Expand Down