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:
commit
8acb4053ab
13
lib/dao.js
13
lib/dao.js
|
@ -331,9 +331,20 @@ setRemoting(DataAccessObject.findById, {
|
||||||
description: 'Find a model instance by id from the data source',
|
description: 'Find a model instance by id from the data source',
|
||||||
accepts: {arg: 'id', type: 'any', description: 'Model id', required: true},
|
accepts: {arg: 'id', type: 'any', description: 'Model id', required: true},
|
||||||
returns: {arg: 'data', type: 'any', root: 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.
|
// alias function for backwards compat.
|
||||||
DataAccessObject.all = function () {
|
DataAccessObject.all = function () {
|
||||||
|
|
Loading…
Reference in New Issue