REST call of DataAccessObject.findById returns 404

Modify the remoting configuration of `DataAccessObject.findById()`
and add a `rest.before` handler that converts `null` to 404 error.

The change is covered by a test in loopback project.
This commit is contained in:
Miroslav Bajtos 2013-11-21 16:19:34 +01:00
parent e028d44271
commit f7c48c0d72
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 () {