File tree Expand file tree Collapse file tree 2 files changed +31
-1
lines changed
Expand file tree Collapse file tree 2 files changed +31
-1
lines changed Original file line number Diff line number Diff line change @@ -179,9 +179,12 @@ export const TRANSIENT_ERROR_CODES = ["EAGAIN", "EBUSY"]
179179 * Checks if an error is a transient error that may succeed on retry.
180180 *
181181 * @param {Error & {code?: string} } error - The error to check
182- * @returns {boolean } True if the error is transient
182+ * @returns {boolean } True if the error is transient, false for invalid input
183183 */
184184export function isTransientError ( error ) {
185+ if ( ! error || typeof error . code !== "string" ) {
186+ return false
187+ }
185188 return TRANSIENT_ERROR_CODES . includes ( error . code )
186189}
187190
Original file line number Diff line number Diff line change @@ -599,6 +599,33 @@ This is a test agent that handles various tasks for you.
599599 const error = Object . assign ( new Error ( "EACCES" ) , { code : "EACCES" } )
600600 expect ( isTransientError ( error ) ) . toBe ( false )
601601 } )
602+
603+ it ( "should return false for null error" , ( ) => {
604+ expect ( isTransientError ( null as unknown as Error ) ) . toBe ( false )
605+ } )
606+
607+ it ( "should return false for undefined error" , ( ) => {
608+ expect ( isTransientError ( undefined as unknown as Error ) ) . toBe ( false )
609+ } )
610+
611+ it ( "should return false when error.code is undefined" , ( ) => {
612+ const error = new Error ( "No code property" )
613+ expect ( isTransientError ( error ) ) . toBe ( false )
614+ } )
615+
616+ it ( "should return false when error.code is null" , ( ) => {
617+ const error = Object . assign ( new Error ( "Null code" ) , { code : null } )
618+ expect ( isTransientError ( error as unknown as Error & { code ?: string } ) ) . toBe ( false )
619+ } )
620+
621+ it ( "should return false when error.code is a number" , ( ) => {
622+ const error = Object . assign ( new Error ( "Number code" ) , { code : 123 } )
623+ expect ( isTransientError ( error as unknown as Error & { code ?: string } ) ) . toBe ( false )
624+ } )
625+
626+ it ( "should return false when error is an empty object" , ( ) => {
627+ expect ( isTransientError ( { } as unknown as Error ) ) . toBe ( false )
628+ } )
602629 } )
603630
604631 describe ( "retryOnTransientError" , ( ) => {
You can’t perform that action at this time.
0 commit comments