an HTTP 500 or 404 is not an error regarding HTTP so there is no need to
emit an error on that. the http request havent caused an error. a HTTP 500
(0r 4XX) indicates an error-state on the application level, that is
abstract to the http layer.
so request emits the 'response' event, here you can intercept the http
non-ok states:
```
var request = require('request')
var req = request(process.argv[2])
.on('error', function (er) {
console.log('error')
console.log(er)
})
.on('end', function () {
console.log('end')
})
.on('response', function(res){
if(res.statusCode >= 400){
console.error('error: ' + res.statusCode)
req.pipe(process.stderr)
}else{
req.pipe(process.stdout)
}
})
```
Am Dienstag, 28. Mai 2013 21:51:16 UTC+2 schrieb Stephen Bartell:
>
> In general, If the connection to a server is successful, but the server
> sends back an error message and sets the status code to an error state, I
> want this response to be emitted as an `error` event on the stream.
>
> To be more specific, I want a streaming interface to a Couchdb changes
> feed and I want errors from Couchdb to be emitted as 'error'. Right now,
> if I create a stream to a couchdb changes feed and something goes wrong
> inside couchdb , like the database doesnt exist or the authentication is
> incorrect, an error will not be emitted. The response will be emitted as
> data and the status code on the response will be the only indication that
> the request failed.
>
> Heres some code and responses to clarify all this:
>
> Heres the program, index.js.
> ```
> var request = require('request')
>
> var req = request(process.argv[2])
> .on('error', function (er) {
> console.log('error')
> console.log(er)
> })
> .on('end', function () {
> console.log('end')
> })
> .pipe(process.stdout)
> ```
>
> Requesting a changes feed where the couchdb port is wrong. This error
> makes sense.
> ```
> $ node index.js http://localhost:598/test
> error
> { [Error: connect ECONNREFUSED]
> code: 'ECONNREFUSED',
> errno: 'ECONNREFUSED',
> syscall: 'connect' }
> ```
>
> Requesting a changes feed where an error occurs in couchdb. The path to
> couchdb is correct, but the database does not exist. The request ends
> "successfully".
> ```
> $ node index.js http://localhost:5984/tes
> {"error":"not_found","reason":"no_db_file"}
> end
> ```
>
> What I want is something like this:
> ```
> $ node index.js http://localhost:5984/tes
> error
> { [Error: not_found],
> reason: "no_db_file" }
> ```
>
>
>
--
--
Job Board: http://jobs.nodejs.org/
Posting guidelines:
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en
---
You received this message because you are subscribed to the Google Groups
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.