-
Notifications
You must be signed in to change notification settings - Fork 2
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 #522 from NYPL/SFR-2136/add-error-boundaries
SFR-2136: Implement error boundary
- Loading branch information
Showing
11 changed files
with
134 additions
and
35 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
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
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
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,33 @@ | ||
import { test, expect } from "../support/test-utils"; | ||
import { | ||
INVALID_COLLECTION_PATH, | ||
HOME_PATH, | ||
COLLECTION_PATH, | ||
} from "~/mocks/mockEnv"; | ||
import { server } from "~/mocks/server"; | ||
|
||
test.afterEach(() => server.resetHandlers()); | ||
test.afterAll(() => server.close()); | ||
|
||
test("View landing page with collection", async ({ page, port }) => { | ||
await page.goto(`http://localhost:${port}${HOME_PATH}`); | ||
const collectionHeading = page.getByRole("heading", { | ||
name: "Recently Added Collections", | ||
level: 2, | ||
}); | ||
expect(collectionHeading).toBeVisible(); | ||
const collectionLink = await page | ||
.getByRole("link", { | ||
name: /Baseball: A Collection by Mike Benowitz/, | ||
}) | ||
.getAttribute("href"); | ||
expect(collectionLink).toContain(COLLECTION_PATH); | ||
}); | ||
|
||
test("Shows error boundary for invalid collection", async ({ page, port }) => { | ||
await page.goto(`http://localhost:${port}${INVALID_COLLECTION_PATH}`); | ||
|
||
const alert = page.getByRole("alert"); | ||
const errorText = alert.getByText("An error 500 occurred on server"); | ||
await expect(errorText).toBeVisible(); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import React, { ErrorInfo } from "react"; | ||
import Error from "../pages/_error"; | ||
|
||
interface ErrorBoundaryProps { | ||
children?: React.ReactNode; | ||
} | ||
|
||
interface ErrorBoundaryState { | ||
error?: Error; | ||
info?: React.ErrorInfo; | ||
} | ||
|
||
class ErrorBoundary extends React.Component< | ||
ErrorBoundaryProps, | ||
ErrorBoundaryState | ||
> { | ||
constructor(props) { | ||
super(props); | ||
|
||
this.state = { error: undefined, info: undefined }; | ||
} | ||
|
||
static getDerivedStateFromError(error: Error) { | ||
return { error }; | ||
} | ||
|
||
componentDidCatch(error: Error, errorInfo: ErrorInfo) { | ||
// TODO: add logging to New Relic | ||
this.setState({ | ||
error, | ||
info: errorInfo, | ||
}); | ||
} | ||
|
||
render() { | ||
const { error } = this.state; | ||
|
||
if (error) { | ||
return <Error statusCode={error} />; | ||
} | ||
|
||
return this.props.children; | ||
} | ||
} | ||
|
||
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
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