Skip to content

Commit 24d5adb

Browse files
committed
fix(json-type-value): 🐛 show value encoding problemsn in development mode
1 parent 9826eb5 commit 24d5adb

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

src/json-type-value/Value.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export class Value<T extends Type = Type> {
66
public type: T,
77
public data: ResolveType<T>,
88
) {}
9-
9+
1010
public encode(codec: JsonValueCodec): void {
1111
const value = this.data;
1212
const type = this.type;
@@ -16,3 +16,24 @@ export class Value<T extends Type = Type> {
1616
else type.encoder(codec.format)(value, encoder);
1717
}
1818
}
19+
20+
if (process.env.NODE_ENV !== 'production') {
21+
const encode = Value.prototype.encode;
22+
Value.prototype.encode = function (codec: JsonValueCodec): void {
23+
try {
24+
encode.call(this, codec);
25+
} catch (error) {
26+
try {
27+
// tslint:disable-next-line no-console
28+
console.error(error);
29+
const type = this.type;
30+
if (type) {
31+
const err = type.validator('object')(this.data);
32+
// tslint:disable-next-line no-console
33+
console.error(err);
34+
}
35+
} catch {}
36+
throw error;
37+
}
38+
};
39+
}

0 commit comments

Comments
 (0)