|
| 1 | +import { Component, type ReactNode, } from 'react' |
| 2 | +import BaseLayout from '~/components/BaseLayout' |
| 3 | + |
| 4 | +export class NotFoundError extends Error { |
| 5 | + constructor(message?: string,) { |
| 6 | + super(message,) |
| 7 | + this.name = 'NotFoundError' |
| 8 | + } |
| 9 | +} |
| 10 | + |
| 11 | +export default function NotFound({ message, }: { message?: string },) { |
| 12 | + return ( |
| 13 | + <div className='flex flex-col items-center justify-center py-24 px-4'> |
| 14 | + <p className='text-7xl font-extralight tracking-tight text-ink-muted select-none mb-6'> |
| 15 | + 404 |
| 16 | + </p> |
| 17 | + <h1 className='text-lg font-medium text-ink mb-2'> |
| 18 | + Page not found |
| 19 | + </h1> |
| 20 | + {message && ( |
| 21 | + <p className='text-sm text-ink-muted mb-6'> |
| 22 | + {message} |
| 23 | + </p> |
| 24 | + )} |
| 25 | + {!message && <div className='mb-6' />} |
| 26 | + <a |
| 27 | + href='/' |
| 28 | + className='text-sm text-ink-muted hover:text-ink transition-colors' |
| 29 | + > |
| 30 | + ← Back to home |
| 31 | + </a> |
| 32 | + </div> |
| 33 | + ) |
| 34 | +} |
| 35 | + |
| 36 | +interface Props { |
| 37 | + children: ReactNode |
| 38 | +} |
| 39 | + |
| 40 | +interface State { |
| 41 | + error: Error | null |
| 42 | +} |
| 43 | + |
| 44 | +export class AppErrorBoundary extends Component<Props, State> { |
| 45 | + constructor(props: Props,) { |
| 46 | + super(props,) |
| 47 | + this.state = { error: null, } |
| 48 | + } |
| 49 | + |
| 50 | + static getDerivedStateFromError(error: Error,) { |
| 51 | + return { error, } |
| 52 | + } |
| 53 | + |
| 54 | + componentDidCatch() { |
| 55 | + // Could log to an error service here |
| 56 | + } |
| 57 | + |
| 58 | + render() { |
| 59 | + if (this.state.error instanceof NotFoundError) { |
| 60 | + return ( |
| 61 | + <BaseLayout> |
| 62 | + <NotFound message={this.state.error.message} /> |
| 63 | + </BaseLayout> |
| 64 | + ) |
| 65 | + } |
| 66 | + if (this.state.error) { |
| 67 | + return ( |
| 68 | + <BaseLayout> |
| 69 | + <div className='flex flex-col items-center justify-center py-24 px-4'> |
| 70 | + <p className='text-5xl font-extralight tracking-tight text-ink-muted select-none mb-6'> |
| 71 | + Error |
| 72 | + </p> |
| 73 | + <h1 className='text-lg font-medium text-ink mb-2'> |
| 74 | + Something went wrong |
| 75 | + </h1> |
| 76 | + <p className='text-sm text-ink-muted mb-6'> |
| 77 | + {this.state.error.message} |
| 78 | + </p> |
| 79 | + <a |
| 80 | + href='/' |
| 81 | + className='text-sm text-ink-muted hover:text-ink transition-colors' |
| 82 | + > |
| 83 | + ← Back to home |
| 84 | + </a> |
| 85 | + </div> |
| 86 | + </BaseLayout> |
| 87 | + ) |
| 88 | + } |
| 89 | + return this.props.children |
| 90 | + } |
| 91 | +} |
0 commit comments