(error: unknown)
| 215 | } |
| 216 | |
| 217 | function extractHttpStatusCode(error: unknown): number | null { |
| 218 | if (!error || typeof error !== "object") { |
| 219 | return null; |
| 220 | } |
| 221 | |
| 222 | const obj = error as Record<string, unknown>; |
| 223 | |
| 224 | // A few common shapes across fetch libraries / AI SDK. |
| 225 | const statusCode = obj.statusCode; |
| 226 | if (typeof statusCode === "number") { |
| 227 | return statusCode; |
| 228 | } |
| 229 | |
| 230 | const status = obj.status; |
| 231 | if (typeof status === "number") { |
| 232 | return status; |
| 233 | } |
| 234 | |
| 235 | const response = obj.response; |
| 236 | if (response && typeof response === "object") { |
| 237 | const responseStatus = (response as Record<string, unknown>).status; |
| 238 | if (typeof responseStatus === "number") { |
| 239 | return responseStatus; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | const cause = obj.cause; |
| 244 | if (cause && typeof cause === "object") { |
| 245 | const causeStatus = (cause as Record<string, unknown>).statusCode; |
| 246 | if (typeof causeStatus === "number") { |
| 247 | return causeStatus; |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | // Best-effort fallback on message contents. |
| 252 | const message = obj.message; |
| 253 | if (typeof message === "string") { |
| 254 | const re = /\b(400|401|403|404|405)\b/; |
| 255 | const match = re.exec(message); |
| 256 | if (match) { |
| 257 | return Number(match[1]); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | return null; |
| 262 | } |
| 263 | |
| 264 | function shouldAutoFallbackToSse(error: unknown): boolean { |
| 265 | const status = extractHttpStatusCode(error); |
no test coverage detected