Skip to content
This repository was archived by the owner on Jun 10, 2024. It is now read-only.

Commit 6ca80b0

Browse files
committed
fix: Custom properties should be available on thrown errors
1 parent 3627e87 commit 6ca80b0

File tree

2 files changed

+37
-40
lines changed

2 files changed

+37
-40
lines changed

‎src/object-schema.js‎

Lines changed: 9 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -112,53 +112,22 @@ class MissingDependentKeysError extends Error {
112112
/**
113113
* Wrapper error for errors occuring during a merge or validate operation.
114114
*/
115-
class WrapperError extends Error {
115+
class WrapperError {
116116

117117
/**
118118
* Creates a new instance.
119119
* @param {string} key The object key causing the error.
120120
* @param {Error} source The source error.
121121
*/
122122
constructor(key, source) {
123-
super(`Key "${key}": ` + source.message);
124-
125-
/**
126-
* The original source error.
127-
* @type {Error}
128-
*/
129-
this.source = source;
130-
}
131-
132-
/**
133-
* The stack from the original error.
134-
* @returns {string}
135-
*/
136-
get stack() {
137-
return this.source.stack;
138-
}
139-
140-
/**
141-
* The line number from the original error.
142-
* @returns {number}
143-
*/
144-
get lineNumber() {
145-
return this.source.lineNumber;
146-
}
147-
148-
/**
149-
* The column number from the original error.
150-
* @returns {number}
151-
*/
152-
get columnNumber() {
153-
return this.source.columnNumber;
154-
}
155-
156-
/**
157-
* The filename from the original error.
158-
* @returns {string}
159-
*/
160-
get fileName() {
161-
return this.source.fileName;
123+
return Object.create(source, {
124+
message: {
125+
value: `Key "${key}": ` + source.message,
126+
configurable: true,
127+
writable: true,
128+
enumerable: true
129+
}
130+
});
162131
}
163132
}
164133

‎tests/object-schema.js‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,34 @@ describe("ObjectSchema", () => {
130130

131131
});
132132

133+
it("should throw an error with custom properties when merge() throws an error with custom properties", () => {
134+
let schema = new ObjectSchema({
135+
foo: {
136+
merge() {
137+
throw {
138+
get message() {
139+
return "Boom!";
140+
},
141+
booya: true
142+
};
143+
},
144+
validate() {}
145+
}
146+
});
147+
148+
let errorThrown = false;
149+
150+
try {
151+
schema.merge({ foo: true }, { foo: true });
152+
} catch (ex) {
153+
errorThrown = true;
154+
assert.isTrue(ex.booya);
155+
}
156+
157+
assert.isTrue(errorThrown);
158+
159+
});
160+
133161
it("should call the merge() strategy for one key when called", () => {
134162

135163
schema = new ObjectSchema({

0 commit comments

Comments
 (0)