Skip to content

Commit

Permalink
More improvements on field returns for failure
Browse files Browse the repository at this point in the history
  • Loading branch information
thecsyco committed Mar 12, 2015
1 parent c3ceeda commit ac50dd9
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 20 deletions.
48 changes: 29 additions & 19 deletions lib/errors.js
Original file line number Diff line number Diff line change
@@ -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;
exports.DataValidationError = AvroDataValidationError;
9 changes: 8 additions & 1 deletion lib/io.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
});
}
}
Expand Down
34 changes: 34 additions & 0 deletions test/io.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down

0 comments on commit ac50dd9

Please sign in to comment.