diff --git a/lib/error.ts b/lib/error.ts index e5b6beab..3951830e 100644 --- a/lib/error.ts +++ b/lib/error.ts @@ -2,19 +2,25 @@ import APIErrorOptions from './interfaces/APIErrorOptions'; export default class APIError extends Error { status: number | string; - stack: string; details: string; constructor({ status, statusText, message, - body = {} + body = {}, }: APIErrorOptions) { const { message: bodyMessage, error } = body; super(); - this.stack = ''; + // Maintains proper stack trace for where our error was thrown (only available on V8) + if (Error.captureStackTrace) { + Error.captureStackTrace(this, APIError); + } else { + this.stack = ''; + } + + this.name = 'APIError'; this.status = status; this.message = message || error || statusText; this.details = bodyMessage; diff --git a/test/error.test.ts b/test/error.test.ts index b9aadb2a..7c5a9ed9 100644 --- a/test/error.test.ts +++ b/test/error.test.ts @@ -29,4 +29,14 @@ describe('APIError', function () { error.message.should.eql('oops. something went wrong'); }); + + it('is an api error', function () { + error = new APIError({ + body: { + error: 'oops. something went wrong' + } + } as APIErrorOptions); + + error.name.should.eql('APIError'); + }); });