You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
38 lines
994 B
38 lines
994 B
import log4js from "logger";
|
|
|
|
const logger = log4js.getLogger("ERROR");
|
|
|
|
interface ClientErrorReport {
|
|
message: string;
|
|
stack?: string;
|
|
url?: string;
|
|
line?: number;
|
|
column?: number;
|
|
userAgent?: string;
|
|
tags?: string[];
|
|
}
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
const body = await readBody<ClientErrorReport>(event).catch(() => null);
|
|
if (!body || typeof body.message !== "string") {
|
|
return R.success({ ok: false });
|
|
}
|
|
|
|
const requestId = event.context.requestId ?? "(no-request-id)";
|
|
const ip = getRequestIP(event, { xForwardedFor: true }) ?? "unknown";
|
|
const tags = Array.isArray(body.tags) ? body.tags.join(",") : "client";
|
|
|
|
logger.error(
|
|
`[${requestId}]`,
|
|
`[CLIENT-ERROR]`,
|
|
`[${tags}]`,
|
|
`url=${body.url ?? "-"}`,
|
|
`ip=${ip}`,
|
|
`ua=${body.userAgent ?? "-"}`,
|
|
"\n",
|
|
body.message,
|
|
body.stack ?? "",
|
|
);
|
|
|
|
return R.success({ ok: true });
|
|
});
|
|
|