6995-testToMaster_2410 #2139

Merged
alexm merged 236 commits from 6995-testToMaster_2410 into master 2024-03-07 07:09:08 +00:00
4 changed files with 92 additions and 28 deletions
Showing only changes of commit c11bcbebee - Show all commits

View File

@ -2,10 +2,12 @@
const UserError = require('../../util/user-error');
const logToConsole = require('strong-error-handler/lib/logger');
const valueIsNot = require('./value-is-not');
const valueInvalid = require('./value-invalid');
const validations = [{
...valueIsNot
}];
const validations = [
valueIsNot,
valueInvalid
];
module.exports = function() {
return function(err, req, res, next) {
// Thrown user errors
@ -15,11 +17,11 @@ module.exports = function() {
}
// Validation errors
if (err.statusCode == 400) {
if ([400, 422].includes(err.statusCode)) {
try {
validations.forEach(validation => {
if (validation.validation(err.message)) {
const error = validation.handleError(req);
const error = validation.handleError(req, err);
if (error)
err.message = validation.message(error, req);
}
@ -28,8 +30,6 @@ module.exports = function() {
return next(err);
} catch (e) {
}
}
if (err.statusCode == 422) {
try {
let code;
let {messages} = err.details;

View File

@ -0,0 +1,58 @@
const SLASH = '/';
const $t = require('i18n');
const {models} = require('vn-loopback/server/server');
const mapLocale = require('../../util/map-locales');
module.exports = {
validation: message => String(message).includes('is not valid'),
message: ({tagValue}, {__: $t}) =>
$t('Field are invalid', {tag: tagValue}),
handleError: ({method: verb, originalUrl, body}, err) => {
let tag = null;
let module = null;
let moduleOriginal = null;
let path = null;
try {
if (originalUrl.includes('?'))
originalUrl = originalUrl.split('?')[0];
originalUrl = originalUrl.split('api/')[1];
[module, ...path] = originalUrl.split(SLASH);
moduleOriginal = module;
let model = models[module];
// Capitalize
if (!model) {
module = module.charAt(0).toUpperCase() + module.slice(1);
model = models[module];
}
// Singular
if (!model) {
module = module.substring(0, module.length - 1);
model = models[module];
}
if (!model) throw new Error('No matching model found');
tag = Object.keys(err.details.codes)[0];
if (tag) {
let tagValue = mapLocale[$t.getLocale()][tag];
if (!tagValue) tagValue = tag;
return {tag, tagValue};
}
} catch (error) {
throw new Error(error);
}
}
};
function isJsonString(str) {
try {
let json = JSON.parse(str);
return (typeof json === 'object');
} catch (e) {
return false;
}
}

View File

@ -1,28 +1,15 @@
const SLASH = '/';
const MODULES = 'modules';
const BACK = 'back';
const path = require('path');
const glob = require('glob');
const modulesPath = `${MODULES}/**/${BACK}/locale/**/**.yml`;
const pathResolve = path.resolve(modulesPath);
const modelsLocale = glob.sync(pathResolve, {}).map(f => {
const file = require(f);
const model = f.substring(f.indexOf(MODULES), f.indexOf(BACK) - 1).split(SLASH)[1];
const locale = path.parse(f).base.split('.')[0];
return [keyMap(model, locale), file.columns];
}
);
const {models} = require('vn-loopback/server/server');
const mapLocale = new Map(modelsLocale);
const $t = require('i18n');
const mapLocale = require('../../util/map-locales');
module.exports = {
validation: message => String(message).startsWith('Value is not'),
message: ({tagValue}, {__: $t}) =>
$t('Field are invalid', {tag: tagValue}),
handleError: ({method: verb, originalUrl, body, __: $t}) => {
handleError: ({method: verb, originalUrl, body}) => {
let tag = null;
let module = null;
let moduleOriginal = null;
let path = null;
let hasId = false;
try {
@ -33,7 +20,6 @@ module.exports = {
[module, ...path] = originalUrl.split(SLASH);
hasId = path.length > 1;
moduleOriginal = module;
let model = models[module];
// Capitalize
if (!model) {
@ -98,7 +84,7 @@ module.exports = {
}
}
if (tag) {
let tagValue = mapLocale.get(keyMap(moduleOriginal, $t.getLocale()))[tag];
let tagValue = mapLocale[$t.getLocale()][tag];
if (!tagValue) tagValue = tag;
return {tag, tagValue};
}
@ -109,9 +95,6 @@ module.exports = {
};
function keyMap(model, local, connector = '_') {
return `${model}${connector}${local}`;
}
function isJsonString(str) {
try {

View File

@ -0,0 +1,23 @@
const SLASH = '/';
const MODULES = 'modules';
const BACK = 'back';
const path = require('path');
const glob = require('glob');
const modulesPath = `${MODULES}/**/${BACK}/locale/**/**.yml`;
const pathResolve = path.resolve(modulesPath);
const modelsLocale = glob.sync(pathResolve, {}).reduce((acc, f) => {
const file = require(f);
const model = f.substring(f.indexOf(MODULES), f.indexOf(BACK) - 1).split(SLASH)[1];
const locale = path.parse(f).base.split('.')[0];
if (!acc[locale]) acc[locale] = {};
acc[locale] = Object.assign(acc[locale], file.columns);
return acc;
}, {}
);
function keyMap(model, local, connector = '_') {
return `${model}${connector}${local}`;
}
module.exports =modelsLocale;