Add an API's HTTP response to an error's stack trace for debugging.
Developed at the Media Engineering Institute (HEIG-VD).
This is a low-level utility developed as a dependency to mocha-api-errors.
It's not meant to be used directly, but here's an example using supertest in a Mocha test:
const enrichApiError = require('enrich-api-error');
const supertest = require('supertest');
const app = require('./my-express-app');
describe('enrich-api-error', () => {
it('should add the HTTP response to a stack trace', async () => {
const res = await supertest(app).get('/test');
if (res.status != 200) {
throw enrichApiError(new Error(`Expected status code to be 200, got ${res.status}`), res);
}
});
});
// This is an example of the output you could get with a mocha test.
// Note the HTTP response description that has been inserted before
// the error's stack trace:
//
// something
// 1) should work
//
// 0 passing (50ms)
// 1 failing
//
// 1) something
// should work:
//
// Error: Expected status code to be 200, got 400
//
// HTTP/1.1 400 Bad Request
// x-powered-by: Express
// content-type: application/json; charset=utf-8
// content-length: 13
// etag: W/"d-pedE0BZFQNM7HX6mFsKPL6l+dUo"
// date: Tue, 28 Nov 2017 08:58:02 GMT
// connection: close
//
// {
// "foo": "bar"
// }
//
// at Context.<anonymous> (spec/index.spec.js:20:21)
// at <anonymous>
// at process._tickCallback (internal/process/next_tick.js:188:7)
enrich-api-error expects the response body to be available as the body
or
text
property of the response object. (This is usually provided by Node.js
HTTP libraries such as request or supertest.)