File tree Expand file tree Collapse file tree 1 file changed +19
-5
lines changed
packages/try-catch-tuple/src Expand file tree Collapse file tree 1 file changed +19
-5
lines changed Original file line number Diff line number Diff line change @@ -148,15 +148,29 @@ tryCatch.sync = tryCatchSync;
148
148
tryCatch . async = tryCatchAsync ;
149
149
tryCatch . errors = tryCatchErrors ;
150
150
151
+ // Handles a raw unknown error, ensures it's wrapped and annotated
151
152
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 } ) ;
156
156
157
157
if ( operationName ) {
158
- processedError . message = `Operation " ${ operationName } " failed: ${ processedError . message } ` ;
158
+ annotateErrorMessage ( processedError , operationName ) ;
159
159
}
160
160
161
161
return [ null , processedError ] as Failure < typeof processedError > ;
162
162
}
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
+ }
You can’t perform that action at this time.
0 commit comments