Skip to content

Commit 643d845

Browse files
committed
Refactor loader functions to improve error handling and simplify response structure in package source tab
1 parent 5ac0d36 commit 643d845

File tree

1 file changed

+39
-89
lines changed

1 file changed

+39
-89
lines changed

apps/cyberstorm-remix/app/p/tabs/Source/Source.tsx

Lines changed: 39 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -29,118 +29,64 @@ type PackageListingOutletContext = OutletContextShape & {
2929
packageDownloadUrl?: string;
3030
};
3131

32-
type ResultType = {
33-
status: string | null;
34-
message?: string;
35-
source?:
36-
| Awaited<ReturnType<typeof getPackageSource>>
37-
| ReturnType<typeof getPackageSource>;
38-
};
39-
4032
export async function loader({ params }: LoaderFunctionArgs) {
4133
if (params.namespaceId && params.packageId) {
42-
const publicEnvVariables = getPublicEnvVariables(["VITE_API_URL"]);
43-
const dapper = new DapperTs(() => {
44-
return {
45-
apiHost: publicEnvVariables.VITE_API_URL,
46-
sessionId: undefined,
47-
};
48-
});
49-
let result: ResultType = {
50-
status: null,
51-
source: undefined,
52-
message: undefined,
53-
};
34+
const { dapper } = getLoaderTools();
5435
try {
5536
const source = await dapper.getPackageSource(
5637
params.namespaceId,
5738
params.packageId
5839
);
59-
result = {
60-
status: null,
61-
source: source,
62-
message: undefined,
40+
41+
return {
42+
source,
6343
};
6444
} catch (error) {
65-
if (isApiError(error)) {
66-
if (error.response.status > 400) {
67-
result = {
68-
status: "error",
69-
source: undefined,
70-
message: `Failed to load source: ${error.message}`,
71-
};
72-
} else {
73-
throw error;
74-
}
75-
} else {
76-
throw error;
77-
}
45+
handleLoaderError(error, {
46+
mappings: [
47+
createNotFoundMapping(
48+
"Source not available.",
49+
"We could not find the requested package source."
50+
),
51+
],
52+
});
7853
}
79-
return result;
8054
}
81-
return {
82-
status: "error",
83-
message: "Failed to load source",
84-
source: undefined,
85-
};
55+
throwUserFacingPayloadResponse({
56+
headline: "Source not available.",
57+
description: "We could not find the requested package source.",
58+
category: "not_found",
59+
status: 404,
60+
});
8661
}
8762

88-
export async function clientLoader({ params }: LoaderFunctionArgs) {
63+
export function clientLoader({ params }: LoaderFunctionArgs) {
8964
if (params.namespaceId && params.packageId) {
90-
const tools = getSessionTools();
91-
const dapper = new DapperTs(() => {
92-
return {
93-
apiHost: tools.getConfig().apiHost,
94-
sessionId: tools.getConfig().sessionId,
95-
};
96-
});
97-
let result: ResultType = {
98-
status: null,
99-
source: undefined,
100-
message: undefined,
65+
const { dapper } = getLoaderTools();
66+
const source = dapper.getPackageSource(
67+
params.namespaceId,
68+
params.packageId
69+
);
70+
71+
return {
72+
source,
10173
};
102-
try {
103-
const source = dapper.getPackageSource(
104-
params.namespaceId,
105-
params.packageId
106-
);
107-
result = {
108-
status: null,
109-
source: source,
110-
message: undefined,
111-
};
112-
} catch (error) {
113-
result = {
114-
status: "error",
115-
source: undefined,
116-
message: "Failed to load source",
117-
};
118-
throw error;
119-
}
120-
return result;
12174
}
122-
return {
123-
status: "error",
124-
message: "Failed to load source",
125-
source: undefined,
126-
};
75+
throwUserFacingPayloadResponse({
76+
headline: "Source not available.",
77+
description: "We could not find the requested package source.",
78+
category: "not_found",
79+
status: 404,
80+
});
12781
}
12882

12983
export default function Source() {
130-
const { status, message, source } = useLoaderData<
131-
typeof loader | typeof clientLoader
132-
>();
84+
const { source } = useLoaderData<typeof loader | typeof clientLoader>();
13385
const outletContext = useOutletContext() as PackageListingOutletContext;
13486

135-
if (status === "error") {
136-
return <div>{message}</div>;
137-
}
13887
return (
13988
<Suspense fallback={<SkeletonBox className="package-source__skeleton" />}>
140-
<Await
141-
resolve={source}
142-
errorElement={<div>Error occurred while loading source</div>}
143-
>
89+
<Await resolve={source} errorElement={<NimbusAwaitErrorElement />}>
14490
{(resolvedValue) => {
14591
const decompilations = resolvedValue?.decompilations ?? [];
14692
const lastDecompilationDate = resolvedValue?.last_decompilation_date;
@@ -192,6 +138,10 @@ export default function Source() {
192138
);
193139
}
194140

141+
export function ErrorBoundary() {
142+
return <NimbusDefaultRouteErrorBoundary />;
143+
}
144+
195145
const DecompilationDateDisplay = (props: {
196146
lastDecompilationDate: string | null | undefined;
197147
}) => {

0 commit comments

Comments
 (0)