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";
|
const disableCartoon = getEnv("NEXT_PUBLIC_DisableCartoon") === "true";
|
||||||
|
|
||||||
if (error)
|
if (error) {
|
||||||
return (
|
return (
|
||||||
<div className="flex flex-col items-center justify-center">
|
<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>
|
<p className="text-sm font-medium opacity-40">{t("error_message")}</p>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
|
@ -10,6 +10,7 @@ export const dynamic = "force-dynamic";
|
|||||||
interface NezhaDataResponse {
|
interface NezhaDataResponse {
|
||||||
error?: string;
|
error?: string;
|
||||||
data?: ServerApi;
|
data?: ServerApi;
|
||||||
|
cause?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const GET = auth(async function GET(req) {
|
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;
|
const response = (await GetNezhaData()) as NezhaDataResponse;
|
||||||
if (response.error) {
|
if (response.error) {
|
||||||
console.log(response.error);
|
|
||||||
return NextResponse.json({ error: response.error }, { status: 400 });
|
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 });
|
return NextResponse.json(response, { status: 200 });
|
||||||
});
|
});
|
||||||
|
@ -75,6 +75,7 @@ export async function GetNezhaData() {
|
|||||||
|
|
||||||
return data;
|
return data;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
return error;
|
return error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -111,6 +112,7 @@ export async function GetServerMonitor({ server_id }: { server_id: number }) {
|
|||||||
}
|
}
|
||||||
return monitorData;
|
return monitorData;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
return error;
|
return error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -163,6 +165,7 @@ export async function GetServerDetail({ server_id }: { server_id: number }) {
|
|||||||
|
|
||||||
return detailData;
|
return detailData;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
return error;
|
return error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
27
lib/utils.ts
27
lib/utils.ts
@ -71,19 +71,20 @@ export const fetcher = (url: string) =>
|
|||||||
throw err;
|
throw err;
|
||||||
});
|
});
|
||||||
|
|
||||||
export const nezhaFetcher = (url: string) =>
|
export const nezhaFetcher = async (url: string) => {
|
||||||
fetch(url)
|
const res = await fetch(url);
|
||||||
.then((res) => {
|
|
||||||
if (!res.ok) {
|
if (!res.ok) {
|
||||||
throw new Error(res.statusText);
|
const error = new Error("An error occurred while fetching the data.");
|
||||||
}
|
// @ts-ignore
|
||||||
return res.json();
|
error.info = await res.json();
|
||||||
})
|
// @ts-ignore
|
||||||
.then((data) => data)
|
error.status = res.status;
|
||||||
.catch((err) => {
|
throw error;
|
||||||
console.error(err);
|
}
|
||||||
throw err;
|
|
||||||
});
|
return res.json();
|
||||||
|
};
|
||||||
|
|
||||||
export function formatRelativeTime(timestamp: number): string {
|
export function formatRelativeTime(timestamp: number): string {
|
||||||
const now = Date.now();
|
const now = Date.now();
|
||||||
|
Loading…
Reference in New Issue
Block a user