Skip to content

Commit a6fb974

Browse files
michael-ciniawskyjoshwiens
authored andcommitted
fix(ValidationError): use Error.captureStackTrace for err.stack handling (#14)
1 parent 0cbab06 commit a6fb974

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

src/ValidationError.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
class ValidationError extends Error {
2-
constructor(err, name) {
3-
super(err);
2+
constructor(errors, name) {
3+
super();
44

5-
this.err = err;
6-
this.stack = false;
5+
this.name = 'ValidationError';
76

87
this.message = `Validation Error\n\n${name || ''} Invalid Options\n\n`;
98

10-
err.forEach((msg) => {
11-
this.message += `options${msg.dataPath} ${msg.message}\n`;
9+
errors.forEach((err) => {
10+
this.message += `options${err.dataPath} ${err.message}\n`;
1211
});
1312

13+
this.errors = errors;
14+
15+
Error.captureStackTrace(this, this.constructor);
16+
1417
return this;
1518
}
1619
}

test/index.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ describe('Error', () => {
4040
test('should have errors for every key in options', () => {
4141
try {
4242
validate();
43-
} catch (error) {
44-
const errors = error.err.map(e => e.dataPath);
43+
} catch (err) {
44+
const errors = err.errors.map(e => e.dataPath);
4545

4646
const expected = ['.string', '.array', '.object.prop', '.boolean', '.type', '.instance'];
4747

4848
expect(errors).toMatchObject(expected);
49-
expect(error.err).toMatchSnapshot();
49+
expect(err.errors).toMatchSnapshot();
5050
}
5151
});
5252
});

0 commit comments

Comments
 (0)