Skip to content
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

Error message update #63

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/app/[ecosystem]/[...packageName]/error.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export default function Error({
return (
<div>
<h2>Something went wrong!</h2>
<p>{error.message}</p>
<button
onClick={
// Attempt to recover by trying to re-render the segment
Expand Down
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>
);
}
23 changes: 20 additions & 3 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,11 +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) {
throw new Error("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