Skip to content

Commit

Permalink
fix: add server-side error log
Browse files Browse the repository at this point in the history
  • Loading branch information
hamster1963 committed Oct 24, 2024
1 parent 7656bbe commit 3e9a6e1
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 16 deletions.
7 changes: 5 additions & 2 deletions app/[locale]/(main)/ClientComponents/ServerOverviewClient.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@ export default function ServerOverviewClient() {
);
const disableCartoon = getEnv("NEXT_PUBLIC_DisableCartoon") === "true";

if (error)
if (error) {
return (
<div className="flex flex-col items-center justify-center">
<p className="text-sm font-medium opacity-40">{error.message}</p>
<p className="text-sm font-medium opacity-40">
Error status:{error.status} {error.info?.cause ?? error.message}
</p>
<p className="text-sm font-medium opacity-40">{t("error_message")}</p>
</div>
);
}

return (
<>
Expand Down
8 changes: 7 additions & 1 deletion app/api/server/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export const dynamic = "force-dynamic";
interface NezhaDataResponse {
error?: string;
data?: ServerApi;
cause?: string;
}

export const GET = auth(async function GET(req) {
Expand All @@ -19,8 +20,13 @@ export const GET = auth(async function GET(req) {

const response = (await GetNezhaData()) as NezhaDataResponse;
if (response.error) {
console.log(response.error);
return NextResponse.json({ error: response.error }, { status: 400 });
}
if (response.cause) {
return NextResponse.json(
{ cause: "server connect error" },
{ status: 400 },
);
}
return NextResponse.json(response, { status: 200 });
});
3 changes: 3 additions & 0 deletions lib/serverFetch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export async function GetNezhaData() {

return data;
} catch (error) {
console.error(error);
return error;
}
}
Expand Down Expand Up @@ -111,6 +112,7 @@ export async function GetServerMonitor({ server_id }: { server_id: number }) {
}
return monitorData;
} catch (error) {
console.error(error);
return error;
}
}
Expand Down Expand Up @@ -163,6 +165,7 @@ export async function GetServerDetail({ server_id }: { server_id: number }) {

return detailData;
} catch (error) {
console.error(error);
return error;
}
}
27 changes: 14 additions & 13 deletions lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,20 @@ export const fetcher = (url: string) =>
throw err;
});

export const nezhaFetcher = (url: string) =>
fetch(url)
.then((res) => {
if (!res.ok) {
throw new Error(res.statusText);
}
return res.json();
})
.then((data) => data)
.catch((err) => {
console.error(err);
throw err;
});
export const nezhaFetcher = async (url: string) => {
const res = await fetch(url);

if (!res.ok) {
const error = new Error("An error occurred while fetching the data.");
// @ts-ignore
error.info = await res.json();
// @ts-ignore
error.status = res.status;
throw error;
}

return res.json();
};

export function formatRelativeTime(timestamp: number): string {
const now = Date.now();
Expand Down

0 comments on commit 3e9a6e1

Please sign in to comment.