mirror of
https://github.com/ZHANGTIANYAO1/teamspeak-music-bot.git
synced 2026-12-01 02:12:50 +08:00
Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4ee1e6f812 |
No files matched your search
@@ -0,0 +1,30 @@
|
||||
import { spawnSync } from "node:child_process";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
describe("application log errors", () => {
|
||||
it("preserves HTTP diagnostics while omitting Axios request and response data", () => {
|
||||
const child = spawnSync(process.execPath, [
|
||||
"--import", "tsx", "--input-type=module", "--eval", `
|
||||
import { AxiosError } from "axios";
|
||||
import { createLogger } from "./src/logger.ts";
|
||||
|
||||
const config = { params: { cookie: "sample-request-cookie" } };
|
||||
const response = { status: 400, data: { code: -460, cookie: "sample-response-cookie" } };
|
||||
const err = new AxiosError("sample-sensitive-url", "ERR_BAD_REQUEST", config, undefined, response);
|
||||
const log = createLogger().child({ component: "test" });
|
||||
log.warn({ err }, "Provider failed");
|
||||
log.error({ reason: err }, "Unhandled rejection");
|
||||
log.error(new Error("ordinary error"));
|
||||
`,
|
||||
], { cwd: fileURLToPath(new URL("..", import.meta.url)), encoding: "utf8" });
|
||||
|
||||
expect(child.status, child.stderr).toBe(0);
|
||||
const [provider, rejection, ordinary] = child.stdout.trim().split("\n").map(line => JSON.parse(line));
|
||||
const expected = { type: "AxiosError", code: "ERR_BAD_REQUEST", status: 400, upstreamCode: -460 };
|
||||
expect(provider.err).toMatchObject(expected);
|
||||
expect(rejection.reason).toMatchObject(expected);
|
||||
expect(child.stdout).not.toContain("sample-");
|
||||
expect(ordinary.err.message).toBe("ordinary error");
|
||||
});
|
||||
});
|
||||
+20
-2
@@ -1,12 +1,30 @@
|
||||
import pino from "pino";
|
||||
import { isAxiosError } from "axios";
|
||||
import { mkdirSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
|
||||
export type Logger = pino.Logger;
|
||||
|
||||
function serializeHttpError(error: unknown): unknown {
|
||||
if (!isAxiosError<{ code?: unknown }>(error)) {
|
||||
return error instanceof Error ? pino.stdSerializers.err(error) : error;
|
||||
}
|
||||
const upstreamCode = error.response?.data?.code;
|
||||
return {
|
||||
type: "AxiosError",
|
||||
code: error.code,
|
||||
status: error.response?.status ?? error.status,
|
||||
upstreamCode: typeof upstreamCode === "number" ? upstreamCode : undefined,
|
||||
};
|
||||
}
|
||||
|
||||
// Axios errors include the original request configuration and may contain
|
||||
// session cookies, authorization headers or secrets in the request URL.
|
||||
const serializers = { err: serializeHttpError, reason: serializeHttpError };
|
||||
|
||||
export function createLogger(logDir?: string): Logger {
|
||||
if (!logDir) {
|
||||
return pino({ level: "info" });
|
||||
return pino({ level: "info", serializers });
|
||||
}
|
||||
|
||||
mkdirSync(logDir, { recursive: true });
|
||||
@@ -32,5 +50,5 @@ export function createLogger(logDir?: string): Logger {
|
||||
console.error("Logger transport error:", err);
|
||||
});
|
||||
|
||||
return pino({ level: "debug" }, transport);
|
||||
return pino({ level: "debug", serializers }, transport);
|
||||
}
|
||||
Reference in new issue
Block a user