Skip to content

Commit

Permalink
Merge pull request #6 from atlantismonk/master
Browse files Browse the repository at this point in the history
修复原先捕捉不到请求端错误的问题;
  • Loading branch information
calvinxiao authored Nov 30, 2017
2 parents 3609797 + ca68009 commit f9659ff
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
7 changes: 7 additions & 0 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,16 @@ var request = function(options, params, callback) {
buf.push(data);
});
res.on('error', function(err) {
//response error event
callback(err);
});
});

req.on('error', function(err) {
//request error event
callback(err);
});

if (params) {
var stringify;
if (options.headers['Content-Type'] && options.headers['Content-Type'].toLowerCase() == 'application/x-www-form-urlencoded') {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "yhsd-api",
"version": "1.0.5",
"version": "1.0.6",
"description": "Youhaosuda API SDK for node.",
"main": "index.js",
"repository": {
Expand Down
21 changes: 20 additions & 1 deletion test/api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,26 @@ describe('test/api.test.js', function () {
done();
})
});


it('api should be throw ENOTFOUND error', function (done) {
Yhsd.config.apiHost = 'localhost:32876';
Yhsd.config.appHost = 'localhost:32876';
Yhsd.config.httpProtocol = 'http';
api.get('products',function (err, token) {
if (err){
// console.log(err.message);
var eResult = err.message.indexOf('ENOTFOUND');
if (eResult > 0) {
(eResult).should.be.ok();
done();
return;
}
}
console.log(token);
done(new Error('没有捕捉到错误!'));
});
});

//it('api should be return error code 429', function (done) {
// var total = 0;
// function _request() {
Expand Down

0 comments on commit f9659ff

Please sign in to comment.