From a8ccffdd7c7a91300c5bd25b66607b4e17eaf474 Mon Sep 17 00:00:00 2001 From: ZeyuFu Date: Sun, 17 May 2026 11:05:01 -0400 Subject: [PATCH] style(runtime-fallback): add explicit optional chain on .replace per review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Addresses cubic-dev-ai P1 finding on #4113 (#4113 review). The original chain `extractErrorName(error)?.toLowerCase().replace(...)` is semantically safe — JavaScript optional chaining short-circuits the ENTIRE access chain when the head returns null/undefined, so when `extractErrorName` returns undefined the whole expression evaluates to undefined without ever reaching `.replace()`. Verified empirically via `const x = undefined; x?.toLowerCase().replace(/_/g, "")` returns undefined with no crash. Applying the suggested defensive `?.` before `.replace` anyway, since it is semantically a no-op and explicit chaining at each hop is easier for static analyzers to reason about. Co-Authored-By: Claude Opus 4.7 (1M context) --- src/hooks/runtime-fallback/error-classifier.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hooks/runtime-fallback/error-classifier.ts b/src/hooks/runtime-fallback/error-classifier.ts index 1f18b93ad..6ba155101 100644 --- a/src/hooks/runtime-fallback/error-classifier.ts +++ b/src/hooks/runtime-fallback/error-classifier.ts @@ -109,7 +109,7 @@ export function classifyErrorType(error: unknown): string | undefined { // Normalize by stripping underscores and dashes so snake_case / kebab-case // provider error names (e.g. "insufficient_quota", "RESOURCE_EXHAUSTED") // match the existing alphanumeric .includes() checks below. - const errorName = extractErrorName(error)?.toLowerCase().replace(/[_-]/g, "") + const errorName = extractErrorName(error)?.toLowerCase()?.replace(/[_-]/g, "") if ( errorName?.includes("ailoadapikeyerror") ||