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

surface request error message, fixes #91 #107

Closed
wants to merge 5 commits into from
Closed
Changes from 2 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
12 changes: 6 additions & 6 deletions lib/eventsource.js
Expand Up @@ -34,10 +34,10 @@ function EventSource (url, eventSourceInitDict) {
var self = this
self.reconnectInterval = 1000

function onConnectionClosed () {
function onConnectionClosed (message) {
if (readyState === EventSource.CLOSED) return
readyState = EventSource.CONNECTING
_emit('error', new Event('error'))
_emit('error', new Event('error', {message}))

// The url may have been changed by a temporary
// redirect. If that's the case, revert it now.
Expand Down Expand Up @@ -121,7 +121,7 @@ function EventSource (url, eventSourceInitDict) {
req = (isSecure ? https : http).request(options, function (res) {
// Handle HTTP errors
if (res.statusCode === 500 || res.statusCode === 502 || res.statusCode === 503 || res.statusCode === 504) {
_emit('error', new Event('error', {status: res.statusCode}))
_emit('error', new Event('error', {status: res.statusCode, message: res.statusMessage}))
onConnectionClosed()
return
}
Expand All @@ -130,7 +130,7 @@ function EventSource (url, eventSourceInitDict) {
if (res.statusCode === 301 || res.statusCode === 307) {
if (!res.headers.location) {
// Server sent redirect response without Location header.
_emit('error', new Event('error', {status: res.statusCode}))
_emit('error', new Event('error', {status: res.statusCode, message: res.statusMessage}))
return
}
if (res.statusCode === 307) reconnectUrl = url
Expand All @@ -140,7 +140,7 @@ function EventSource (url, eventSourceInitDict) {
}

if (res.statusCode !== 200) {
_emit('error', new Event('error', {status: res.statusCode}))
_emit('error', new Event('error', {status: res.statusCode, message: res.statusMessage}))
return self.close()
}

Expand Down Expand Up @@ -210,7 +210,7 @@ function EventSource (url, eventSourceInitDict) {
})
})

req.on('error', onConnectionClosed)
req.on('error', err => onConnectionClosed(err.message))
if (req.setNoDelay) req.setNoDelay(true)
req.end()
}
Expand Down