|
| 1 | +package io.a2a.client.transport.rest; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 4 | +import com.fasterxml.jackson.databind.JsonNode; |
| 5 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 6 | +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; |
| 7 | +import io.a2a.client.http.A2AHttpResponse; |
| 8 | +import io.a2a.spec.A2AClientException; |
| 9 | +import io.a2a.spec.AuthenticatedExtendedCardNotConfiguredError; |
| 10 | +import io.a2a.spec.ContentTypeNotSupportedError; |
| 11 | +import io.a2a.spec.InternalError; |
| 12 | +import io.a2a.spec.InvalidAgentResponseError; |
| 13 | +import io.a2a.spec.InvalidParamsError; |
| 14 | +import io.a2a.spec.InvalidRequestError; |
| 15 | +import io.a2a.spec.JSONParseError; |
| 16 | +import io.a2a.spec.MethodNotFoundError; |
| 17 | +import io.a2a.spec.PushNotificationNotSupportedError; |
| 18 | +import io.a2a.spec.TaskNotCancelableError; |
| 19 | +import io.a2a.spec.TaskNotFoundError; |
| 20 | +import io.a2a.spec.UnsupportedOperationError; |
| 21 | +import java.util.logging.Level; |
| 22 | +import java.util.logging.Logger; |
| 23 | + |
| 24 | +/** |
| 25 | + * Utility class to A2AHttpResponse to appropriate A2A error types |
| 26 | + */ |
| 27 | +public class RestErrorMapper { |
| 28 | + |
| 29 | + private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper().registerModule(new JavaTimeModule()); |
| 30 | + |
| 31 | + public static A2AClientException mapRestError(A2AHttpResponse response) { |
| 32 | + return RestErrorMapper.mapRestError(response.body(), response.status()); |
| 33 | + } |
| 34 | + |
| 35 | + public static A2AClientException mapRestError(String body, int code) { |
| 36 | + try { |
| 37 | + if (body != null && !body.isBlank()) { |
| 38 | + JsonNode node = OBJECT_MAPPER.readTree(body); |
| 39 | + String className = node.findValue("error").asText(); |
| 40 | + String errorMessage = node.findValue("message").asText(); |
| 41 | + return mapRestError(className, errorMessage, code); |
| 42 | + } |
| 43 | + return mapRestError("", "", code); |
| 44 | + } catch (JsonProcessingException ex) { |
| 45 | + Logger.getLogger(RestErrorMapper.class.getName()).log(Level.SEVERE, null, ex); |
| 46 | + return new A2AClientException("Failed to parse error response: " + ex.getMessage()); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + public static A2AClientException mapRestError(String className, String errorMessage, int code) { |
| 51 | + switch (className) { |
| 52 | + case "io.a2a.spec.TaskNotFoundError": |
| 53 | + return new A2AClientException(errorMessage, new TaskNotFoundError()); |
| 54 | + case "io.a2a.spec.AuthenticatedExtendedCardNotConfiguredError": |
| 55 | + return new A2AClientException(errorMessage, new AuthenticatedExtendedCardNotConfiguredError()); |
| 56 | + case "io.a2a.spec.ContentTypeNotSupportedError": |
| 57 | + return new A2AClientException(errorMessage, new ContentTypeNotSupportedError(null, null, errorMessage)); |
| 58 | + case "io.a2a.spec.InternalError": |
| 59 | + return new A2AClientException(errorMessage, new InternalError(errorMessage)); |
| 60 | + case "io.a2a.spec.InvalidAgentResponseError": |
| 61 | + return new A2AClientException(errorMessage, new InvalidAgentResponseError(null, null, errorMessage)); |
| 62 | + case "io.a2a.spec.InvalidParamsError": |
| 63 | + return new A2AClientException(errorMessage, new InvalidParamsError()); |
| 64 | + case "io.a2a.spec.InvalidRequestError": |
| 65 | + return new A2AClientException(errorMessage, new InvalidRequestError()); |
| 66 | + case "io.a2a.spec.JSONParseError": |
| 67 | + return new A2AClientException(errorMessage, new JSONParseError()); |
| 68 | + case "io.a2a.spec.MethodNotFoundError": |
| 69 | + return new A2AClientException(errorMessage, new MethodNotFoundError()); |
| 70 | + case "io.a2a.spec.PushNotificationNotSupportedError": |
| 71 | + return new A2AClientException(errorMessage, new PushNotificationNotSupportedError()); |
| 72 | + case "io.a2a.spec.TaskNotCancelableError": |
| 73 | + return new A2AClientException(errorMessage, new TaskNotCancelableError()); |
| 74 | + case "io.a2a.spec.UnsupportedOperationError": |
| 75 | + return new A2AClientException(errorMessage, new UnsupportedOperationError()); |
| 76 | + default: |
| 77 | + return new A2AClientException(errorMessage); |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments