mirror of
https://github.com/snapotter-hq/SnapOtter.git
synced 2026-08-03 07:46:42 +02:00
fix(image-tools): surface Sharp encode failures instead of "Error: Error" (#534)
Wrap convert and gif-tools process functions so a Sharp .toBuffer() failure carries an authored SafeError title (and the original as cause) rather than a scrubbed "Error: Error".
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
import { isSafeMessageError, isToolInputError, SafeError } from "@snapotter/shared";
|
||||
import type { ToolProcessCtx } from "../routes/tool-factory.js";
|
||||
|
||||
type ImageProcess<T> = (
|
||||
inputBuffer: Buffer,
|
||||
settings: T,
|
||||
filename: string,
|
||||
ctx?: ToolProcessCtx,
|
||||
) => Promise<{ buffer: Buffer; filename: string; contentType: string }>;
|
||||
|
||||
/**
|
||||
* Wrap an image tool's process function so an otherwise-opaque failure (most
|
||||
* often a Sharp `.toBuffer()` that throws an empty-message Error) surfaces a
|
||||
* safe, authored title instead of "Error: Error".
|
||||
*
|
||||
* The API's Sentry scrubber replaces any non-SafeError message with a type-only
|
||||
* value (see `rebuildErrorValue`), so a bare Sharp failure is undiagnosable.
|
||||
* Re-throwing as a SafeError makes the title survive while the original error is
|
||||
* kept as `cause`, preserving its stack and exact location. Errors we already
|
||||
* author (SafeError) or that flag bad user input (ToolInputError) pass through
|
||||
* untouched so their class is not masked.
|
||||
*/
|
||||
export function withImageEncodeContext<T>(
|
||||
message: string,
|
||||
codeOf: (settings: T) => string,
|
||||
process: ImageProcess<T>,
|
||||
): ImageProcess<T> {
|
||||
return async (inputBuffer, settings, filename, ctx) => {
|
||||
try {
|
||||
return await process(inputBuffer, settings, filename, ctx);
|
||||
} catch (err) {
|
||||
if (isSafeMessageError(err) || isToolInputError(err)) throw err;
|
||||
throw new SafeError(message, {
|
||||
kind: "bug",
|
||||
code: codeOf(settings),
|
||||
cause: err instanceof Error ? err : new Error(String(err)),
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user