UNPKG

yekonga-server

Version:
124 lines (105 loc) 4.33 kB
// @ts-nocheck /*global Yekonga */ const H = Yekonga.Helper; module.exports = class Handler { static error(formattedError, error) { var errorData = {} var type = "Error"; if (error.originalError instanceof Yekonga.Error.Authentication) { type = 'AuthenticationError'; } else if (error.originalError instanceof Yekonga.Error.Forbidden) { type = 'ForbiddenError'; } else if (error.originalError instanceof Yekonga.Error.InternalService) { type = 'InternalServiceError'; } else if ( error.originalError instanceof Yekonga.Error.Validation || formattedError.message.startsWith('Validation:') || ( formattedError.extensions && formattedError.extensions.code && formattedError.extensions.code.toLowerCase().includes('validation')) ) { type = 'ValidationError'; } errorData.type = type; errorData.message = formattedError.message; if (Yekonga.Config.debug) { console.error(error); } return errorData; } static response(res) { var resetPermission = false; if (res.errors && res.errors.length) { const errList = []; for (const error of res.errors) { const validateTest = /Variable "[$](\w)*" got invalid value (.)* at "(.)*";/gmi; if (validateTest.test(error.message)) { var field = null; var row = '0'; var fieldTest = /"(\w)*\[0]([.](?<field1>([a-zA-Z0-9])+))+"|Field "(?<field2>([a-zA-Z0-9])+)"/gmi; var rowTest = /"(\w)*\[(?<row>([0-9]*))](.[a-zA-Z0-9])*"/gmi; var fieldGroups = fieldTest.exec(error.message); var rowGroups = rowTest.exec(error.message); // @ts-ignore if (fieldGroups) fieldGroups = fieldGroups.groups; // @ts-ignore if (rowGroups) rowGroups = rowGroups.groups; if (fieldGroups && fieldGroups.field1) { field = fieldGroups.field1; } else if (fieldGroups && fieldGroups.field2) { field = fieldGroups.field2; } if (rowGroups && rowGroups.row) { row = rowGroups.row; } errList.push({ row: parseInt(row), field, type: "Validation", message: error.message.replace(validateTest, '').trim() }); } else { errList.push({ message: error.message, type: error.type }); } } /** @type {any} */ var result = { errors: errList, resetPermission } if (res.data) { result.data = res.data; } if (Yekonga.Config.endToEndEncryption) { result = { data: H.encryptUrl(result), resetPermission }; } return result; } res.resetPermission = resetPermission; if (Yekonga.Config.endToEndEncryption) { res = { data: H.encryptUrl(res) }; } Yekonga.Temporary = {}; return res; } static mocks() { // return false; return { Int: () => { return 0; }, Float: () => { return 0.0; }, String: () => { return null; }, Boolean: () => { return false; }, Any: () => { return null; }, Object: () => { return {}; }, Array: () => { return []; }, Date: () => { return null; }, Bytes: () => { return 0; }, Time: () => { return null; }, Datetime: () => { return null; }, Timestamp: () => { return null; }, URL: () => { return null; }, AnyOrArray: () => { return null; }, } }; }