|
| 1 | +// Copyright (C) 2025 Intel Corporation |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +import { Button, Heading, IllustratedMessage, View } from '@geti/ui'; |
| 5 | +import { NotFound } from '@geti/ui/icons'; |
| 6 | +import { isRouteErrorResponse, useRouteError } from 'react-router'; |
| 7 | + |
| 8 | +import { paths } from '../../router'; |
| 9 | +import { redirectTo } from '../utils'; |
| 10 | + |
| 11 | +const useErrorMessage = () => { |
| 12 | + const error = useRouteError(); |
| 13 | + |
| 14 | + if (isRouteErrorResponse(error)) { |
| 15 | + if (error.status === 400) { |
| 16 | + return 'The server cannot or will not process the current request.'; |
| 17 | + } |
| 18 | + |
| 19 | + if (error.status === 403) { |
| 20 | + return 'You do not have permission to access this page.'; |
| 21 | + } |
| 22 | + |
| 23 | + if (error.status === 404) { |
| 24 | + return "This page doesn't exist!"; |
| 25 | + } |
| 26 | + |
| 27 | + if (error.status === 401) { |
| 28 | + return "You aren't authorized to see this"; |
| 29 | + } |
| 30 | + |
| 31 | + if (error.status === 500) { |
| 32 | + return 'The server encountered an error and could not complete your request.'; |
| 33 | + } |
| 34 | + |
| 35 | + if (error.status === 503) { |
| 36 | + return 'Looks like our API is down'; |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + if (error instanceof TypeError) { |
| 41 | + return error.message; |
| 42 | + } |
| 43 | + |
| 44 | + return 'An unknown error occurred'; |
| 45 | +}; |
| 46 | + |
| 47 | +export const ErrorPage = (): JSX.Element => { |
| 48 | + const message = useErrorMessage(); |
| 49 | + |
| 50 | + return ( |
| 51 | + <View height={'100vh'}> |
| 52 | + <IllustratedMessage> |
| 53 | + <NotFound /> |
| 54 | + <Heading>{message}</Heading> |
| 55 | + |
| 56 | + <Button |
| 57 | + variant={'accent'} |
| 58 | + marginTop={'size-200'} |
| 59 | + onPress={() => { |
| 60 | + redirectTo(paths.root({})); |
| 61 | + }} |
| 62 | + > |
| 63 | + Go back to home page |
| 64 | + </Button> |
| 65 | + </IllustratedMessage> |
| 66 | + </View> |
| 67 | + ); |
| 68 | +}; |
0 commit comments