Skip to content

Commit

Permalink
fix(dh): fix API error translation logic (#3886)
Browse files Browse the repository at this point in the history
  • Loading branch information
dzhavat authored Jan 16, 2025
1 parent 5834557 commit 0dd6109
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@
"invalid_gln": "'{{ value.value }}' er et ugyldigt GLN/EIC",
"invalid_length": "Indholdet af '{{ param }}' opfylder ikke krav til længden. Teksten skal være mellem {{ min }} og {{ max }} tegn.",
"invalid_enum": "Værdien '{{ value }}' er ikke tilladt for feltet '{{ param }}'.",
"invalidPhoneNumber": " {{ marketParticipant.market_participant.validation.user.authentication.invalid_phone }}",
"invalidPhoneNumber": "{{ marketParticipant.market_participant.validation.user.authentication.invalid_phone }}",
"args": {
"param": {
"Invitation": {
Expand All @@ -357,7 +357,7 @@
"required_permission_removed": "Denne handling ville resultere i, at en påkrævet rettighed ikke længere ville være tilgængelig, og handlingen blev derfor blokeret.",
"organization": {
"business_register_identifier": {
"reserved": "CVR-nummer {{ identifier }} er i brug"
"reserved": "CVR-nummer \"{{ identifier }}\" er i brug"
},
"domain": {
"reserved": "E-maildomæne er i brug",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@
"invalid_gln": "'{{ value.value }}' is an invalid GLN/EIC",
"invalid_length": "The contents of '{{ param }}' do not meet the length requirements. The text should be between {{ min }} and {{ max }} characters.",
"invalid_enum": "Value '{{ value }}' is not valid for field '{{ param }}'",
"invalidPhoneNumber": " {{ marketParticipant.market_participant.validation.user.authentication.invalid_phone }}",
"invalidPhoneNumber": "{{ marketParticipant.market_participant.validation.user.authentication.invalid_phone }}",
"args": {
"param": {
"Invitation": {
Expand All @@ -357,7 +357,7 @@
"required_permission_removed": "This operation would result in a required permission no longer being accessable, hence the operation was blocked.",
"organization": {
"business_register_identifier": {
"reserved": "Business register identifier {{ identifier }} is already in use"
"reserved": "Business register identifier \"{{ identifier }}\" is already in use"
},
"domain": {
"reserved": "E-mail domain is already in use",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,40 @@ export const readApiErrorResponse = (errors: ApiErrorCollection[]) => {

const translateApiError = (errorDescriptor: ApiErrorDescriptor) => {
const translationKey = `marketParticipant.${errorDescriptor.code}`;

const translation = translate(
translationKey,
flatten(translateArgs(errorDescriptor.args, translationKey))
);

return translationKey === translation
? translate(`marketParticipant.market_participant.error_fallback`, {
message: errorDescriptor.message,
})
: translation;
};

const translateArgs = (args: Record<string, string>, code: string) =>
Object.entries(args).reduce((acc, [key, value]) => {
const translateArgs = (args: Record<string, string>, code: string) => {
if (translationWithArgsExists(args, code)) {
return args;
}

return Object.entries(args).reduce((acc, [key, value]) => {
const translationPath = code.split('.');
translationPath.pop();

const translationKey = `${translationPath.join('.')}.args.${key}.${value}`;
const translation = translate(translationKey);
return { ...acc, [key]: translationKey === translation ? translationKey : translation };

return {
...acc,
[key]: translationKey === translation ? translationKey : translation,
};
}, {});
};

function translationWithArgsExists(args: Record<string, string>, code: string) {
const translationWithArgs = translate(code, flatten(args));

return translationWithArgs !== code;
}

0 comments on commit 0dd6109

Please sign in to comment.