mirror of
https://seed.radicle.dev/rad:z4V1sjrXqjvFdnCUbxPFqd5p4DtH5.git
synced 2026-11-21 06:10:44 +08:00
71 lines
1.9 KiB
TypeScript
71 lines
1.9 KiB
TypeScript
import type { ErrorRoute, NotFoundRoute } from "@app/lib/router/definitions";
|
|
import type { ProjectRoute } from "@app/views/projects/router";
|
|
|
|
import { baseUrlToString } from "@app/lib/utils";
|
|
import { ResponseParseError, ResponseError } from "@http-client/lib/fetcher";
|
|
|
|
export function handleError(
|
|
error: Error | ResponseParseError | ResponseError,
|
|
route: ProjectRoute,
|
|
): NotFoundRoute | ErrorRoute {
|
|
const url = baseUrlToString(route.node);
|
|
if (error instanceof ResponseError && error.status === 404) {
|
|
let subject;
|
|
|
|
if (route.resource === "project.commit") {
|
|
subject = "Commit";
|
|
} else if (route.resource === "project.issue") {
|
|
subject = "Issue";
|
|
} else if (route.resource === "project.patch") {
|
|
subject = "Patch";
|
|
} else {
|
|
subject = "Repository";
|
|
}
|
|
|
|
return {
|
|
resource: "notFound",
|
|
params: { title: `${subject} not found` },
|
|
};
|
|
} else if (error instanceof ResponseError) {
|
|
return {
|
|
resource: "error",
|
|
params: {
|
|
error,
|
|
title: "Could not load this repository",
|
|
description: `Make sure you are able to connect to the seed <a href="${url}">${url}</a>.`,
|
|
},
|
|
};
|
|
} else if (error instanceof ResponseParseError) {
|
|
return {
|
|
resource: "error",
|
|
params: {
|
|
error,
|
|
title: "Could not parse the request",
|
|
description: error.description,
|
|
},
|
|
};
|
|
} else {
|
|
return {
|
|
resource: "error",
|
|
params: {
|
|
error,
|
|
title: "Could not load this repository",
|
|
description:
|
|
"You stumbled on an unknown error, we aren't exactly sure what happened.",
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
export function unreachableError(): NotFoundRoute | ErrorRoute {
|
|
return {
|
|
resource: "error",
|
|
params: {
|
|
error: undefined,
|
|
title: "Could not load this route",
|
|
description:
|
|
"You stumbled on an unknown error, we aren't exactly sure what happened.",
|
|
},
|
|
};
|
|
}
|