Browse Source

Expose http parse error codes

Currently http parse errors do not expose the error details available
from http_parser. This patch exposes the error code as `err.code`.
v0.9.1-release
Felix Geisendörfer 13 years ago
committed by isaacs
parent
commit
18240193ba
  1. 6
      src/node_http_parser.cc
  2. 1
      test/simple/test-http-client-parse-error.js

6
src/node_http_parser.cc

@ -432,9 +432,12 @@ public:
// If there was a parse error in one of the callbacks // If there was a parse error in one of the callbacks
// TODO What if there is an error on EOF? // TODO What if there is an error on EOF?
if (!parser->parser_.upgrade && nparsed != len) { 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<Value> e = Exception::Error(String::NewSymbol("Parse Error"));
Local<Object> obj = e->ToObject(); Local<Object> obj = e->ToObject();
obj->Set(String::NewSymbol("bytesParsed"), nparsed_obj); obj->Set(String::NewSymbol("bytesParsed"), nparsed_obj);
obj->Set(String::NewSymbol("code"), String::New(http_errno_name(err)));
return scope.Close(e); return scope.Close(e);
} else { } else {
return scope.Close(nparsed_obj); return scope.Close(nparsed_obj);
@ -455,9 +458,12 @@ public:
if (parser->got_exception_) return Local<Value>(); if (parser->got_exception_) return Local<Value>();
if (rv != 0) { if (rv != 0) {
enum http_errno err = HTTP_PARSER_ERRNO(&parser->parser_);
Local<Value> e = Exception::Error(String::NewSymbol("Parse Error")); Local<Value> e = Exception::Error(String::NewSymbol("Parse Error"));
Local<Object> obj = e->ToObject(); Local<Object> obj = e->ToObject();
obj->Set(String::NewSymbol("bytesParsed"), Integer::New(0)); obj->Set(String::NewSymbol("bytesParsed"), Integer::New(0));
obj->Set(String::NewSymbol("code"), String::New(http_errno_name(err)));
return scope.Close(e); return scope.Close(e);
} }

1
test/simple/test-http-client-parse-error.js

@ -48,6 +48,7 @@ srv.listen(common.PORT, '127.0.0.1', function() {
console.log('got error from client'); console.log('got error from client');
srv.close(); srv.close();
assert.ok(e.message.indexOf('Parse Error') >= 0); assert.ok(e.message.indexOf('Parse Error') >= 0);
assert.equal(e.code, 'HPE_INVALID_CONSTANT');
parseError = true; parseError = true;
}); });
}); });

Loading…
Cancel
Save