mirror of
https://github.com/rjNemo/ticket_manager
synced 2026-06-12 11:46:40 +00:00
error handling after get request & redirect to error page
This commit is contained in:
parent
202eaad2df
commit
8f7e790c70
7 changed files with 43 additions and 32 deletions
11
client/src/controllers/ErrorController.tsx
Normal file
11
client/src/controllers/ErrorController.tsx
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
import React, { FC } from "react";
|
||||||
|
import { Redirect } from "react-router-dom";
|
||||||
|
|
||||||
|
interface IProps {
|
||||||
|
error: any;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const ErrorController: FC<IProps> = ({ error }) => {
|
||||||
|
if (error === "Not Found") return <Redirect to="/404" />;
|
||||||
|
return <></>;
|
||||||
|
};
|
||||||
|
|
@ -1,16 +1,19 @@
|
||||||
import React, { FC, useState, useEffect } from "react";
|
import React, { FC, useState, useEffect } from "react";
|
||||||
import { useParams, Redirect } from "react-router-dom";
|
import { useParams } from "react-router-dom";
|
||||||
|
import { ErrorController } from "./ErrorController";
|
||||||
import { ProjectPage } from "../pages/ProjectPage";
|
import { ProjectPage } from "../pages/ProjectPage";
|
||||||
import ProjectVM from "../VM/ProjectVM";
|
import ProjectVM from "../VM/ProjectVM";
|
||||||
import { Project } from "../types/Project";
|
import { Project } from "../types/Project";
|
||||||
|
import { HttpResponse } from "../types/HttpResponse";
|
||||||
import { Preloader } from "../components/Preloader";
|
import { Preloader } from "../components/Preloader";
|
||||||
import { Constants } from "../utils/Constants";
|
import { Constants } from "../utils/Constants";
|
||||||
import { HttpResponse, get } from "../utils/http";
|
import { get } from "../utils/http";
|
||||||
|
|
||||||
export const ProjectController: FC = () => {
|
export const ProjectController: FC = () => {
|
||||||
const [project, setProject] = useState({} as Project);
|
const [project, setProject] = useState<Project>({} as Project);
|
||||||
const [isLoading, setIsLoading] = useState(true);
|
const [isLoading, setIsLoading] = useState(true);
|
||||||
const [hasError, setHasError] = useState(false);
|
const [hasError, setHasError] = useState(false);
|
||||||
|
const [error, setError] = useState();
|
||||||
const { id } = useParams();
|
const { id } = useParams();
|
||||||
|
|
||||||
async function httpGet(id: string): Promise<void> {
|
async function httpGet(id: string): Promise<void> {
|
||||||
|
|
@ -24,6 +27,7 @@ export const ProjectController: FC = () => {
|
||||||
}
|
}
|
||||||
} catch (ex) {
|
} catch (ex) {
|
||||||
setHasError(true);
|
setHasError(true);
|
||||||
|
setError(ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -33,10 +37,9 @@ export const ProjectController: FC = () => {
|
||||||
}
|
}
|
||||||
}, [id]);
|
}, [id]);
|
||||||
|
|
||||||
const viewModel = new ProjectVM(project);
|
|
||||||
|
|
||||||
if (hasError) {
|
if (hasError) {
|
||||||
return <Redirect to="/error" />;
|
return <ErrorController error={error} />;
|
||||||
}
|
}
|
||||||
|
const viewModel = new ProjectVM(project);
|
||||||
return isLoading ? <Preloader /> : <ProjectPage viewModel={viewModel} />;
|
return isLoading ? <Preloader /> : <ProjectPage viewModel={viewModel} />;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -1,12 +0,0 @@
|
||||||
import React, { FC } from "react";
|
|
||||||
|
|
||||||
interface IProps {
|
|
||||||
error: any;
|
|
||||||
}
|
|
||||||
export const ErrorPage: FC<IProps> = ({ error }) => {
|
|
||||||
return (
|
|
||||||
<div className="section">
|
|
||||||
<p>{error}</p>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
};
|
|
||||||
10
client/src/pages/NotFoundPage.tsx
Normal file
10
client/src/pages/NotFoundPage.tsx
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
import React, { FC } from "react";
|
||||||
|
|
||||||
|
interface IProps {}
|
||||||
|
export const NotFoundPage: FC<IProps> = () => {
|
||||||
|
return (
|
||||||
|
<div className="section">
|
||||||
|
<p>error</p>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
3
client/src/types/HttpResponse.ts
Normal file
3
client/src/types/HttpResponse.ts
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
export interface HttpResponse<T> extends Response {
|
||||||
|
parsedBody?: T;
|
||||||
|
}
|
||||||
|
|
@ -1,8 +1,4 @@
|
||||||
import { Redirect } from "react-router-dom";
|
import { HttpResponse } from "../types/HttpResponse";
|
||||||
|
|
||||||
export interface HttpResponse<T> extends Response {
|
|
||||||
parsedBody?: T;
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function http<T>(request: RequestInfo): Promise<HttpResponse<T>> {
|
export async function http<T>(request: RequestInfo): Promise<HttpResponse<T>> {
|
||||||
const response: HttpResponse<T> = await fetch(request);
|
const response: HttpResponse<T> = await fetch(request);
|
||||||
|
|
@ -10,7 +6,7 @@ export async function http<T>(request: RequestInfo): Promise<HttpResponse<T>> {
|
||||||
response.parsedBody = await response.json();
|
response.parsedBody = await response.json();
|
||||||
} catch (ex) {}
|
} catch (ex) {}
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
throw new Error(response.statusText);
|
throw response.statusText;
|
||||||
}
|
}
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,15 +2,15 @@ import React from "react";
|
||||||
import {
|
import {
|
||||||
Router,
|
Router,
|
||||||
Route,
|
Route,
|
||||||
Switch,
|
Switch
|
||||||
Redirect
|
// Redirect
|
||||||
//Link, NavLink
|
//Link, NavLink
|
||||||
} from "react-router-dom";
|
} from "react-router-dom";
|
||||||
import * as creacteHistory from "history";
|
import * as creacteHistory from "history";
|
||||||
// import { TicketPage } from "../pages/TicketPage";
|
// import { TicketPage } from "../pages/TicketPage";
|
||||||
// import { HomeController } from "../controllers/HomeController";
|
// import { HomeController } from "../controllers/HomeController";
|
||||||
import { ProjectController } from "../controllers/ProjectController";
|
import { ProjectController } from "../controllers/ProjectController";
|
||||||
import { ErrorPage } from "../pages/ErrorPage";
|
import { NotFoundPage } from "../pages/NotFoundPage";
|
||||||
// import { UserController } from "../controllers/UserController";
|
// import { UserController } from "../controllers/UserController";
|
||||||
// import { TicketController } from "../controllers/TicketController";
|
// import { TicketController } from "../controllers/TicketController";
|
||||||
|
|
||||||
|
|
@ -34,13 +34,13 @@ export const AppRouter = () => {
|
||||||
<TicketController />
|
<TicketController />
|
||||||
</Route> */}
|
</Route> */}
|
||||||
|
|
||||||
<Route path="/error">
|
<Route path="/404">
|
||||||
<ErrorPage error="" />
|
<NotFoundPage />
|
||||||
</Route>
|
</Route>
|
||||||
|
|
||||||
<Route path="*">
|
{/* <Route path="*">
|
||||||
<Redirect to="/error" />
|
<Redirect to="/error" />
|
||||||
</Route>
|
</Route> */}
|
||||||
</Switch>
|
</Switch>
|
||||||
</div>
|
</div>
|
||||||
</Router>
|
</Router>
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue