feat: provide better error message

The current hasone relation error message is not an appropriate one when there is no data.
 The 404 error code seems fine but message is not.
 So adds a better error message when the relation contains no data.

prlint fixes
This commit is contained in:
Sujesh T 2019-11-30 15:22:47 +05:30
parent 4edcc4f14c
commit 215b245122
2 changed files with 883 additions and 754 deletions

View File

@ -187,8 +187,10 @@ module.exports = function(registry) {
const idDesc = ModelCtor.modelName + ' id';
ModelCtor.sharedCtor.accepts = [
{arg: 'id', type: 'any', required: true, http: {source: 'path'},
description: idDesc},
{
arg: 'id', type: 'any', required: true, http: {source: 'path'},
description: idDesc,
},
// {arg: 'instance', type: 'object', http: {source: 'body'}}
{arg: 'options', type: 'object', http: createOptionsViaModelMethod},
];
@ -254,7 +256,7 @@ module.exports = function(registry) {
));
console.warn(g.f(
'Please rework your app to use the offical solution for injecting ' +
'"options" argument from request context,\nsee %s',
'"options" argument from request context,\nsee %s',
'http://loopback.io/doc/en/lb3/Using-current-context.html',
));
}
@ -385,7 +387,7 @@ module.exports = function(registry) {
method.accessType === ACL.WRITE ||
method.accessType === ACL.EXECUTE, 'invalid accessType ' +
method.accessType +
'. It must be "READ", "REPLICATE", "WRITE", or "EXECUTE"');
'. It must be "READ", "REPLICATE", "WRITE", or "EXECUTE"');
return method.accessType;
}
@ -565,7 +567,9 @@ module.exports = function(registry) {
if (ctx.result !== null) return cb();
const fk = ctx.getArgByName('fk');
const msg = g.f('Unknown "%s" id "%s".', toModelName, fk);
const msg = fk ?
g.f('Unknown "%s" id "%s".', toModelName, fk) :
g.f('No "%s" instance(s) found', toModelName);
const error = new Error(msg);
error.statusCode = error.status = 404;
error.code = 'MODEL_NOT_FOUND';
@ -676,10 +680,12 @@ module.exports = function(registry) {
isStatic: false,
http: {verb: 'put', path: '/' + pathName + '/:fk'},
accepts: [
{arg: 'fk', type: 'any',
{
arg: 'fk', type: 'any',
description: g.f('Foreign key for %s', relationName),
required: true,
http: {source: 'path'}},
http: {source: 'path'},
},
{arg: 'data', type: 'object', model: toModelName, http: {source: 'body'}},
{arg: 'options', type: 'object', http: 'optionsFromRequest'},
],
@ -704,10 +710,12 @@ module.exports = function(registry) {
define('__link__' + relationName, {
isStatic: false,
http: {verb: 'put', path: '/' + pathName + '/rel/:fk'},
accepts: [{arg: 'fk', type: 'any',
accepts: [{
arg: 'fk', type: 'any',
description: g.f('Foreign key for %s', relationName),
required: true,
http: {source: 'path'}},
http: {source: 'path'},
},
].concat(accepts).concat([
{arg: 'options', type: 'object', http: 'optionsFromRequest'},
]),

File diff suppressed because it is too large Load Diff