quirons-broker
Advanced tools
Comparing version 0.0.1-alpha.109 to 0.0.1-alpha.110
import { Either } from 'fp-ts/lib/Either'; | ||
import * as t from 'io-ts'; | ||
declare type Language = 'en' | 'ptBR'; | ||
declare const ErrorCodeKey: t.KeyofC<{ | ||
@@ -22,3 +23,3 @@ ENTITY_ALREADY_EXIST: null; | ||
readonly usedPayload?: string; | ||
constructor(errorCode: ErrorCode, payload?: string); | ||
constructor(errorCode: ErrorCode, payload?: string, language?: Language); | ||
} | ||
@@ -31,3 +32,3 @@ declare class APIValidationError extends APIError { | ||
} | ||
declare const raiseErrorFromDecode: <T>(result: Either<t.Errors, T>) => never; | ||
declare const raiseErrorFromDecode: <T>(result: Either<t.Errors, T>, language?: Language) => never; | ||
export { APIError, APIValidationError, APIBadRequestError, ErrorCode, ErrorCodeKey, raiseErrorFromDecode }; |
115
errors.js
@@ -39,57 +39,117 @@ "use strict"; | ||
exports.ErrorCodeKey = ErrorCodeKey; | ||
const errorInfoWith = (payload = '') => ({ | ||
const englishMessages = { | ||
ENTITY_ALREADY_EXIST: `The provided entity already exists`, | ||
ENTITY_DOES_NOT_EXIST: 'The provided entity doesn\'t exist', | ||
BAD_REQUEST: 'An error has occurred in the request data processing.', | ||
VALIDATION_ERROR: 'The provided body data has an invalid format.', | ||
INTERNAL_SERVER_ERROR: 'Unexpected error', | ||
UNAUTHENTICATED: 'You must provide the correct credentials and branchId to access this resource', | ||
REQUIRED_BRANCH_ID: 'You must provide a branchId as query parameter (?branchId=...)', | ||
REQUIRED_ID_PARAMETER: 'The id parameter is required for the specified method', | ||
DECODED_ERROR: 'The message sent is outside the established format', | ||
TOO_MANY_REQUESTS: 'You have exceeded the request limits, please wait and try again later.' | ||
}; | ||
const brazilianPortugueseMessages = { | ||
ENTITY_ALREADY_EXIST: `A entidade fornecida já existe`, | ||
ENTITY_DOES_NOT_EXIST: 'A entidade fornecida não existe', | ||
BAD_REQUEST: 'Ocorreu um erro ao processar as informações da requisição.', | ||
VALIDATION_ERROR: 'As informações do corpo da mensagem possuem um formato inválido.', | ||
INTERNAL_SERVER_ERROR: 'Erro inesperado.', | ||
UNAUTHENTICATED: 'Você deve informar as credenciais corretas de acesso (usuário e senha) e possuir acesso a filial desejada', | ||
REQUIRED_BRANCH_ID: 'Você deve informar o branchId como parâmetro de busca (?branchId=...)', | ||
REQUIRED_ID_PARAMETER: 'O id é um parâmetro obrigatório neste método específico', | ||
DECODED_ERROR: 'A mensagem enviada está fora dos padrões estabelecidos', | ||
TOO_MANY_REQUESTS: 'Você excedeu o limite de requisições, por favor aguarde e tente novamente mais tarde.' | ||
}; | ||
const getMessage = (language) => { | ||
if (language === 'ptBR') { | ||
return brazilianPortugueseMessages; | ||
} | ||
return englishMessages; | ||
}; | ||
const getDetailedMessage = (payload, language) => { | ||
const englisDetailedhMessages = { | ||
ENTITY_ALREADY_EXIST: `Already exists an entity (${payload}) with the provided id`, | ||
ENTITY_DOES_NOT_EXIST: `Doesn't exist the entity (${payload}) with the specified id`, | ||
BAD_REQUEST: `It was returned the following message: "${payload}"`, | ||
VALIDATION_ERROR: `${payload || 'A Validation error has occurred, verify the body data format'}`, | ||
INTERNAL_SERVER_ERROR: englishMessages['INTERNAL_SERVER_ERROR'], | ||
UNAUTHENTICATED: englishMessages['UNAUTHENTICATED'], | ||
REQUIRED_BRANCH_ID: englishMessages['REQUIRED_BRANCH_ID'], | ||
REQUIRED_ID_PARAMETER: englishMessages['REQUIRED_ID_PARAMETER'], | ||
DECODED_ERROR: englishMessages['DECODED_ERROR'], | ||
TOO_MANY_REQUESTS: englishMessages['TOO_MANY_REQUESTS'] | ||
}; | ||
const brazilianPortugueseDetailedMessages = { | ||
ENTITY_ALREADY_EXIST: `Já existe uma entidade (${payload}) com o id informado`, | ||
ENTITY_DOES_NOT_EXIST: `Não existe a entidade (${payload}) com o id informado`, | ||
BAD_REQUEST: `Foi retornada a seguinte mensagem: "${payload}"`, | ||
VALIDATION_ERROR: `${payload || 'Ocorreu um erro de validação, verifique o formato das informações do corpo da mensagem'}`, | ||
INTERNAL_SERVER_ERROR: brazilianPortugueseMessages['INTERNAL_SERVER_ERROR'], | ||
UNAUTHENTICATED: brazilianPortugueseMessages['UNAUTHENTICATED'], | ||
REQUIRED_BRANCH_ID: brazilianPortugueseMessages['REQUIRED_BRANCH_ID'], | ||
REQUIRED_ID_PARAMETER: brazilianPortugueseMessages['REQUIRED_ID_PARAMETER'], | ||
DECODED_ERROR: brazilianPortugueseMessages['DECODED_ERROR'], | ||
TOO_MANY_REQUESTS: brazilianPortugueseMessages['TOO_MANY_REQUESTS'] | ||
}; | ||
if (language === 'ptBR') { | ||
return brazilianPortugueseDetailedMessages; | ||
} | ||
return englisDetailedhMessages; | ||
}; | ||
const errorInfoWith = (payload = '', language = 'en') => ({ | ||
ENTITY_ALREADY_EXIST: { | ||
status: 400, | ||
detailedMessage: `Already exists an entity (${payload}) with the provided id`, | ||
message: `The provided entity already exists` | ||
message: getMessage(language)['ENTITY_ALREADY_EXIST'], | ||
detailedMessage: getDetailedMessage(payload, language)['ENTITY_ALREADY_EXIST'] | ||
}, | ||
ENTITY_DOES_NOT_EXIST: { | ||
status: 404, | ||
detailedMessage: `Doesn't exist the entity (${payload}) with the specified id`, | ||
message: 'The provided entity doesn\'t exist' | ||
message: getMessage(language)['ENTITY_DOES_NOT_EXIST'], | ||
detailedMessage: getDetailedMessage(payload, language)['ENTITY_DOES_NOT_EXIST'] | ||
}, | ||
BAD_REQUEST: { | ||
status: 400, | ||
detailedMessage: `It was returned the following message: "${payload}"`, | ||
message: 'An error has occurred in the request data processing.' | ||
message: getMessage(language)['BAD_REQUEST'], | ||
detailedMessage: getDetailedMessage(payload, language)['BAD_REQUEST'] | ||
}, | ||
VALIDATION_ERROR: { | ||
status: 400, | ||
detailedMessage: `${payload || 'A Validation error has occurred, verify the body data format'}`, | ||
message: 'The provided body data has an invalid format.' | ||
message: getMessage(language)['VALIDATION_ERROR'], | ||
detailedMessage: getDetailedMessage(payload, language)['VALIDATION_ERROR'] | ||
}, | ||
INTERNAL_SERVER_ERROR: { | ||
status: 500, | ||
message: 'An unexpected error has occurred in the request processing', | ||
detailedMessage: 'Unexpected error' | ||
message: getMessage(language)['INTERNAL_SERVER_ERROR'], | ||
detailedMessage: getDetailedMessage(payload, language)['INTERNAL_SERVER_ERROR'] | ||
}, | ||
UNAUTHENTICATED: { | ||
status: 401, | ||
message: 'Unauthenticated', | ||
detailedMessage: 'You must provide the correct credentials and branchId to access this resource' | ||
message: getMessage(language)['UNAUTHENTICATED'], | ||
detailedMessage: getDetailedMessage(payload, language)['UNAUTHENTICATED'] | ||
}, | ||
REQUIRED_BRANCH_ID: { | ||
status: 400, | ||
message: 'The branchId query parameter is required', | ||
detailedMessage: 'You must provide a branchId as query parameter (?branchId=...)' | ||
message: getMessage(language)['REQUIRED_BRANCH_ID'], | ||
detailedMessage: getDetailedMessage(payload, language)['REQUIRED_BRANCH_ID'] | ||
}, | ||
REQUIRED_ID_PARAMETER: { | ||
status: 400, | ||
message: 'The id parameter is required', | ||
detailedMessage: 'The id parameter is required for the specified method' | ||
message: getMessage(language)['REQUIRED_ID_PARAMETER'], | ||
detailedMessage: getDetailedMessage(payload, language)['REQUIRED_ID_PARAMETER'] | ||
}, | ||
DECODED_ERROR: { | ||
status: 400, | ||
message: 'Decoded error message', | ||
detailedMessage: 'The message sent is outside the established format' | ||
message: getMessage(language)['DECODED_ERROR'], | ||
detailedMessage: getDetailedMessage(payload, language)['DECODED_ERROR'] | ||
}, | ||
TOO_MANY_REQUESTS: { | ||
status: 429, | ||
message: 'Too many requests', | ||
detailedMessage: 'You have exceeded the request limits, please wait and try again later.' | ||
message: getMessage(language)['TOO_MANY_REQUESTS'], | ||
detailedMessage: getDetailedMessage(payload, language)['TOO_MANY_REQUESTS'] | ||
} | ||
}); | ||
class APIError extends Error { | ||
constructor(errorCode, payload = '') { | ||
const { status, message, detailedMessage } = errorInfoWith(payload)[errorCode]; | ||
constructor(errorCode, payload = '', language = 'en') { | ||
const { status, message, detailedMessage } = errorInfoWith(payload, language)[errorCode]; | ||
super(message); | ||
@@ -121,3 +181,3 @@ this.code = errorCode; | ||
exports.APIBadRequestError = APIBadRequestError; | ||
const raiseErrorFromDecode = (result) => { | ||
const raiseErrorFromDecode = (result, language = 'en') => { | ||
const errors = PathReporter_1.PathReporter.report(result); | ||
@@ -150,6 +210,9 @@ const left = result._tag === 'Left' ? result.left : []; | ||
const s = attributes.length > 1 ? 's' : ''; | ||
const message = `Invalid or missing value${s} for the attribute${s}: ` | ||
+ `${attributesDetails}`; | ||
const es = attributes.length > 1 ? 'es' : ''; | ||
const message = language == 'ptBR' | ||
? `Valor${es} inválido${s} ou faltante${s} para o${s} atributo${s}:` | ||
: `Invalid or missing value${s} for the attribute${s}: ` | ||
+ `${attributesDetails}`; | ||
throw new APIValidationError(message); | ||
}; | ||
exports.raiseErrorFromDecode = raiseErrorFromDecode; |
{ | ||
"name": "quirons-broker", | ||
"version": "0.0.1-alpha.109", | ||
"version": "0.0.1-alpha.110", | ||
"description": "A small library to expose the broker types", | ||
@@ -5,0 +5,0 @@ "main": "index.ts", |
@@ -53,2 +53,3 @@ "use strict"; | ||
fromTTalk(data) { | ||
var _a; | ||
const result = ttalk.PositionInfo.decode(data); | ||
@@ -64,3 +65,3 @@ if (result._tag === 'Left') { | ||
description: data.name, | ||
cbo: data.cbo || undefined, | ||
cbo: ((_a = data.cbo) === null || _a === void 0 ? void 0 : _a.replace('-', '')) || undefined, | ||
activityDetails: data.activityDetails || undefined | ||
@@ -67,0 +68,0 @@ }; |
1015783
23534