refs #5878 perf: handleError
This commit is contained in:
parent
c8ca855ba9
commit
fb524e2f14
|
@ -351,5 +351,7 @@
|
|||
"sageWithholdingFk": "Sage con tenencia",
|
||||
"Cannot past travels with entries": "No se pueden pasar envíos con entradas",
|
||||
"It was not able to remove the next expeditions:": "No se pudo eliminar las siguientes expediciones: {{expeditions}}",
|
||||
"Field are invalid": "El campo '{{tag}}' no es válido"
|
||||
"Field are invalid": "El campo '{{tag}}' no es válido",
|
||||
"additionalData": "additionalData",
|
||||
"isToBeMailed": "isToBeMailed"
|
||||
}
|
|
@ -1,15 +1,26 @@
|
|||
|
||||
const UserError = require('../../util/user-error');
|
||||
const logToConsole = require('strong-error-handler/lib/logger');
|
||||
function isJsonString(str) {
|
||||
try {
|
||||
let json = JSON.parse(str);
|
||||
return (typeof json === 'object');
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
const validations = [{
|
||||
validation: message => String(message).startsWith('Value is not'),
|
||||
message: ({__: $t}, _tag) =>
|
||||
$t('Field are invalid', {tag: $t(_tag)}),
|
||||
handleError: ({method, originalUrl, body}) => {
|
||||
handleError: ({method: verb, originalUrl, body}) => {
|
||||
const {models} = require('vn-loopback/server/server');
|
||||
let tag = null;
|
||||
try {
|
||||
let [module, id, path] = originalUrl.split('api/')[1].split('/');
|
||||
let [module, ...path] = originalUrl.split('?')[0].split('api/')[1];
|
||||
path = path.join('/');
|
||||
// let module = url.split('/')[0];
|
||||
// let [id, path] = url.substring(url.indexOf('/') + 1).split('/');
|
||||
|
||||
let model = models[module];
|
||||
if (!model) {
|
||||
|
@ -17,14 +28,48 @@ const validations = [{
|
|||
model = models[module];
|
||||
}
|
||||
if (!model) throw new Error('No matching model found');
|
||||
const {accepts} = model.sharedClass.methods().find(method => method.name === path);
|
||||
const currentMethod = model.sharedClass.methods().find(method => {
|
||||
const methodMatch = [method.http].flat().filter(el => el.verb === verb.toLowerCase());
|
||||
if (methodMatch.length > 0) {
|
||||
const http = methodMatch.find(el => el.path.endsWith(path));
|
||||
if (http)
|
||||
return method;
|
||||
}
|
||||
}
|
||||
);
|
||||
const {accepts} = currentMethod;
|
||||
for (const [key, value] of Object.entries(body)) {
|
||||
const accept = accepts.find(acc => acc.arg === key);
|
||||
if (!value && accept.type !== 'any') {
|
||||
if (accept.type !== 'any') {
|
||||
let isValid = false;
|
||||
if (value) {
|
||||
switch (accept.type) {
|
||||
case 'object':
|
||||
isValid = isJsonString(value);
|
||||
break;
|
||||
case 'number':
|
||||
isValid = /^[0-9]*$/.test(value);
|
||||
break;
|
||||
|
||||
case 'boolean':
|
||||
isValid = value instanceof Boolean;
|
||||
break;
|
||||
|
||||
case 'date':
|
||||
isValid = (new Date(date) !== 'Invalid Date') && !isNaN(new Date(date));
|
||||
break;
|
||||
|
||||
case 'string':
|
||||
isValid = typeof value == 'string';
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!value || !isValid) {
|
||||
tag = key;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return tag;
|
||||
} catch (error) {
|
||||
throw new Error(error);
|
||||
|
|
Loading…
Reference in New Issue