Skip to content

Commit 778f8da

Browse files
authored
Decorate invalid default error message (#415)
1 parent f258a65 commit 778f8da

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

lib/types.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2828,7 +2828,19 @@ function Field(schema, opts) {
28282828
// allowed instead).
28292829
// http://apache-avro.679487.n3.nabble.com/field-union-default-in-Java-td1175327.html
28302830
var type = this.type;
2831-
var val = type._copy(value, {coerce: 2, wrap: 2});
2831+
var val;
2832+
try {
2833+
val = type._copy(value, {coerce: 2, wrap: 2});
2834+
} catch (err) {
2835+
var msg = f('incompatible field default %j (%s)', value, err.message);
2836+
if (Type.isType(type, 'union')) {
2837+
msg += f(
2838+
', union defaults must match the first branch\'s type (%j)',
2839+
type.types[0]
2840+
);
2841+
}
2842+
throw new Error(msg);
2843+
}
28322844
// The clone call above will throw an error if the default is invalid.
28332845
if (isPrimitive(type.typeName) && type.typeName !== 'bytes') {
28342846
// These are immutable.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "avsc",
3-
"version": "5.7.6",
3+
"version": "5.7.7",
44
"description": "Avro for JavaScript",
55
"homepage": "https://github.com/mtth/avsc",
66
"keywords": [

test/test_types.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1585,7 +1585,7 @@ suite('types', function () {
15851585
name: 'Person',
15861586
fields: [{name: 'name', type: ['null', 'string'], 'default': ''}]
15871587
});
1588-
});
1588+
}, /incompatible.*first branch/);
15891589
});
15901590

15911591
test('record default', function () {

0 commit comments

Comments
 (0)