Merge pull request #44 from strongloop/feature/get-unknown-id-should-return-404

REST call of DataAccessObject.findById returns 404
This commit is contained in:
Miroslav Bajtoš 2013-11-21 10:09:04 -08:00
commit 8acb4053ab
1 changed files with 12 additions and 1 deletions

View File

@ -331,9 +331,20 @@ setRemoting(DataAccessObject.findById, {
description: 'Find a model instance by id from the data source',
accepts: {arg: 'id', type: 'any', description: 'Model id', required: true},
returns: {arg: 'data', type: 'any', root: true},
http: {verb: 'get', path: '/:id'}
http: {verb: 'get', path: '/:id'},
rest: {after: convertNullToNotFoundError}
});
function convertNullToNotFoundError(ctx, cb) {
if (ctx.result !== null) return cb();
var modelName = ctx.method.sharedClass.name;
var id = ctx.getArgByName('id');
var msg = 'Unkown "' + modelName + '" id "' + id + '".';
var error = new Error(msg);
error.statusCode = error.status = 404;
cb(error);
}
// alias function for backwards compat.
DataAccessObject.all = function () {