Skip to content

Commit d3ecd0c

Browse files
committed
fix: add defensive null checks to isTransientError function
Signed-off-by: leocavalcante <leo@cavalcante.dev>
1 parent c9a4b77 commit d3ecd0c

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

src/paths.mjs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff 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
*/
184184
export function isTransientError(error) {
185+
if (!error || typeof error.code !== "string") {
186+
return false
187+
}
185188
return TRANSIENT_ERROR_CODES.includes(error.code)
186189
}
187190

tests/paths.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff 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", () => {

0 commit comments

Comments
 (0)