Last active
August 29, 2015 13:57
-
-
Save pon/9415651 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
create = { | |
handler: function(request, reply) { | |
return new Address().save({ | |
name: request.payload.name, | |
address_line1: request.payload.address_line1 | |
}).then(function(model) { | |
return new Address({ | |
'id': model.id | |
}).fetch().then(function(model) { | |
return reply(model); | |
}); | |
}, function(error) { | |
return reply(error); | |
}); | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Address = Db.Model.extend({ | |
tableName: 'addresses', | |
validate: function() { | |
var basicValidation, payload, schema; | |
payload = this.clone(); | |
schema = { | |
name: Joi.string().max(50).required(), | |
address_line1: Joi.string().required() | |
}; | |
basicValidation = Joi.validate(payload.toJSON(), schema, { | |
stripUnknown: true | |
}); | |
if(basicValidation) { | |
throw new Error(basicValidation.message); | |
} else { | |
return; | |
} | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
db.Model.prototype.initialize = function() { | |
this.on('saving', this.validate, this); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Possibly unhandled Error: the value of name is not allowed to be undefined | |
at Db.Model.extend.validate (/Users/peternagel/workspace/lob/lob-api/build/app/models/addressesModel.js:66:13) |
I implemented Tim's CheckIt library and tried out the code here: bookshelf/bookshelf#39
It works pretty much the same as what we have and it logs a Possibly unhandled error. Is it acceptable to have an error here that is possibly unhandled since we are catching it in the controller?
@PeteOtto An unhandled error is definitely not something to ignore. Can you try it again with the extraneous code cut from the controller? (https://gist.github.com/bendrucker/9417825#file-addresscontroller-js)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The "value of name is not allowed to be undefined" is caught in the addressController on line 12 and returned in the reply. However, it is still being logged as Possibly unhandled.