Files
SnapOtter/packages/shared/src/tool-errors.ts
T
SnapOtterandGitHub ae6a4c8b7c fix: error-only Sentry telemetry, storm-proof capture, and crash fixes (#476)
Removes Sentry tracing entirely (BullMQ idle polling burned 4.8M transactions in 2 days at the baked 0.1 rate), decouples PostHog sampling, and replaces the type-only error scrub with a vetted-field sanitizer plus SafeError/ToolInputError contracts. One classified capture path with per-signature throttles and a per-process ceiling makes storms impossible (NODE-1E was 4,541 events from one 30s loop). Browser errors move to a dedicated web Sentry project with their own source maps. Adds the SNAPOTTER_TELEMETRY runtime kill switch and silences test fleets.

Crash fixes: remote 204/304 SSRF process kill (NODE-20), conversion-preset boot crash loop (NODE-21), Redis version preflight + unhandled subscribe rejection (NODE-1T), Sign PDF on plain-http origins (NODE-1K/1M), wavesurfer/pdf.js teardown rejections (NODE-1P/1N), bundle-import ZlibError to 400 (NODE-1Z), chart-maker input errors declassified (NODE-1H/1J), asset requests skip the session DB lookup (NODE-1D).
2026-07-10 21:41:49 +08:00

61 lines
2.2 KiB
TypeScript

/**
* Error classes shared by api, web, and the engine packages.
*
* SafeError: an error whose message was AUTHORED BY US and is safe to send to
* Sentry verbatim. RULE: the message must be a CONSTANT string; anything
* variable (exit codes, versions, counts) goes into `code` so Sentry grouping
* stays stable. Detection is by marker property, not instanceof, so it
* survives error copying across module boundaries.
*
* ToolInputError: the user's input was the problem (bad CSV, corrupt media).
* Never reported to Sentry. Engine packages can import these helpers
* directly; the raw marker form Object.assign(err, { isToolInputError: true })
* remains the wire format for contexts where an import is undesirable, and
* because instanceof is brittle across duplicate module instances.
*/
export type SafeErrorKind = "operational" | "bug";
export class SafeError extends Error {
readonly isSafeMessage = true;
readonly kind: SafeErrorKind;
readonly code?: string;
readonly statusCode?: number;
constructor(
message: string,
opts: { kind?: SafeErrorKind; code?: string; statusCode?: number; cause?: unknown } = {},
) {
super(message, opts.cause !== undefined ? { cause: opts.cause } : undefined);
this.name = "SafeError";
this.kind = opts.kind ?? "operational";
this.code = opts.code;
this.statusCode = opts.statusCode;
}
}
/**
* Marker-detected errors that were copied across module boundaries may lack
* `kind`, `code`, or `statusCode`, so consumers must tolerate their absence.
*/
export function isSafeMessageError(err: unknown): err is SafeError {
return err instanceof Error && (err as { isSafeMessage?: unknown }).isSafeMessage === true;
}
export class ToolInputError extends Error {
readonly isToolInputError = true;
readonly statusCode = 400;
constructor(message: string) {
super(message);
this.name = "ToolInputError";
}
}
export function isToolInputError(err: unknown): err is Error & { isToolInputError: true } {
return err instanceof Error && (err as { isToolInputError?: unknown }).isToolInputError === true;
}
export function markToolInputError<E extends Error>(err: E): E {
return Object.assign(err, { isToolInputError: true });
}