Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
Expose http parse error codes
Browse files Browse the repository at this point in the history
Currently http parse errors do not expose the error details available
from http_parser. This patch exposes the error code as `err.code`.
  • Loading branch information
felixge authored and isaacs committed Mar 20, 2012
1 parent 891f9de commit 1824019
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/node_http_parser.cc
Expand Up @@ -432,9 +432,12 @@ class Parser : public ObjectWrap {
// If there was a parse error in one of the callbacks
// TODO What if there is an error on EOF?
if (!parser->parser_.upgrade && nparsed != len) {
enum http_errno err = HTTP_PARSER_ERRNO(&parser->parser_);

Local<Value> e = Exception::Error(String::NewSymbol("Parse Error"));
Local<Object> obj = e->ToObject();
obj->Set(String::NewSymbol("bytesParsed"), nparsed_obj);
obj->Set(String::NewSymbol("code"), String::New(http_errno_name(err)));
return scope.Close(e);
} else {
return scope.Close(nparsed_obj);
Expand All @@ -455,9 +458,12 @@ class Parser : public ObjectWrap {
if (parser->got_exception_) return Local<Value>();

if (rv != 0) {
enum http_errno err = HTTP_PARSER_ERRNO(&parser->parser_);

Local<Value> e = Exception::Error(String::NewSymbol("Parse Error"));
Local<Object> obj = e->ToObject();
obj->Set(String::NewSymbol("bytesParsed"), Integer::New(0));
obj->Set(String::NewSymbol("code"), String::New(http_errno_name(err)));
return scope.Close(e);
}

Expand Down
1 change: 1 addition & 0 deletions test/simple/test-http-client-parse-error.js
Expand Up @@ -48,6 +48,7 @@ srv.listen(common.PORT, '127.0.0.1', function() {
console.log('got error from client');
srv.close();
assert.ok(e.message.indexOf('Parse Error') >= 0);
assert.equal(e.code, 'HPE_INVALID_CONSTANT');
parseError = true;
});
});
Expand Down

0 comments on commit 1824019

Please sign in to comment.