-
Notifications
You must be signed in to change notification settings - Fork 4
Refactor loader functions to improve error handling and simplify response structure in package source tab #1617
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
Draft
Oksamies
wants to merge
1
commit into
11-19-_ts-api-react-actions_enhance_error_handling_in_api_actions_and_hooks_with_user-facing_error_mapping
Choose a base branch
from
11-19-refactor_loader_functions_to_improve_error_handling_and_simplify_response_structure_in_package_source_tab
base: 11-19-_ts-api-react-actions_enhance_error_handling_in_api_actions_and_hooks_with_user-facing_error_mapping
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,151 +1,105 @@ | ||
| import { faClock, faDownload } from "@fortawesome/free-solid-svg-icons"; | ||
| import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; | ||
| import { | ||
| getPublicEnvVariables, | ||
| getSessionTools, | ||
| } from "cyberstorm/security/publicEnvVariables"; | ||
| NimbusAwaitErrorElement, | ||
| NimbusDefaultRouteErrorBoundary, | ||
| } from "cyberstorm/utils/errors/NimbusErrorBoundary"; | ||
| import { handleLoaderError } from "cyberstorm/utils/errors/handleLoaderError"; | ||
| import { createNotFoundMapping } from "cyberstorm/utils/errors/loaderMappings"; | ||
| import { throwUserFacingPayloadResponse } from "cyberstorm/utils/errors/userFacingErrorResponse"; | ||
| import { getLoaderTools } from "cyberstorm/utils/getLoaderTools"; | ||
| import { Suspense } from "react"; | ||
| import { Await, type LoaderFunctionArgs, useOutletContext } from "react-router"; | ||
| import { useLoaderData } from "react-router"; | ||
| import { | ||
| Await, | ||
| type LoaderFunctionArgs, | ||
| useLoaderData, | ||
| useOutletContext, | ||
| } from "react-router"; | ||
| import ago from "s-ago"; | ||
| import { type OutletContextShape } from "~/root"; | ||
| import { CodeBoxHTML } from "~/commonComponents/CodeBoxHTML/CodeBoxHTML"; | ||
| import type { OutletContextShape } from "~/root"; | ||
|
|
||
| import { | ||
| NewAlert as Alert, | ||
| Heading, | ||
| NewAlert, | ||
| NewButton, | ||
| NewIcon, | ||
| SkeletonBox, | ||
| TooltipWrapper, | ||
| } from "@thunderstore/cyberstorm"; | ||
| import { DapperTs, getPackageSource } from "@thunderstore/dapper-ts"; | ||
| import { isApiError } from "@thunderstore/thunderstore-api"; | ||
|
|
||
| import { CodeBoxHTML } from "../../../commonComponents/CodeBoxHTML/CodeBoxHTML"; | ||
| import "./Source.css"; | ||
|
|
||
| type PackageListingOutletContext = OutletContextShape & { | ||
| packageDownloadUrl?: string; | ||
| }; | ||
|
|
||
| type ResultType = { | ||
| status: string | null; | ||
| message?: string; | ||
| source?: | ||
| | Awaited<ReturnType<typeof getPackageSource>> | ||
| | ReturnType<typeof getPackageSource>; | ||
| }; | ||
|
|
||
| export async function loader({ params }: LoaderFunctionArgs) { | ||
| if (params.namespaceId && params.packageId) { | ||
| const publicEnvVariables = getPublicEnvVariables(["VITE_API_URL"]); | ||
| const dapper = new DapperTs(() => { | ||
| return { | ||
| apiHost: publicEnvVariables.VITE_API_URL, | ||
| sessionId: undefined, | ||
| }; | ||
| }); | ||
| let result: ResultType = { | ||
| status: null, | ||
| source: undefined, | ||
| message: undefined, | ||
| }; | ||
| const { dapper } = getLoaderTools(); | ||
| try { | ||
| const source = await dapper.getPackageSource( | ||
| params.namespaceId, | ||
| params.packageId | ||
| ); | ||
| result = { | ||
| status: null, | ||
| source: source, | ||
| message: undefined, | ||
|
|
||
| return { | ||
| source, | ||
| }; | ||
| } catch (error) { | ||
| if (isApiError(error)) { | ||
| if (error.response.status > 400) { | ||
| result = { | ||
| status: "error", | ||
| source: undefined, | ||
| message: `Failed to load source: ${error.message}`, | ||
| }; | ||
| } else { | ||
| throw error; | ||
| } | ||
| } else { | ||
| throw error; | ||
| } | ||
| handleLoaderError(error, { | ||
| mappings: [ | ||
| createNotFoundMapping( | ||
| "Source not available.", | ||
| "We could not find the requested package source." | ||
| ), | ||
| ], | ||
| }); | ||
| } | ||
| return result; | ||
| } | ||
| return { | ||
| status: "error", | ||
| message: "Failed to load source", | ||
| source: undefined, | ||
| }; | ||
| throwUserFacingPayloadResponse({ | ||
| headline: "Source not available.", | ||
| description: "We could not find the requested package source.", | ||
| category: "not_found", | ||
| status: 404, | ||
| }); | ||
| } | ||
|
|
||
| export async function clientLoader({ params }: LoaderFunctionArgs) { | ||
| export function clientLoader({ params }: LoaderFunctionArgs) { | ||
| if (params.namespaceId && params.packageId) { | ||
| const tools = getSessionTools(); | ||
| const dapper = new DapperTs(() => { | ||
| return { | ||
| apiHost: tools.getConfig().apiHost, | ||
| sessionId: tools.getConfig().sessionId, | ||
| }; | ||
| }); | ||
| let result: ResultType = { | ||
| status: null, | ||
| source: undefined, | ||
| message: undefined, | ||
| const { dapper } = getLoaderTools(); | ||
| const source = dapper.getPackageSource( | ||
| params.namespaceId, | ||
| params.packageId | ||
| ); | ||
|
|
||
| return { | ||
| source, | ||
| }; | ||
| try { | ||
| const source = dapper.getPackageSource( | ||
| params.namespaceId, | ||
| params.packageId | ||
| ); | ||
| result = { | ||
| status: null, | ||
| source: source, | ||
| message: undefined, | ||
| }; | ||
| } catch (error) { | ||
| result = { | ||
| status: "error", | ||
| source: undefined, | ||
| message: "Failed to load source", | ||
| }; | ||
| throw error; | ||
| } | ||
| return result; | ||
| } | ||
| return { | ||
| status: "error", | ||
| message: "Failed to load source", | ||
| source: undefined, | ||
| }; | ||
| throwUserFacingPayloadResponse({ | ||
| headline: "Source not available.", | ||
| description: "We could not find the requested package source.", | ||
| category: "not_found", | ||
| status: 404, | ||
| }); | ||
| } | ||
|
|
||
| export default function Source() { | ||
| const { status, message, source } = useLoaderData< | ||
| typeof loader | typeof clientLoader | ||
| >(); | ||
| const { source } = useLoaderData<typeof loader | typeof clientLoader>(); | ||
| const outletContext = useOutletContext() as PackageListingOutletContext; | ||
|
|
||
| if (status === "error") { | ||
| return <div>{message}</div>; | ||
| } | ||
| return ( | ||
| <Suspense fallback={<SkeletonBox className="package-source__skeleton" />}> | ||
| <Await | ||
| resolve={source} | ||
| errorElement={<div>Error occurred while loading source</div>} | ||
| > | ||
| <Await resolve={source} errorElement={<NimbusAwaitErrorElement />}> | ||
| {(resolvedValue) => { | ||
| const decompilations = resolvedValue?.decompilations ?? []; | ||
| const lastDecompilationDate = resolvedValue?.last_decompilation_date; | ||
| if (decompilations.length === 0) { | ||
| return ( | ||
| <Alert csVariant="info">Decompiled source not available.</Alert> | ||
| <NewAlert csVariant="info"> | ||
| Decompiled source not available. | ||
| </NewAlert> | ||
| ); | ||
| } | ||
| return decompilations.map((decompilation) => { | ||
|
|
@@ -171,10 +125,10 @@ export default function Source() { | |
| /> | ||
| </div> | ||
| {decompilation.is_truncated && ( | ||
| <Alert csVariant="warning"> | ||
| <NewAlert csVariant="warning"> | ||
| The result has been truncated due to the large size, | ||
| download it to view the full contents! | ||
| </Alert> | ||
| </NewAlert> | ||
| )} | ||
| <div | ||
| className="package-source__decompilations-file" | ||
|
|
@@ -191,6 +145,10 @@ export default function Source() { | |
| ); | ||
| } | ||
|
|
||
| export function ErrorBoundary() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I didn't get any response the last time I suggested using |
||
| return <NimbusDefaultRouteErrorBoundary />; | ||
| } | ||
|
|
||
| const DecompilationDateDisplay = (props: { | ||
| lastDecompilationDate: string | null | undefined; | ||
| }) => { | ||
|
|
||
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.
packageListing.tsxseems to always render link to this tab even if the source doesn't exists. Not ideal UX and not up to feature parity