Skip to content

Commit

Permalink
Decorate invalid default error message (#415)
Browse files Browse the repository at this point in the history
  • Loading branch information
mtth authored Nov 12, 2022
1 parent f258a65 commit 778f8da
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
14 changes: 13 additions & 1 deletion lib/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -2828,7 +2828,19 @@ function Field(schema, opts) {
// allowed instead).
// http://apache-avro.679487.n3.nabble.com/field-union-default-in-Java-td1175327.html
var type = this.type;
var val = type._copy(value, {coerce: 2, wrap: 2});
var val;
try {
val = type._copy(value, {coerce: 2, wrap: 2});
} catch (err) {
var msg = f('incompatible field default %j (%s)', value, err.message);
if (Type.isType(type, 'union')) {
msg += f(
', union defaults must match the first branch\'s type (%j)',
type.types[0]
);
}
throw new Error(msg);
}
// The clone call above will throw an error if the default is invalid.
if (isPrimitive(type.typeName) && type.typeName !== 'bytes') {
// These are immutable.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "avsc",
"version": "5.7.6",
"version": "5.7.7",
"description": "Avro for JavaScript",
"homepage": "https://github.com/mtth/avsc",
"keywords": [
Expand Down
2 changes: 1 addition & 1 deletion test/test_types.js
Original file line number Diff line number Diff line change
Expand Up @@ -1585,7 +1585,7 @@ suite('types', function () {
name: 'Person',
fields: [{name: 'name', type: ['null', 'string'], 'default': ''}]
});
});
}, /incompatible.*first branch/);
});

test('record default', function () {
Expand Down

0 comments on commit 778f8da

Please sign in to comment.