Skip to content

Commit

Permalink
error message component
Browse files Browse the repository at this point in the history
  • Loading branch information
karamba228 committed Feb 17, 2025
1 parent ea76b32 commit a5e135a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
6 changes: 6 additions & 0 deletions src/app/[ecosystem]/[...packageName]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import React from "react";
import { mdiAlert, mdiCircleSmall } from "@mdi/js";
import Icon from "@mdi/react";
import { notFound } from "next/navigation";
import ErrorMessage from "@/components/ErrorMessage";

import Risk from "@/components/Risk";
import Maturity from "@/components/Maturity";
Expand Down Expand Up @@ -32,6 +33,11 @@ export default async function PackageScoreComponent({ params }: Props) {
notFound();
}

if (status === "invalid_ecosystem") {
const message = `Ecosystem ${ecosystem} is not supported`;
return <ErrorMessage message={message} />;
}

return (
<div className="grid grid-cols-2 gap-4">
<div>
Expand Down
7 changes: 7 additions & 0 deletions src/components/ErrorMessage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default function ErrorMessage({ message }: { message: string }) {
return (
<div>
<strong>{message}</strong>
</div>
);
}
24 changes: 20 additions & 4 deletions src/utils/score_res.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@ export type PackageScore =
source: null;
score: null;
}
| {
ecosystem: string;
package_name: string;
status: "invalid_ecosystem";
package: Package;
source: null;
score: null;
}
| {
ecosystem: string;
package_name: string;
Expand All @@ -106,12 +114,20 @@ export async function fetchPackageScore(
name: string,
): Promise<PackageScore> {
const url = `${BASE_URL}/score/${ecosystem.toLowerCase()}/${name}`;
console.debug(url);
const res = await fetch(url);
if (res.status != 200) {
const error = await res.json();
throw new Error(error?.detail || "Failed to fetch package score");

if (!res.ok) {
const err = await res.json();
return {
ecosystem,
package_name: name,
status: err.error,
package: {} as Package,
source: null,
score: null,
};
}

const data = await res.json();
return data;
}
Expand Down

0 comments on commit a5e135a

Please sign in to comment.