Files
spring-ai/providers/src/main/java/com/ankurm/providers/Failures.java
T

51 lines
1.7 KiB
Java

package com.ankurm.providers;
import java.io.IOException;
import com.anthropic.errors.AnthropicIoException;
import com.anthropic.errors.AnthropicServiceException;
import com.google.genai.errors.ApiException;
import com.openai.errors.OpenAIIoException;
import com.openai.errors.OpenAIServiceException;
/**
* Decides whether a failed model call is worth sending to another provider. The three vendors'
* SDKs throw three unrelated exception families, and Gemini's arrives wrapped in a plain
* RuntimeException, so the cause chain is walked.
*/
public final class Failures {
private Failures() {
}
/** The HTTP status found anywhere in the cause chain, or -1. */
public static int status(Throwable t) {
for (Throwable c = t; c != null; c = c.getCause()) {
if (c instanceof OpenAIServiceException e) {
return e.statusCode();
}
if (c instanceof AnthropicServiceException e) {
return e.statusCode();
}
if (c instanceof ApiException e) {
return e.code();
}
}
return -1;
}
/** True for overload, rate limit, server errors and network failures; false for 4xx the caller caused. */
public static boolean isTransient(Throwable t) {
int status = status(t);
if (status != -1) {
return status == 408 || status == 429 || status >= 500;
}
for (Throwable c = t; c != null; c = c.getCause()) {
if (c instanceof OpenAIIoException || c instanceof AnthropicIoException || c instanceof IOException) {
return true;
}
}
return false;
}
}