@@ -89,6 +89,17 @@ function isNotFoundError(error: unknown): boolean {
8989 return ! ! ( error && typeof error === 'object' && 'status' in error && error . status === 404 ) ;
9090}
9191
92+ /**
93+ * Ensure a value is an Error instance.
94+ * Converts non-Error values to Error instances with a string representation.
95+ *
96+ * @param error The error value to convert
97+ * @returns An Error instance
98+ */
99+ function ensureError ( error : unknown ) : Error {
100+ return error instanceof Error ? error : new Error ( String ( error ) ) ;
101+ }
102+
92103/**
93104 * Call the OpenAI moderation API.
94105 *
@@ -168,12 +179,10 @@ export const moderationCheck: CheckFn<ModerationContext, string, ModerationConfi
168179 return {
169180 tripwireTriggered : false ,
170181 executionFailed : true ,
171- originalException : fallbackError instanceof Error
172- ? fallbackError
173- : new Error ( String ( fallbackError ) ) ,
182+ originalException : ensureError ( fallbackError ) ,
174183 info : {
175184 checked_text : data ,
176- error : fallbackError instanceof Error ? fallbackError . message : String ( fallbackError ) ,
185+ error : ensureError ( fallbackError ) . message ,
177186 } ,
178187 } ;
179188 }
@@ -183,10 +192,10 @@ export const moderationCheck: CheckFn<ModerationContext, string, ModerationConfi
183192 return {
184193 tripwireTriggered : false ,
185194 executionFailed : true ,
186- originalException : error instanceof Error ? error : new Error ( String ( error ) ) ,
195+ originalException : ensureError ( error ) ,
187196 info : {
188197 checked_text : data ,
189- error : error instanceof Error ? error . message : String ( error ) ,
198+ error : ensureError ( error ) . message ,
190199 } ,
191200 } ;
192201 }
@@ -241,10 +250,10 @@ export const moderationCheck: CheckFn<ModerationContext, string, ModerationConfi
241250 return {
242251 tripwireTriggered : false ,
243252 executionFailed : true ,
244- originalException : error instanceof Error ? error : new Error ( String ( error ) ) ,
253+ originalException : ensureError ( error ) ,
245254 info : {
246255 checked_text : data ,
247- error : error instanceof Error ? error . message : 'Moderation API call failed' ,
256+ error : ensureError ( error ) . message ,
248257 } ,
249258 } ;
250259 }
0 commit comments