Catch error and return null on find/findOne

This commit is contained in:
Jannis Ötjengerdes 2018-07-13 16:18:25 +02:00
parent 6e09d0b4bf
commit 73d06d8271
1 changed files with 18 additions and 1 deletions

View File

@ -16,6 +16,8 @@ var jutil = require('loopback-datasource-juggler/lib/jutil');
var RelationMixin = require('./relations');
var InclusionMixin = require('loopback-datasource-juggler/lib/include');
var findMethodNames = ['find', 'findOne'];
/**
* Export the RemoteConnector class.
*/
@ -120,6 +122,11 @@ function createProxyMethod(Model, remotes, remoteMethod) {
} else {
callback = utils.createPromiseCallback();
}
var callbackPromise = callback.promise;
if (findMethodNames.includes(remoteMethod.name)) {
callback = proxy404toNull(callback);
}
if (remoteMethod.isStatic) {
remotes.invoke(remoteMethod.stringName, args, callback);
@ -128,7 +135,17 @@ function createProxyMethod(Model, remotes, remoteMethod) {
remotes.invoke(remoteMethod.stringName, ctorArgs, args, callback);
}
return callback.promise;
return callbackPromise;
}
function proxy404toNull(cb) {
return function(err, data) {
if (err && err.code === 'MODEL_NOT_FOUND') {
cb(null, {});
return;
}
cb(err, data);
};
}
scope[remoteMethod.name] = remoteMethodProxy;