mirror of
https://github.com/tdurieux/anonymous_github.git
synced 2026-05-24 01:54:02 +02:00
log AnonymousError context detail in serialized output
Surface the path/repo/url tied to an AnonymousError when it gets serialized for logging — previously logs only carried name, message, and httpStatus, which made file_not_found entries impossible to trace back to a specific file or repo. Extract the existing detail formatting out of toString() into a public detail() method, harden it against AnonymizedFile getters that can throw, and have serializeError include the result as a "detail" field. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
+30
-14
@@ -27,21 +27,37 @@ export default class AnonymousError extends CustomError {
|
|||||||
this.cause = opt?.cause;
|
this.cause = opt?.cause;
|
||||||
}
|
}
|
||||||
|
|
||||||
toString(): string {
|
detail(): string | undefined {
|
||||||
let out = "";
|
if (this.value == null) return undefined;
|
||||||
let detail = this.value ? JSON.stringify(this.value) : null;
|
try {
|
||||||
if (this.value instanceof Repository) {
|
if (this.value instanceof Repository) return this.value.repoId;
|
||||||
detail = this.value.repoId;
|
if (this.value instanceof AnonymizedFile) {
|
||||||
} else if (this.value instanceof AnonymizedFile) {
|
const repoId = this.value.repository?.repoId;
|
||||||
detail = `/r/${this.value.repository.repoId}/${this.value.anonymizedPath}`;
|
// anonymizedPath getter can throw if the file isn't initialized;
|
||||||
} else if (this.value instanceof GitHubRepository) {
|
// fall back to whatever path is known.
|
||||||
detail = `${this.value.fullName}`;
|
let p: string | undefined;
|
||||||
} else if (this.value instanceof User) {
|
try {
|
||||||
detail = `${this.value.username}`;
|
p = this.value.anonymizedPath;
|
||||||
} else if (this.value instanceof GitHubBase) {
|
} catch {
|
||||||
detail = `GHDownload ${this.value.data.repoId}`;
|
p = this.value.filePath;
|
||||||
|
}
|
||||||
|
return repoId ? `/r/${repoId}/${p ?? ""}` : p;
|
||||||
|
}
|
||||||
|
if (this.value instanceof GitHubRepository) return this.value.fullName;
|
||||||
|
if (this.value instanceof User) return this.value.username;
|
||||||
|
if (this.value instanceof GitHubBase) {
|
||||||
|
return `GHDownload ${this.value.data.repoId}`;
|
||||||
|
}
|
||||||
|
if (typeof this.value === "string") return this.value;
|
||||||
|
return JSON.stringify(this.value);
|
||||||
|
} catch {
|
||||||
|
return String(this.value);
|
||||||
}
|
}
|
||||||
out += this.message;
|
}
|
||||||
|
|
||||||
|
toString(): string {
|
||||||
|
let out = this.message;
|
||||||
|
const detail = this.detail();
|
||||||
if (detail) {
|
if (detail) {
|
||||||
out += `: ${detail}`;
|
out += `: ${detail}`;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -140,6 +140,7 @@ type ErrorLike = {
|
|||||||
cause?: unknown;
|
cause?: unknown;
|
||||||
request?: { url?: string; method?: string };
|
request?: { url?: string; method?: string };
|
||||||
response?: { url?: string; status?: number };
|
response?: { url?: string; status?: number };
|
||||||
|
detail?: () => string | undefined;
|
||||||
};
|
};
|
||||||
|
|
||||||
export function serializeError(err: unknown): Record<string, unknown> {
|
export function serializeError(err: unknown): Record<string, unknown> {
|
||||||
@@ -161,6 +162,14 @@ export function serializeError(err: unknown): Record<string, unknown> {
|
|||||||
// AnonymousError carries an httpStatus and an inner cause.
|
// AnonymousError carries an httpStatus and an inner cause.
|
||||||
if (typeof e.httpStatus === "number") out.httpStatus = e.httpStatus;
|
if (typeof e.httpStatus === "number") out.httpStatus = e.httpStatus;
|
||||||
if (e.code !== undefined && e.code !== e.message) out.code = e.code;
|
if (e.code !== undefined && e.code !== e.message) out.code = e.code;
|
||||||
|
if (typeof e.detail === "function") {
|
||||||
|
try {
|
||||||
|
const d = e.detail();
|
||||||
|
if (d) out.detail = d;
|
||||||
|
} catch {
|
||||||
|
/* ignore */
|
||||||
|
}
|
||||||
|
}
|
||||||
if (e.cause) out.cause = serializeError(e.cause);
|
if (e.cause) out.cause = serializeError(e.cause);
|
||||||
|
|
||||||
// Only include the stack when there's nothing else useful — avoids dumping
|
// Only include the stack when there's nothing else useful — avoids dumping
|
||||||
|
|||||||
Reference in New Issue
Block a user