diff --git a/index.js b/index.js index 64d53cf..df44168 100644 --- a/index.js +++ b/index.js @@ -89,7 +89,13 @@ function mock(superagent) { try { var response = current(request); if (response.status !== 200) { - cb && cb(response, null); + // superagent puts status and response on the error it returns, + // which should be an actual instance of Error + // See http://visionmedia.github.io/superagent/#error-handling + var error = new Error(response.status); + error.status = response.status; + error.response = response; + cb && cb(error, null); } else { cb && cb(null, response); } diff --git a/test.js b/test.js index cdb4727..4c7f27f 100644 --- a/test.js +++ b/test.js @@ -208,6 +208,8 @@ describe('superagent mock', function() { request.get('/topics/1') .end(function(err, data) { err.should.have.property('status', 500); + err.should.have.property('response'); + should.deepEqual(err.response, {body: {}, status: 500}); done(); }); });