JSPM

enrich-api-error

1.0.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 51
  • Score
    100M100P100Q63053F
  • License MIT

Add an API's HTTP response to an error's stack trace for debugging.

Package Exports

  • enrich-api-error

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (enrich-api-error) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

enrich-api-error

Add an API's HTTP response to an error's stack trace for debugging.

npm version Build Status Coverage Status License

Developed at the Media Engineering Institute (HEIG-VD).

Usage

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)