|
3 | 3 |
|
4 | 4 | package com.azure.ai.agents.implementation.http; |
5 | 5 |
|
| 6 | +import com.azure.core.exception.ClientAuthenticationException; |
| 7 | +import com.azure.core.exception.HttpResponseException; |
| 8 | +import com.azure.core.exception.ResourceNotFoundException; |
6 | 9 | import com.azure.core.http.HttpHeaderName; |
7 | 10 | import com.azure.core.http.HttpHeaders; |
8 | 11 | import com.azure.core.http.HttpMethod; |
|
18 | 21 | import com.openai.core.http.HttpRequest; |
19 | 22 | import com.openai.core.http.HttpRequestBody; |
20 | 23 | 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; |
21 | 33 |
|
22 | 34 | import java.io.ByteArrayOutputStream; |
23 | 35 | import java.util.List; |
@@ -103,17 +115,72 @@ public CompletableFuture<HttpResponse> executeAsync(HttpRequest request, Request |
103 | 115 |
|
104 | 116 | return this.httpPipeline.send(azureRequest, requestContext) |
105 | 117 | .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) |
114 | 119 | .toFuture(); |
115 | 120 | } |
116 | 121 |
|
| 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 | + |
117 | 184 | /** |
118 | 185 | * Converts the OpenAI request metadata and body into an Azure {@link com.azure.core.http.HttpRequest}. |
119 | 186 | */ |
|
0 commit comments