Merge pull request #94 from angfal/404_error_catch

Catch error and return null when findById/findOne doesn't find an entity
This commit is contained in:
Miroslav Bajtoš 2019-01-22 16:46:58 +01:00 committed by GitHub
commit 4245df5c41
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 54 additions and 3 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 = ['findById', '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, null);
return;
}
cb(err, data);
};
}
scope[remoteMethod.name] = remoteMethodProxy;

View File

@ -191,9 +191,8 @@ describe('Remote model tests', function() {
ClientModel.deleteById(user.id, function(err) {
if (err) return done(err);
ClientModel.findById(user.id, function(err, notFound) {
if (err) return done(err);
assert.equal(notFound, null);
assert(err && err.statusCode === 404,
'should have failed with HTTP 404');
done();
});
});
@ -202,6 +201,15 @@ describe('Remote model tests', function() {
});
describe('Model.findById(id, callback)', function() {
it('should return null when an instance does not exist',
function(done) {
ClientModel.findById(23, function(err, notFound) {
if (err) return done(err);
assert.equal(notFound, null);
done();
});
});
it('should find an instance by id from the attached data source',
function(done) {
ServerModel.create({first: 'michael', last: 'jordan', id: 23},
@ -218,6 +226,32 @@ describe('Remote model tests', function() {
});
});
describe('Model.findOne([filter], callback)', function() {
it('should return null when an instance does not exist',
function(done) {
ClientModel.findOne({where: {id: 24}}, function(err, notFound) {
if (err) return done(err);
assert.equal(notFound, null);
done();
});
});
it('should find an instance from the attached data source',
function(done) {
ServerModel.create({first: 'keanu', last: 'reeves', id: 24},
function(err) {
if (err) return done(err);
ClientModel.findOne({where: {id: 24}}, function(err, user) {
if (err) return done(err);
assert.equal(user.id, 24);
assert.equal(user.first, 'keanu');
assert.equal(user.last, 'reeves');
done();
});
});
});
});
describe('Model.count([query], callback)', function() {
it('should return the count of Model instances from both data source',
function(done) {