From 4bf5970f9cb42d08fbcc0226856200a0d2839807 Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Tue, 3 Feb 2015 11:13:15 -0800 Subject: [PATCH] Map not found to 404 for hasOne --- lib/model.js | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/lib/model.js b/lib/model.js index 6438585f..a6ed0b15 100644 --- a/lib/model.js +++ b/lib/model.js @@ -431,6 +431,17 @@ Model.belongsToRemoting = function(relationName, relation, define) { }, fn); }; +function convertNullToNotFoundError(toModelName, ctx, cb) { + if (ctx.result !== null) return cb(); + + var fk = ctx.getArgByName('fk'); + var msg = 'Unknown "' + toModelName + '" id "' + fk + '".'; + var error = new Error(msg); + error.statusCode = error.status = 404; + error.code = 'MODEL_NOT_FOUND'; + cb(error); +} + Model.hasOneRemoting = function(relationName, relation, define) { var pathName = (relation.options.http && relation.options.http.path) || relationName; var toModelName = relation.modelTo.modelName; @@ -441,7 +452,8 @@ Model.hasOneRemoting = function(relationName, relation, define) { accepts: {arg: 'refresh', type: 'boolean', http: {source: 'query'}}, description: 'Fetches hasOne relation ' + relationName, accessType: 'READ', - returns: {arg: relationName, type: relation.modelTo.modelName, root: true} + returns: {arg: relationName, type: relation.modelTo.modelName, root: true}, + rest: {after: convertNullToNotFoundError.bind(null, toModelName)} }); define('__create__' + relationName, { @@ -468,24 +480,12 @@ Model.hasOneRemoting = function(relationName, relation, define) { description: 'Deletes ' + relationName + ' of this model.', accessType: 'WRITE' }); - }; Model.hasManyRemoting = function(relationName, relation, define) { var pathName = (relation.options.http && relation.options.http.path) || relationName; var toModelName = relation.modelTo.modelName; - function convertNullToNotFoundError(ctx, cb) { - if (ctx.result !== null) return cb(); - - var fk = ctx.getArgByName('fk'); - var msg = 'Unknown "' + toModelName + '" id "' + fk + '".'; - var error = new Error(msg); - error.statusCode = error.status = 404; - error.code = 'MODEL_NOT_FOUND'; - cb(error); - } - var findByIdFunc = this.prototype['__findById__' + relationName]; define('__findById__' + relationName, { isStatic: false, @@ -496,7 +496,7 @@ Model.hasManyRemoting = function(relationName, relation, define) { description: 'Find a related item by id for ' + relationName, accessType: 'READ', returns: {arg: 'result', type: toModelName, root: true}, - rest: {after: convertNullToNotFoundError} + rest: {after: convertNullToNotFoundError.bind(null, toModelName)} }, findByIdFunc); var destroyByIdFunc = this.prototype['__destroyById__' + relationName];