Skip to content

Commit 8c77862

Browse files
committed
Error mapping in place
1 parent 275e2e9 commit 8c77862

File tree

1 file changed

+75
-8
lines changed

1 file changed

+75
-8
lines changed

sdk/ai/azure-ai-agents/src/main/java/com/azure/ai/agents/implementation/http/HttpClientHelper.java

Lines changed: 75 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33

44
package com.azure.ai.agents.implementation.http;
55

6+
import com.azure.core.exception.ClientAuthenticationException;
7+
import com.azure.core.exception.HttpResponseException;
8+
import com.azure.core.exception.ResourceNotFoundException;
69
import com.azure.core.http.HttpHeaderName;
710
import com.azure.core.http.HttpHeaders;
811
import com.azure.core.http.HttpMethod;
@@ -18,6 +21,15 @@
1821
import com.openai.core.http.HttpRequest;
1922
import com.openai.core.http.HttpRequestBody;
2023
import com.openai.core.http.HttpResponse;
24+
import com.openai.errors.BadRequestException;
25+
import com.openai.errors.InternalServerException;
26+
import com.openai.errors.NotFoundException;
27+
import com.openai.errors.OpenAIException;
28+
import com.openai.errors.PermissionDeniedException;
29+
import com.openai.errors.RateLimitException;
30+
import com.openai.errors.UnauthorizedException;
31+
import com.openai.errors.UnexpectedStatusCodeException;
32+
import com.openai.errors.UnprocessableEntityException;
2133

2234
import java.io.ByteArrayOutputStream;
2335
import java.util.List;
@@ -103,17 +115,72 @@ public CompletableFuture<HttpResponse> executeAsync(HttpRequest request, Request
103115

104116
return this.httpPipeline.send(azureRequest, requestContext)
105117
.map(response -> (HttpResponse) new AzureHttpResponseAdapter(response))
106-
// .onErrorMap(t -> {
107-
// // 2 or 3 from Azure Errors, should be mapped to Stainless Error.
108-
// // - Auth
109-
// // - Resource not found
110-
// // - HttpResponse Ex
111-
// //
112-
// // new StainlessException(t.getCause())
113-
// })
118+
.onErrorMap(HttpClientWrapper::mapAzureExceptionToOpenAI)
114119
.toFuture();
115120
}
116121

122+
/**
123+
* Maps Azure exceptions to their corresponding OpenAI exception types.
124+
*
125+
* @param throwable The Azure exception to map.
126+
* @return The corresponding OpenAI exception.
127+
*/
128+
private static Throwable mapAzureExceptionToOpenAI(Throwable throwable) {
129+
if (throwable instanceof HttpResponseException httpResponseException) {
130+
int statusCode = httpResponseException.getResponse().getStatusCode();
131+
Headers headers = toOpenAIHeaders(httpResponseException.getResponse().getHeaders());
132+
Throwable cause = httpResponseException.getCause();
133+
134+
return switch (statusCode) {
135+
case 400 -> BadRequestException.builder()
136+
.headers(headers)
137+
.cause(cause)
138+
.build();
139+
case 401 -> UnauthorizedException.builder()
140+
.headers(headers)
141+
.cause(cause)
142+
.build();
143+
case 403 -> PermissionDeniedException.builder()
144+
.headers(headers)
145+
.cause(cause)
146+
.build();
147+
case 404 -> NotFoundException.builder()
148+
.headers(headers)
149+
.cause(cause)
150+
.build();
151+
case 422 -> UnprocessableEntityException.builder()
152+
.headers(headers)
153+
.cause(cause)
154+
.build();
155+
case 429 -> RateLimitException.builder()
156+
.headers(headers)
157+
.cause(cause)
158+
.build();
159+
case 500, 502, 503, 504 -> InternalServerException.builder()
160+
.statusCode(statusCode)
161+
.headers(headers)
162+
.cause(cause)
163+
.build();
164+
default -> UnexpectedStatusCodeException.builder()
165+
.statusCode(statusCode)
166+
.headers(headers)
167+
.cause(cause)
168+
.build();
169+
};
170+
} else {
171+
return new OpenAIException(throwable.getMessage(), throwable.getCause());
172+
}
173+
}
174+
175+
/**
176+
* Converts Azure {@link HttpHeaders} to OpenAI {@link Headers}.
177+
*/
178+
private static Headers toOpenAIHeaders(HttpHeaders azureHeaders) {
179+
Headers.Builder builder = Headers.builder();
180+
azureHeaders.forEach(header -> builder.put(header.getName(), header.getValue()));
181+
return builder.build();
182+
}
183+
117184
/**
118185
* Converts the OpenAI request metadata and body into an Azure {@link com.azure.core.http.HttpRequest}.
119186
*/

0 commit comments

Comments
 (0)