Skip to content

Commit

Permalink
Added the possibility to give an axiosError To the ErrorPage so it ca…
Browse files Browse the repository at this point in the history
…n handle the rendering of the error
  • Loading branch information
arnedierick committed Apr 25, 2024
1 parent 01c6795 commit 5e10f0e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 27 deletions.
2 changes: 1 addition & 1 deletion frontend/src/i18n/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@
"error": {
"title": "Error",
"subtitle": "Something went wrong, please try again later",
"homepage": "Back Home",
"back": "Go back",
"403_message": "you are not authorized to access this page.",
"404_message": "Sorry, the page you visited does not exist.",
"500_message": "Sorry, Something went wrong"
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/i18n/nl/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@
"error": {
"title": "Fout",
"subtitle": "Er ging iets mis, probeer het later opnieuw.",
"homepage": "Naar Start",
"back": "Keer terug",
"403_message": "Je bent niet bevoegd om deze pagina te bezoeken.",
"404_message" : "Sorry, de pagina die je bezocht bestaat niet.",
"500_message": "Sorry, er ging iets mis."
Expand Down
57 changes: 32 additions & 25 deletions frontend/src/pages/error/Error.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import React from "react";
import { AppRoutes } from "../../@types/routes"
import {useTranslation} from "react-i18next";
import {Button, Result, Space, Typography} from "antd";
import { Link, useNavigate } from "react-router-dom";

import React from "react";
import { useTranslation } from "react-i18next";
import { Button, Result, Space, Typography } from "antd";
import { useNavigate } from "react-router-dom";
import { AxiosError } from "axios";

interface ErrorPageProps {
errorCode?: number;
errorMessage?: string;
axiosError?: AxiosError;
}

const Error: React.FC<ErrorPageProps> = ({ errorCode, errorMessage }) => {
const { t } = useTranslation()
const Error: React.FC<ErrorPageProps> = ({ errorCode, errorMessage, axiosError }) => {
const { t } = useTranslation();
const { Title, Text } = Typography;
const navigate = useNavigate()
const navigate = useNavigate();

let title:string = "";
let status: "403" | "404" | "500" = "404";
let title: string
let status: "403" | "404" | "500" = "404";

// Mapping van errorCode naar status
const statusMapping: { [key: number]: "403" | "404" | "500" } = {
Expand All @@ -25,39 +25,46 @@ const Error: React.FC<ErrorPageProps> = ({ errorCode, errorMessage }) => {
500: "500"
};

const textMapping: { [key: number]: string} = {
const textMapping: { [key: number]: string } = {
404: t("error.404_message"),
403: t("error.403_message"),
500: t("error.500_message")
};

// If axiosError was provided, put error Info into the variables
if(axiosError){
errorCode = axiosError.response?.status || undefined;
if(axiosError.response !== undefined){
errorMessage = axiosError.response.data?.toString() || undefined
}
}

// Add default message to error page when no error message was provided
if(errorMessage === undefined){
if(errorCode !== undefined){
// add default message to error page when no error message was provided
if (errorMessage === undefined) {
if (errorCode !== undefined) {
errorMessage = textMapping[errorCode] || t("error.subtitle");
}else{
} else {
errorMessage = t("error.subtitle");
}
}
// Add default error title if no error code was provided, otherwise set it to error code
if(errorCode === undefined){
title = t("error.title")

}else{
title = errorCode.toString()
status = statusMapping[errorCode] || "404"
if (errorCode === undefined) {
title = t("error.title");
} else {
title = errorCode.toString();
status = statusMapping[errorCode] || "404";
}

return (
<Result status={status}>
<Space direction="vertical">
<Title>{title}</Title>
<Text>{errorMessage}</Text>
<Button onClick={()=> navigate(-1)} type="primary" >{t("error.homepage")}</Button>

<Button onClick={() => navigate(-1)} type="primary">{t("error.back")}</Button>
</Space>
</Result>
);
}
};

export default Error;

0 comments on commit 5e10f0e

Please sign in to comment.