diff --git a/src/config/errors.ts b/src/config/errors.ts new file mode 100644 index 000000000..35074724a --- /dev/null +++ b/src/config/errors.ts @@ -0,0 +1,60 @@ +import i18n from 'i18next'; // Importing i18n instance + + +export const errorMessages = { + "0001": i18n.t('error:errors-bff.eServiceDescriptorNotFound'), + "0002": i18n.t('error:errors-bff.eServiceDescriptorWithoutInterface'), + "0003": i18n.t('error:errors-bff.notValidDescriptor'), + "0004": i18n.t('error:errors-bff.eServiceDocumentNotFound'), + "0005": i18n.t('error:errors-bff.eServiceNotFound'), + "0006": i18n.t('error:errors-bff.draftDescriptorAlreadyExists'), + "007": i18n.t('error:errors-bff.eServiceNameDuplicate'), + "0008": i18n.t('error:errors-bff.originNotCompliant'), + "0009": i18n.t('error:errors-bff.attributeNotFound'), + "0010": i18n.t('error:errors-bff.inconsistentDailyCalls'), + "0011": i18n.t('error:errors-bff.interfaceAlreadyExists'), + "0012": i18n.t('error:errors-bff.eServiceNotInDraftState'), + "0013": i18n.t('error:errors-bff.eServiceNotInReceiveMode'), + "0014": i18n.t('error:errors-bff.tenantNotFound'), + "0015": i18n.t('error:errors-bff.tenantKindNotFound'), + "0016": i18n.t('error:errors-bff.riskAnalysisValidationFailed'), + "0017": i18n.t('error:errors-bff.eServiceRiskAnalysisNotFound'), + "0018": i18n.t('error:errors-bff.eServiceRiskAnalysisIsRequired'), + "0019": i18n.t('error:errors-bff.riskAnalysisNotValid'), + "0020": i18n.t('error:errors-bff.documentPrettyNameDuplicate'), + "0021": i18n.t('error:errors-bff.riskAnalysisDuplicated'), + "0022": i18n.t('error:errors-bff.eServiceWithoutValidDescriptors'), + "0023": i18n.t('error:errors-bff.audienceCannotBeEmpty'), + "0024": i18n.t('error:errors-bff.eServiceWithActiveOrPendingDelegation'), + "0025": i18n.t('error:errors-bff.invalidEServiceFlags'), + "0026": i18n.t('error:errors-bff.inconsistentAttributesSeedGroupsCount'), + "0027": i18n.t('error:errors-bff.descriptorAttributeGroupSupersetMissingInAttributesSeed'), + "0028": i18n.t('error:errors-bff.unchangedAttributes'), + "0029": i18n.t('error:errors-bff.eServiceTemplateNotFound'), + "0030": i18n.t('error:errors-bff.eServiceTemplateWithoutPublishedVersion'), + "0031": i18n.t('error:errors-bff.templateInstanceNotAllowed'), + "0032": i18n.t('error:errors-bff.eServiceNotAnInstance'), + "0033": i18n.t('error:errors-bff.eServiceAlreadyUpgraded'), + "0034": i18n.t('error:errors-bff.invalidDescriptorVersion'), + "0035": i18n.t('error:errors-bff.eServiceTemplateInterfaceNotFound'), + "0036": i18n.t('error:errors-bff.eServiceTemplateInterfaceDataNotValid'), + "0037": i18n.t('error:errors-bff.descriptorTemplateVersionNotFound'), +} as const; + + +export const getMappedError = (errorCode: string) => { + const { service, error } = splitServiceCodeAndMessageCode(errorCode) + console.log("service,error", service, typeof error) + console.log("err2", errorMessages[error]) + console.log("err", i18n.t('error:errors-bff.eServiceNameDuplicate')) + return errorMessages[error] +} + + +const splitServiceCodeAndMessageCode = (errorCode: string): { service: string, error: keyof typeof errorMessages } => { + const splitMessage = errorCode.split('-') + return { + service: splitMessage[0], + error: splitMessage[1] as keyof typeof errorMessages + } +} \ No newline at end of file diff --git a/src/config/query-client.ts b/src/config/query-client.ts index d55543df2..c771a92b7 100644 --- a/src/config/query-client.ts +++ b/src/config/query-client.ts @@ -8,6 +8,7 @@ import { QueryClient, } from '@tanstack/react-query' import { AxiosError } from 'axios' +import { getMappedError } from './errors' declare module '@tanstack/react-query' { interface MutationMeta< @@ -177,16 +178,19 @@ mutationCache.config.onSuccess = (data, variables, context, mutation) => { } mutationCache.config.onError = (error, variables, context, mutation) => { + // If the error is due to the user cancelling the mutation, do nothing. if (error instanceof CancellationError) return const meta = resolveMeta({ mutation, error, variables, context }) + let errorMessage = meta.errorToastLabel if (meta.errorToastLabel) { let correlationId if (error instanceof AxiosError) { correlationId = error.response?.data.correlationId + errorMessage = getMappedError(error.response?.data.errors[0].code) } - showToast(meta.errorToastLabel, 'error', correlationId) + showToast(errorMessage, 'error', correlationId) } } diff --git a/src/static/locales/en/error.json b/src/static/locales/en/error.json index fd3006d44..35050aa5e 100644 --- a/src/static/locales/en/error.json +++ b/src/static/locales/en/error.json @@ -31,5 +31,44 @@ "assistencePartySelection": { "title": "Error: not authorized.", "description": "You do not have the necessary permissions to view PDND Interoperability in support mode, or your session has expired. Try signing in again from Google Workspace." + }, + "errors-bff": { + "eServiceDescriptorNotFound": "eService descriptor not found", + "eServiceDescriptorWithoutInterface": "eService descriptor without interface", + "notValidDescriptor": "Invalid descriptor", + "eServiceDocumentNotFound": "eService document not found", + "eServiceNotFound": "eService not found", + "draftDescriptorAlreadyExists": "A draft descriptor already exists", + "eServiceNameDuplicate": "The eService name already exists", + "originNotCompliant": "Non-compliant origin", + "attributeNotFound": "Attribute not found", + "inconsistentDailyCalls": "Inconsistent daily calls", + "interfaceAlreadyExists": "Interface already exists", + "eServiceNotInDraftState": "The eService is not in draft state", + "eServiceNotInReceiveMode": "The eService is not in receive mode", + "tenantNotFound": "Tenant not found", + "tenantKindNotFound": "Tenant type not found", + "riskAnalysisValidationFailed": "Risk analysis validation failed", + "eServiceRiskAnalysisNotFound": "eService risk analysis not found", + "eServiceRiskAnalysisIsRequired": "eService risk analysis is required", + "riskAnalysisNotValid": "Invalid risk analysis", + "documentPrettyNameDuplicate": "Document name already exists", + "riskAnalysisDuplicated": "Duplicated risk analysis", + "eServiceWithoutValidDescriptors": "eService without valid descriptors", + "audienceCannotBeEmpty": "The audience cannot be empty", + "eServiceWithActiveOrPendingDelegation": "eService with active or pending delegation", + "invalidEServiceFlags": "Invalid eService flags", + "inconsistentAttributesSeedGroupsCount": "Inconsistent attribute seed group count", + "descriptorAttributeGroupSupersetMissingInAttributesSeed": "Attribute group superset missing in attribute seed", + "unchangedAttributes": "Attributes not modified", + "eServiceTemplateNotFound": "eService template not found", + "eServiceTemplateWithoutPublishedVersion": "eService template without published version", + "templateInstanceNotAllowed": "Template instance not allowed", + "eServiceNotAnInstance": "The eService is not an instance", + "eServiceAlreadyUpgraded": "The eService has already been upgraded", + "invalidDescriptorVersion": "Invalid descriptor version", + "eServiceTemplateInterfaceNotFound": "eService template interface not found", + "eServiceTemplateInterfaceDataNotValid": "Invalid eService template interface data", + "descriptorTemplateVersionNotFound": "Descriptor template version not found" } } diff --git a/src/static/locales/it/error.json b/src/static/locales/it/error.json index 9cf51c631..2e21fde1a 100644 --- a/src/static/locales/it/error.json +++ b/src/static/locales/it/error.json @@ -31,5 +31,44 @@ "assistencePartySelection": { "title": "Errore: non autorizzato", "description": "Non hai i permessi necessari per visualizzare PDND Interoperabilità in modalità assistenza, oppure la sessione è scaduta. Prova ad effettuare nuovamente l’accesso da Google Workspace." - } + }, + "errors-bff": { + "eServiceDescriptorNotFound": "Descrittore dell'eService non trovato", + "eServiceDescriptorWithoutInterface": "Descrittore dell'eService senza interfaccia", + "notValidDescriptor": "Descrittore non valido", + "eServiceDocumentNotFound": "Documento dell'eService non trovato", + "eServiceNotFound": "eService non trovato", + "draftDescriptorAlreadyExists": "Esiste già un descrittore in bozza", + "eServiceNameDuplicate": "Il nome dell'eService è già presente", + "originNotCompliant": "Origine non conforme", + "attributeNotFound": "Attributo non trovato", + "inconsistentDailyCalls": "Chiamate giornaliere non consistenti", + "interfaceAlreadyExists": "Interfaccia già esistente", + "eServiceNotInDraftState": "L'eService non è in stato di bozza", + "eServiceNotInReceiveMode": "L'eService non è in modalità ricezione", + "tenantNotFound": "Tenant non trovato", + "tenantKindNotFound": "Tipo di tenant non trovato", + "riskAnalysisValidationFailed": "Validazione dell'analisi del rischio fallita", + "eServiceRiskAnalysisNotFound": "Analisi del rischio dell'eService non trovata", + "eServiceRiskAnalysisIsRequired": "Analisi del rischio dell'eService richiesta", + "riskAnalysisNotValid": "Analisi del rischio non valida", + "documentPrettyNameDuplicate": "Nome del documento già presente", + "riskAnalysisDuplicated": "Analisi del rischio duplicata", + "eServiceWithoutValidDescriptors": "eService senza descrittori validi", + "audienceCannotBeEmpty": "Il pubblico non può essere vuoto", + "eServiceWithActiveOrPendingDelegation": "eService con delega attiva o in sospeso", + "invalidEServiceFlags": "Flag dell'eService non validi", + "inconsistentAttributesSeedGroupsCount": "Conteggio dei gruppi di attributi non consistente", + "descriptorAttributeGroupSupersetMissingInAttributesSeed": "Gruppo di attributi mancanti nel seed degli attributi", + "unchangedAttributes": "Attributi non modificati", + "eServiceTemplateNotFound": "Template dell'eService non trovato", + "eServiceTemplateWithoutPublishedVersion": "Template dell'eService senza versione pubblicata", + "templateInstanceNotAllowed": "Istanza del template non consentita", + "eServiceNotAnInstance": "L'eService non è un'istanza", + "eServiceAlreadyUpgraded": "L'eService è già stato aggiornato", + "invalidDescriptorVersion": "Versione del descrittore non valida", + "eServiceTemplateInterfaceNotFound": "Interfaccia del template dell'eService non trovata", + "eServiceTemplateInterfaceDataNotValid": "Dati dell'interfaccia del template dell'eService non validi", + "descriptorTemplateVersionNotFound": "Versione del template del descrittore non trovata" +} }