From ac50dd9bb3417d36ca389c43b7b1e8e48b765b5f Mon Sep 17 00:00:00 2001 From: Jeremiah Lu Date: Thu, 12 Mar 2015 15:11:35 -0700 Subject: [PATCH] More improvements on field returns for failure --- lib/errors.js | 48 +++++++++++++++++++++++++++++------------------- lib/io.js | 9 ++++++++- test/io.test.js | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 20 deletions(-) diff --git a/lib/errors.js b/lib/errors.js index 173f8bd..5561102 100644 --- a/lib/errors.js +++ b/lib/errors.js @@ -1,48 +1,58 @@ var util = require('util'); -var AvroIOError = function() { - Error.call(this); +var AvroIOError = function() { + Error.call(this); this.name = 'Avro IO Error'; this.message = util.format.apply(null, arguments); Error.captureStackTrace(this, arguments.callee); -}; +}; -var AvroFileError = function() { - Error.call(this); +var AvroFileError = function() { + Error.call(this); this.name = 'Avro File Error'; this.message = util.format.apply(null, arguments); Error.captureStackTrace(this, arguments.callee); -}; - -var AvroBlockError = function() { - Error.call(this); +}; + +var AvroBlockError = function() { + Error.call(this); this.name = 'Avro Block Error'; this.message = util.format.apply(null, arguments); Error.captureStackTrace(this, arguments.callee); -}; +}; -var AvroBlockDelayReadError = function() { - Error.call(this); +var AvroBlockDelayReadError = function() { + Error.call(this); this.name = 'Avro Block Delay Read Error'; this.message = util.format.apply(null, arguments); Error.captureStackTrace(this, arguments.callee); -}; +}; -var AvroInvalidSchemaError = function() { - Error.call(this); +var AvroInvalidSchemaError = function() { + Error.call(this); this.name = 'Avro Invalid Schema Error'; this.message = util.format.apply(null, arguments); Error.captureStackTrace(this, arguments.callee); -}; +}; + +var AvroDataValidationError = function() { + Error.call(this); + this.name = 'Avro Data Validation Error'; + this.message = util.format.apply(null, arguments); + this.fieldPath = []; + Error.captureStackTrace(this, arguments.callee); +}; util.inherits(AvroIOError, Error); util.inherits(AvroFileError, Error); util.inherits(AvroBlockError, Error); util.inherits(AvroBlockDelayReadError, Error); util.inherits(AvroInvalidSchemaError, Error); +util.inherits(AvroDataValidationError, Error); -exports.BlockDelayReadError = AvroBlockDelayReadError; -exports.BlockError = AvroBlockError; +exports.IOError = AvroIOError; exports.FileError = AvroFileError; +exports.BlockError = AvroBlockError; +exports.BlockDelayReadError = AvroBlockDelayReadError; exports.InvalidSchemaError = AvroInvalidSchemaError; -exports.IOError = AvroIOError; \ No newline at end of file +exports.DataValidationError = AvroDataValidationError; diff --git a/lib/io.js b/lib/io.js index 81f66a5..d5d66d3 100644 --- a/lib/io.js +++ b/lib/io.js @@ -575,7 +575,14 @@ DatumWriter.prototype = { writeRecord: function(writersSchema, datum, encoder) { var self = this; _.each(writersSchema.fields, function(field) { - self.writeData(field.type, datum[field.name], encoder); + try { + self.writeData(field.type, datum[field.name], encoder); + } catch (err) { + if (err.fieldPath) { + err.fieldPath.unshift(field.name); + } + throw err; + } }); } } diff --git a/test/io.test.js b/test/io.test.js index 3ba5ec3..183516e 100644 --- a/test/io.test.js +++ b/test/io.test.js @@ -380,6 +380,40 @@ describe('IO', function(){ block.toBuffer()[16].should.equal(data.age * 2); }) }); + + describe('bad writeRecord()', function(){ + it('should encode a record by encoding the values of its fields in the order that they are declared', function(){ + var schema = Avro.Schema({ + "name": "user", + "type": "record", + "fields": [ + {"name":"firstName","type": "string"}, + {"name":"lastName","type": "string"}, + {"name":"bah","type": "string"}, + {"name":"age","type": "int"} + ] + }); + var data = { + "firstName": "bob", + "lastName": "the_builder", + "extra": "foo", + "age": 40 + } + var block = DataFile.Block(); + var writer = IO.DatumWriter(schema); + var encoder = IO.BinaryEncoder(block); + var thrown = false; + try { + writer.writeRecord(schema, data, encoder); + } catch (err){ + err.fieldPath[0].should.equal("bah"); + thrown = true; + } + + thrown.should.equal(true); + }) + }); + describe('write()', function(){ it('should encode an int/long with zig-zag encoding', function() { var schema = Avro.Schema({