From 73d06d82716370f1e7d6414b0af4cca294603e22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jannis=20=C3=96tjengerdes?= Date: Fri, 13 Jul 2018 16:18:25 +0200 Subject: [PATCH] Catch error and return null on find/findOne --- lib/remote-connector.js | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/remote-connector.js b/lib/remote-connector.js index eef404e..c61fcc7 100644 --- a/lib/remote-connector.js +++ b/lib/remote-connector.js @@ -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;