-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from mishaganin/dev
Add error boundary
- Loading branch information
Showing
4 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
.error-boundary { | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
flex-direction: column; | ||
width: 100%; | ||
height: 100vh; | ||
|
||
.error-boundary__title { | ||
font-family: 'Bebas Neue', sans-serif; | ||
color: #c652e8; | ||
} | ||
|
||
.error-boundary__link { | ||
font-family: 'Bebas Neue', sans-serif; | ||
color: #5266e8; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import React from 'react'; | ||
import { Link, isRouteErrorResponse, useRouteError } from 'react-router-dom'; | ||
import './ErrorBoundary.scss'; | ||
import { HttpStatusCode } from '../../types/types.ts'; | ||
|
||
const ErrorBoundary = () => { | ||
const error = useRouteError(); | ||
|
||
console.log(error); | ||
|
||
const renderErrorMessage = (): JSX.Element => { | ||
let errorMessage = 'Sorry.. there was an error'; | ||
if (isRouteErrorResponse(error)) { | ||
if (error.status === HttpStatusCode.NOT_FOUND) { | ||
errorMessage = 'This page doesn\'t exist!'; | ||
} | ||
|
||
if (error.status === HttpStatusCode.UNAUTHORIZED) { | ||
errorMessage = 'You aren\'t authorized to see this'; | ||
} | ||
|
||
if (error.status === HttpStatusCode.SERVICE_UNAVAILABLE) { | ||
errorMessage = 'Looks like our API is down'; | ||
} | ||
|
||
if (error.status === HttpStatusCode.I_AM_A_TEAPOT) { | ||
errorMessage = '🫖'; | ||
} | ||
} | ||
|
||
return <h1 className="error-boundary__title">{errorMessage}</h1>; | ||
}; | ||
|
||
return ( | ||
<div className="error-boundary"> | ||
{renderErrorMessage()} | ||
<Link className="error-boundary__link" to="/"> | ||
<span>Go back to home page</span> | ||
</Link> | ||
</div> | ||
); | ||
}; | ||
|
||
export default ErrorBoundary; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters