https://github.com/mediacomem/enrich-api-error
Add an API's HTTP response to an error's stack trace for debugging.
https://github.com/mediacomem/enrich-api-error
Last synced: about 1 year ago
JSON representation
Add an API's HTTP response to an error's stack trace for debugging.
- Host: GitHub
- URL: https://github.com/mediacomem/enrich-api-error
- Owner: MediaComem
- License: mit
- Created: 2017-11-28T10:02:15.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-11-30T08:32:37.000Z (over 8 years ago)
- Last Synced: 2025-03-17T18:35:51.852Z (over 1 year ago)
- Language: JavaScript
- Size: 59.6 KB
- Stars: 0
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# enrich-api-error
Add an API's HTTP response to an error's stack trace for debugging.
[](https://badge.fury.io/js/enrich-api-error)
[](https://gemnasium.com/github.com/MediaComem/enrich-api-error)
[](https://travis-ci.org/MediaComem/enrich-api-error)
[](https://coveralls.io/github/MediaComem/enrich-api-error?branch=master)
[](LICENSE.txt)
Developed at the [Media Engineering Institute](http://mei.heig-vd.ch) ([HEIG-VD](https://heig-vd.ch)).
## Usage
This is a low-level utility developed as a dependency to [mocha-api-errors](https://github.com/MediaComem/mocha-api-errors).
It's not meant to be used directly, but here's an example using [supertest](https://github.com/visionmedia/supertest) in a [Mocha](https://mochajs.org) test:
```js
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. (spec/index.spec.js:20:21)
// at
// 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.)