File size: 2,588 Bytes
4545bb3
 
 
 
 
 
 
bcc4b86
4545bb3
 
bcc4b86
4545bb3
 
 
bcc4b86
4545bb3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { Button, Group, Stack, Text, Title } from '@mantine/core';
import { Component, type ErrorInfo, type ReactNode } from 'react';
import { Link, useLocation } from 'react-router-dom';

/** One layout for "page not found", "no access" and "something broke": what happened, and a way on. */
export function ProblemPage({ code, title, children, actions }: { code: string; title: string; children: ReactNode; actions?: ReactNode }) {
  return (
    <section className="fpt-problem" aria-labelledby="problem-title" data-testid={`problem-${code}`}>

      <Stack gap="sm" maw={520} mx="auto" py={64} px="md">

        <Text size="sm" c="dimmed" fw={600}>{code}</Text>

        <Title order={1} id="problem-title">{title}</Title>

        <Text>{children}</Text>

        <Group gap="sm" mt="md">{actions ?? <Button component={Link} to="/" variant="filled">Go to Home</Button>}</Group>

      </Stack>

    </section>
  );
}

export function NotFoundPage() {
  const loc = useLocation();
  return (
    <ProblemPage code="404" title="This page does not exist">

      There is nothing at <code>{loc.pathname}</code>. The link may be old, or the test run or report may have been removed.

    </ProblemPage>
  );
}

/** Catches a screen that crashed while drawing, so the rest of the app keeps working and the person can recover. */
export class ScreenErrorBoundary extends Component<{ children: ReactNode; resetKey?: string }, { error: Error | null }> {
  state: { error: Error | null } = { error: null };

  static getDerivedStateFromError(error: Error) {
    return { error };
  }

  componentDidCatch(error: Error, info: ErrorInfo) {
    // the browser console is the only place the details go: nothing is sent anywhere
    console.error('Screen error', error, info.componentStack);
  }

  componentDidUpdate(prev: { resetKey?: string }) {
    if (this.state.error && prev.resetKey !== this.props.resetKey) this.setState({ error: null });   // navigated away
  }

  render() {
    if (!this.state.error) return this.props.children;
    return (
      <ProblemPage code="Error" title="This screen stopped working"

        actions={<><Button variant="filled" onClick={() => window.location.reload()}>Reload the page</Button><Button component={Link} to="/" variant="default">Go to Home</Button></>}>
        Something went wrong while showing this page. Your test runs are not affected: they keep running on the server.
        Reload the page; if it happens again, tell your administrator what you were doing.
      </ProblemPage>
    );
  }
}