Skip to content

Commit

Permalink
N21-1631 change ctl error handling (#3216)
Browse files Browse the repository at this point in the history
  • Loading branch information
MBergCap authored Apr 24, 2024
1 parent 6baaaba commit 2530d93
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 80 deletions.
39 changes: 25 additions & 14 deletions src/composables/external-tool-mappings.composable.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
import { BusinessError } from "@/store/types/commons";

const BusinessErrorMessageTranslationKeyMap = new Map<string, string>([
["tool_param_duplicate", "pages.tool.apiError.tool_param_duplicate"],
["tool_version_mismatch", "pages.tool.apiError.tool_version_mismatch"],
["tool_param_required", "pages.tool.apiError.tool_param_required"],
["tool_param_type_mismatch", "pages.tool.apiError.tool_param_type_mismatch"],
["tool_param_value_regex", "pages.tool.apiError.tool_param_value_regex"],
["tool_with_name_exists", "pages.tool.apiError.tool_with_name_exists"],
["tool_param_unknown", "pages.tool.apiError.tool_param_unknown"],
["tool_param_value_missing", "pages.tool.apiError.tool_param_value_missing"],
const ErrorTypeTranslationKeyMap = new Map<string, string>([
["TOOL_PARAMETER_DUPLICATE", "pages.tool.apiError.tool_param_duplicate"],
["TOOL_PARAMETER_REQUIRED", "pages.tool.apiError.tool_param_required"],
[
"TOOL_PARAMETER_TYPE_MISMATCH",
"pages.tool.apiError.tool_param_type_mismatch",
],
["TOOL_PARAMETER_VALUE_REGEX", "pages.tool.apiError.tool_param_value_regex"],
[
"CONTEXT_EXTERNAL_TOOL_NAME_ALREADY_EXISTS",
"pages.tool.apiError.tool_with_name_exists",
],
["TOOL_PARAMETER_UNKNOWN", "pages.tool.apiError.tool_param_unknown"],
[
"TOOL_PARAMETER_VALUE_MISSING",
"pages.tool.apiError.tool_param_value_missing",
],
]);

export function useExternalToolMappings() {
Expand All @@ -19,13 +27,16 @@ export function useExternalToolMappings() {
return undefined;
}

const translationKey = Array.from(
BusinessErrorMessageTranslationKeyMap.entries()
).find(([key]) => businessError.message.startsWith(key))?.[1];
if (businessError.error && "type" in businessError.error) {
const translationKey = ErrorTypeTranslationKeyMap.get(
businessError.error.type
);

if (translationKey) {
return translationKey;
if (translationKey) {
return translationKey;
}
}

return businessError.message;
};

Expand Down
174 changes: 116 additions & 58 deletions src/composables/external-tool-mappings.composable.unit.ts
Original file line number Diff line number Diff line change
@@ -1,74 +1,132 @@
import {
SchoolExternalToolResponse,
SchoolExternalToolSearchListResponse,
} from "@/serverApi/v3";
import { useExternalToolMappings } from "./external-tool-mappings.composable";
import { BusinessError } from "@/store/types/commons";
import { schoolToolConfigurationStatusFactory } from "@@/tests/test-utils/factory";

describe("useExternalToolUtils", () => {
const setup = () => {
const { getBusinessErrorTranslationKey } = useExternalToolMappings();

const toolResponse: SchoolExternalToolResponse = {
id: "id",
name: "toolName",
toolId: "toolId",
toolVersion: 1,
schoolId: "schoolId",
parameters: [
{
name: "name",
value: "value",
},
],
status: schoolToolConfigurationStatusFactory.build(),
};

const listResponse: SchoolExternalToolSearchListResponse = {
data: [toolResponse],
};

return {
listResponse,
toolResponse,
getBusinessErrorTranslationKey,
};
};

describe("useExternalToolMappings", () => {
describe("getBusinessErrorTranslationKey", () => {
it("should return original message when key is undefined", () => {
const { getBusinessErrorTranslationKey } = setup();
describe("when error type was found", () => {
const setup = () => {
const { getBusinessErrorTranslationKey } = useExternalToolMappings();

const translationKey: string | undefined =
getBusinessErrorTranslationKey(undefined);
expect(translationKey).toBeUndefined();
const key = "pages.tool.apiError.tool_with_name_exists";
const apiError = {
message:
"A tool with the same name is already assigned to this course. Tool names must be unique within a course.",
code: 400,
title: "Toolname already exists",
type: "CONTEXT_EXTERNAL_TOOL_NAME_ALREADY_EXISTS",
};
const businessError: BusinessError = {
statusCode: apiError.code,
message: apiError.message,
error: apiError,
};

return { getBusinessErrorTranslationKey, businessError, key };
};

it("should return translation key", () => {
const { getBusinessErrorTranslationKey, businessError, key } = setup();

const translationKey: string | undefined =
getBusinessErrorTranslationKey(businessError);
expect(translationKey).toEqual(key);
});
});

describe("when translation key was not found", () => {
const setup = () => {
const { getBusinessErrorTranslationKey } = useExternalToolMappings();

const apiError = {
message: "message",
code: 400,
title: "title",
type: "some_key",
};
const businessError: BusinessError = {
statusCode: apiError.code,
message: apiError.message,
error: apiError,
};

return { getBusinessErrorTranslationKey, businessError };
};

it("should return original error message", () => {
const { getBusinessErrorTranslationKey, businessError } = setup();

const translationKey: string | undefined =
getBusinessErrorTranslationKey(businessError);
expect(translationKey).toEqual(businessError.message);
});
});

describe("when businessError.error is undefined", () => {
const setup = () => {
const { getBusinessErrorTranslationKey } = useExternalToolMappings();

const businessError: BusinessError = {
statusCode: 400,
message: "original message",
error: undefined,
};

return { getBusinessErrorTranslationKey, businessError };
};

it("should return original error message", () => {
const { getBusinessErrorTranslationKey, businessError } = setup();

const translationKey: string | undefined =
getBusinessErrorTranslationKey(businessError);

expect(translationKey).toEqual(businessError.message);
});
});

it("should return translation key when message was found", () => {
const { getBusinessErrorTranslationKey } = setup();
const error: BusinessError = {
statusCode: "400",
message: "tool_param_duplicate: Some validationError was thrown",
describe("when error.type doesn't exist", () => {
const setup = () => {
const { getBusinessErrorTranslationKey } = useExternalToolMappings();

const apiError = {
message: "message",
code: 400,
title: "title",
};
const businessError: BusinessError = {
statusCode: apiError.code,
message: apiError.message,
error: apiError,
};

return { getBusinessErrorTranslationKey, businessError };
};

const translationKey: string | undefined =
getBusinessErrorTranslationKey(error);
expect(translationKey).toEqual(
"pages.tool.apiError.tool_param_duplicate"
);
it("should return original error message", () => {
const { getBusinessErrorTranslationKey, businessError } = setup();

const translationKey: string | undefined =
getBusinessErrorTranslationKey(businessError);
expect(translationKey).toEqual(businessError.message);
});
});

it("should return original message when key was not found", () => {
const { getBusinessErrorTranslationKey } = setup();
const error: BusinessError = {
statusCode: "400",
message: "some_error: which is not defined in map",
describe("when businessError is undefined", () => {
const setup = () => {
const { getBusinessErrorTranslationKey } = useExternalToolMappings();

return {
getBusinessErrorTranslationKey,
};
};

const translationKey: string | undefined =
getBusinessErrorTranslationKey(error);
expect(translationKey).toEqual(error.message);
it("should return undefined", () => {
const { getBusinessErrorTranslationKey } = setup();

const translationKey: string | undefined =
getBusinessErrorTranslationKey(undefined);
expect(translationKey).toBeUndefined();
});
});
});
});
2 changes: 0 additions & 2 deletions src/locales/de.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1556,8 +1556,6 @@ export default {
"Bei der Übertragung eines Parameterwerts ist ein Fehler ausgetreten. Bitte an den Support wenden.",
"pages.tool.apiError.tool_param_value_regex":
"Der Wert eines Parameters folgt nicht den vorgegebenen Regeln. Bitte den Wert entsprechend anpassen.",
"pages.tool.apiError.tool_version_mismatch":
"Die benutzte Version dieses Tools ist nicht mehr aktuell. Bitte die Version aktualisieren.",
"pages.tool.apiError.tool_with_name_exists":
"Ein Tool mit gleichem Namen wurde bereits dem Kurs zugeordnet. Die einem Kurs zugeordneten Tools müssen eindeutige Namen haben.",
"pages.tool.context.description":
Expand Down
2 changes: 0 additions & 2 deletions src/locales/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1528,8 +1528,6 @@ export default {
"An error occurred while transferring a parameter value. Please contact support.",
"pages.tool.apiError.tool_param_value_regex":
"The value of a parameter does not follow the given rules. Please adjust the value accordingly.",
"pages.tool.apiError.tool_version_mismatch":
"The version of this Tool used is out of date. Please update the version.",
"pages.tool.apiError.tool_with_name_exists":
"A tool with the same name is already assigned to this course. Tool names must be unique within a course.",
"pages.tool.context.description":
Expand Down
2 changes: 0 additions & 2 deletions src/locales/es.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1573,8 +1573,6 @@ export default {
"Se produjo un error al transferir un valor de parámetro. Por favor contacte al soporte.",
"pages.tool.apiError.tool_param_value_regex":
"El valor de un parámetro no sigue las reglas dadas. Por favor, ajuste el valor en consecuencia.",
"pages.tool.apiError.tool_version_mismatch":
"La versión de esta Herramienta utilizada está desactualizada. Actualice la versión.",
"pages.tool.apiError.tool_with_name_exists":
"Ya se ha asignado una herramienta con el mismo nombre a este curso. Los nombres de las herramientas deben ser únicos dentro de un curso.",
"pages.tool.context.description":
Expand Down
2 changes: 0 additions & 2 deletions src/locales/uk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1549,8 +1549,6 @@ export default {
"Під час передачі значення параметра сталася помилка. Зверніться до служби підтримки.",
"pages.tool.apiError.tool_param_value_regex":
"Значення параметра не відповідає наведеним правилам. Відкоригуйте значення відповідно.",
"pages.tool.apiError.tool_version_mismatch":
"Використана версія цього інструменту застаріла. Будь ласка, оновіть версію.",
"pages.tool.apiError.tool_with_name_exists":
"Інструмент із такою ж назвою вже призначено цьому курсу. Назви інструментів мають бути унікальними в межах курсу.",
"pages.tool.context.description":
Expand Down

0 comments on commit 2530d93

Please sign in to comment.