-
Notifications
You must be signed in to change notification settings - Fork 3
Handle 404 errors from API during server render #1678
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
18 changes: 18 additions & 0 deletions
18
frontends/main/src/app-pages/ErrorPage/GlobalErrorPage.tsx
This file contains hidden or 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 @@ | ||
| "use client" | ||
|
|
||
| import React from "react" | ||
| import ErrorPageTemplate from "./ErrorPageTemplate" | ||
| import { Typography } from "ol-components" | ||
|
|
||
| const GlobalErrorPage = ({ error }: { error: Pick<Error, "message"> }) => { | ||
| return ( | ||
| <ErrorPageTemplate title="Unexpected Error"> | ||
| <Typography variant="h3" component="h1"> | ||
| Unexpected Error | ||
| </Typography> | ||
| {error.message || ""} | ||
| </ErrorPageTemplate> | ||
| ) | ||
| } | ||
|
|
||
| export default GlobalErrorPage |
This file contains hidden or 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 hidden or 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,19 @@ | ||
| "use client" | ||
|
|
||
| /* This is the catch-all error page that receives errors from server rendered root layout | ||
| * components and metadata. | ||
| * It is only enabled in production so that in development we see the Next.js error overlay. | ||
| * It is passed an error object as an argument, though this has been stripped of everything except | ||
| * the message and a digest for server logs correlation; to prevent leaking anything to the client | ||
| * | ||
| * https://nextjs.org/docs/app/building-your-application/routing/error-handling#handling-errors-in-root-layouts | ||
| */ | ||
|
|
||
| import React from "react" | ||
| import GlobalErrorPage from "@/app-pages/ErrorPage/GlobalErrorPage" | ||
|
|
||
| const GlobalError = ({ error }: { error: Error }) => { | ||
| return <GlobalErrorPage error={error} /> | ||
| } | ||
|
|
||
| export default GlobalError |
This file contains hidden or 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,39 @@ | ||
| import type { AxiosError } from "axios" | ||
| import handleNotFound from "./handleNotFound" | ||
| import { nextNavigationMocks } from "ol-test-utilities/mocks/nextNavigation" | ||
|
|
||
| describe("Handle not found wrapper utility", () => { | ||
| test("Should call notFound() for errors with status 404", async () => { | ||
| const error: Partial<AxiosError> = { | ||
| status: 404, | ||
| message: "Not Found", | ||
| } | ||
|
|
||
| const promise = Promise.reject(error) | ||
|
|
||
| await handleNotFound(promise) | ||
|
|
||
| expect(nextNavigationMocks.notFound).toHaveBeenCalled() | ||
| }) | ||
|
|
||
| test("Should not call notFound() for success and return result", async () => { | ||
| const resolvedValue = { data: "success" } | ||
| const promise = Promise.resolve(resolvedValue) | ||
|
|
||
| const result = await handleNotFound(promise) | ||
|
|
||
| expect(result).toEqual(resolvedValue) | ||
| expect(nextNavigationMocks.notFound).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| test("Should rethrow non 404 errors", async () => { | ||
| const error = new Error("Something went wrong") | ||
|
|
||
| const promise = Promise.reject(error) | ||
|
|
||
| await expect(handleNotFound(promise)).rejects.toThrow( | ||
| "Something went wrong", | ||
| ) | ||
| expect(nextNavigationMocks.notFound).not.toHaveBeenCalled() | ||
| }) | ||
| }) |
This file contains hidden or 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,24 @@ | ||
| import type { AxiosError } from "axios" | ||
| import { notFound } from "next/navigation" | ||
|
|
||
| /* This is intended to wrap API calls that fetch resources during server render, | ||
| * such as to gather metadata for the learning resource drawer. | ||
| * | ||
| * The ./app/global-error.tsx boundary for root layout errors is only supplied the | ||
| * error message so we cannot determine that it is a 404 to show the NotFoundPage. | ||
| * Instead we must handle at each point of use so need a utility function. Below we | ||
| * use next/navigation's notFound() to render ./app/not-found.tsx | ||
| */ | ||
|
|
||
| const handleNotFound = async <T>(promise: Promise<T | never>): Promise<T> => { | ||
| try { | ||
| return await promise | ||
| } catch (error) { | ||
| if ((error as AxiosError).status === 404) { | ||
| return notFound() | ||
| } | ||
| throw error | ||
| } | ||
| } | ||
|
|
||
| export default handleNotFound |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These were colliding:
After:
