From 778f8daa057b39b9538ebaa04f7c382bd95482b2 Mon Sep 17 00:00:00 2001 From: Matthieu Monsch <1216372+mtth@users.noreply.github.com> Date: Fri, 11 Nov 2022 18:59:04 -0800 Subject: [PATCH] Decorate invalid default error message (#415) --- lib/types.js | 14 +++++++++++++- package.json | 2 +- test/test_types.js | 2 +- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/lib/types.js b/lib/types.js index 8070de0a..f4d644e6 100644 --- a/lib/types.js +++ b/lib/types.js @@ -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. diff --git a/package.json b/package.json index 0609a5da..5c2a56da 100644 --- a/package.json +++ b/package.json @@ -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": [ diff --git a/test/test_types.js b/test/test_types.js index cd470b7c..60ee218d 100644 --- a/test/test_types.js +++ b/test/test_types.js @@ -1585,7 +1585,7 @@ suite('types', function () { name: 'Person', fields: [{name: 'name', type: ['null', 'string'], 'default': ''}] }); - }); + }, /incompatible.*first branch/); }); test('record default', function () {