Skip to content

Commit 8d4baa8

Browse files
Merge pull request #347 from mailgun/update-error-class
feature: Add type property to errors
2 parents 8ac1829 + b25f2bf commit 8d4baa8

File tree

5 files changed

+21423
-22
lines changed

5 files changed

+21423
-22
lines changed

dist/error.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ export default class APIError extends Error {
33
status: number | string;
44
stack: string;
55
details: string;
6+
type: string;
67
constructor({ status, statusText, message, body }: APIErrorOptions);
78
}

dist/mailgun.node.js

Lines changed: 13009 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/mailgun.web.js

Lines changed: 8380 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/error.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import APIErrorOptions from './interfaces/APIErrorOptions';
22

33
export default class APIError extends Error {
4-
status: number | string;
5-
stack: string;
6-
details: string;
4+
public status: number | string;
5+
public stack: string;
6+
public details: string;
7+
public type: string;
78

89
constructor({
910
status,
@@ -25,5 +26,6 @@ export default class APIError extends Error {
2526
this.status = status;
2627
this.message = message || error || statusText;
2728
this.details = bodyMessage;
29+
this.type = 'MailgunAPIError';
2830
}
2931
}

test/error.test.ts

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,46 @@ import APIError from '../lib/error';
22
import APIErrorOptions from '../lib/interfaces/APIErrorOptions';
33

44
describe('APIError', function () {
5-
let error;
6-
75
it('sets status', function () {
8-
error = new APIError({ status: 200 } as APIErrorOptions);
9-
6+
const error = new APIError({ status: 200 } as APIErrorOptions);
107
error.status.should.eql(200);
118
});
129

13-
it('sets message from message field', function () {
14-
error = new APIError({
15-
body: {
16-
message: 'oops. something went wrong'
17-
}
18-
} as APIErrorOptions);
10+
describe('details property', () => {
11+
it('sets details from message field', () => {
12+
const error = new APIError({
13+
body: {
14+
message: 'oops. something went wrong'
15+
}
16+
} as APIErrorOptions);
1917

20-
error.details.should.eql('oops. something went wrong');
18+
error.details.should.eql('oops. something went wrong');
19+
});
20+
21+
it('sets details if body is a string', () => {
22+
const error = new APIError({
23+
body: 'oops. something went wrong'
24+
} as APIErrorOptions);
25+
26+
error.details.should.eql('oops. something went wrong');
27+
});
2128
});
2229

23-
it('sets message from error field', function () {
24-
error = new APIError({
30+
it('sets message from error field', () => {
31+
const error = new APIError({
2532
body: {
2633
error: 'oops. something went wrong'
2734
}
2835
} as APIErrorOptions);
2936

3037
error.message.should.eql('oops. something went wrong');
3138
});
39+
40+
it('Creates instance if no data', () => {
41+
const error = new APIError({} as APIErrorOptions);
42+
error.should.has.property('status');
43+
error.should.has.property('message');
44+
error.should.has.property('details');
45+
error.type.should.eql('MailgunAPIError');
46+
});
3247
});

0 commit comments

Comments
 (0)