Skip to content

Commit 275d88d

Browse files
committed
refactor: improve error handling and message annotation
1 parent 1fbc619 commit 275d88d

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

packages/try-catch-tuple/src/tryCatch.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,15 +148,29 @@ tryCatch.sync = tryCatchSync;
148148
tryCatch.async = tryCatchAsync;
149149
tryCatch.errors = tryCatchErrors;
150150

151+
// Handles a raw unknown error, ensures it's wrapped and annotated
151152
function handleError(rawError: unknown, operationName?: string) {
152-
const processedError =
153-
rawError instanceof Error
154-
? rawError
155-
: new Error(String(rawError), { cause: rawError });
153+
const processedError = isError(rawError)
154+
? rawError
155+
: new Error(String(rawError), { cause: rawError });
156156

157157
if (operationName) {
158-
processedError.message = `Operation "${operationName}" failed: ${processedError.message}`;
158+
annotateErrorMessage(processedError, operationName);
159159
}
160160

161161
return [null, processedError] as Failure<typeof processedError>;
162162
}
163+
164+
// Utility to prefix error messages with operation context
165+
function annotateErrorMessage<E extends Error>(
166+
error: E,
167+
operationName: string,
168+
): void {
169+
error.message = `Operation "${operationName}" failed: ${error.message}`;
170+
}
171+
172+
// Type guards
173+
function isError(error: unknown): error is Error {
174+
if (!error) return false;
175+
return Error?.isError?.(error) ?? error instanceof Error;
176+
}

0 commit comments

Comments
 (0)