mirror of
https://github.com/hamster1963/nezha-dash.git
synced 2025-04-24 21:10:45 +08:00
fix: add server-side error log
This commit is contained in:
parent
7656bbe319
commit
3e9a6e1eef
@ -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 (
|
||||
<>
|
||||
|
@ -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) {
|
||||
@ -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 });
|
||||
});
|
||||
|
@ -75,6 +75,7 @@ export async function GetNezhaData() {
|
||||
|
||||
return data;
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return error;
|
||||
}
|
||||
}
|
||||
@ -111,6 +112,7 @@ export async function GetServerMonitor({ server_id }: { server_id: number }) {
|
||||
}
|
||||
return monitorData;
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return error;
|
||||
}
|
||||
}
|
||||
@ -163,6 +165,7 @@ export async function GetServerDetail({ server_id }: { server_id: number }) {
|
||||
|
||||
return detailData;
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return error;
|
||||
}
|
||||
}
|
||||
|
27
lib/utils.ts
27
lib/utils.ts
@ -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();
|
||||
|
Loading…
Reference in New Issue
Block a user