mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 22:11:36 -05:00
fix(sqs): translate Query-protocol error codes to JSON __type equivalents (#59)
The AWS SDK v2 uses the __type field in JSON error responses to instantiate typed exceptions. Query-protocol codes such as AWS.SimpleQueueService.NonExistentQueue were being forwarded verbatim into the JSON __type field, causing the SDK to fall back to a generic SqsException instead of QueueDoesNotExistException. Add a jsonType() method to AwsException that maps Query-protocol codes to their JSON-protocol equivalents (e.g. QueueDoesNotExist, QueueNameExists). Both AwsExceptionMapper and the inline catch in AwsJsonController now call jsonType() so the mapping is applied consistently across all code paths.
This commit is contained in:
1 parent
ff2f68d8ab
commit
7d6cf6179d
4 files changed
+120
-3
No files matched your search
@@ -1,11 +1,29 @@
|
||||
package io.github.hectorvent.floci.core.common;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Base exception for AWS emulator errors.
|
||||
* Maps to AWS-style error responses with code, message, and HTTP status.
|
||||
* <p>
|
||||
* Some services use different error code formats for Query (XML) and JSON protocols.
|
||||
* {@link #jsonType()} returns the JSON-protocol {@code __type} value that the AWS SDK v2
|
||||
* uses to instantiate a specific typed exception rather than falling back to a generic one.
|
||||
*/
|
||||
public class AwsException extends RuntimeException {
|
||||
|
||||
/**
|
||||
* Maps Query-protocol error codes to their JSON-protocol {@code __type} equivalents.
|
||||
* Codes absent from this map are used as-is for both protocols.
|
||||
*/
|
||||
private static final Map<String, String> JSON_TYPE_BY_QUERY_CODE = Map.of(
|
||||
"AWS.SimpleQueueService.NonExistentQueue", "QueueDoesNotExist",
|
||||
"QueueAlreadyExists", "QueueNameExists",
|
||||
"ReceiptHandleIsInvalid", "ReceiptHandleIsInvalid",
|
||||
"TooManyEntriesInBatchRequest", "TooManyEntriesInBatchRequest",
|
||||
"BatchEntryIdNotUnique", "BatchEntryIdNotDistinct"
|
||||
);
|
||||
|
||||
private final String errorCode;
|
||||
private final int httpStatus;
|
||||
|
||||
@@ -22,4 +40,12 @@ public class AwsException extends RuntimeException {
|
||||
public int getHttpStatus() {
|
||||
return httpStatus;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the JSON-protocol {@code __type} value for this error.
|
||||
* The AWS SDK v2 uses this to map responses to typed exception classes.
|
||||
*/
|
||||
public String jsonType() {
|
||||
return JSON_TYPE_BY_QUERY_CODE.getOrDefault(errorCode, errorCode);
|
||||
}
|
||||
}
|
||||
@@ -18,8 +18,8 @@ public class AwsExceptionMapper implements ExceptionMapper<AwsException> {
|
||||
public Response toResponse(AwsException exception) {
|
||||
LOG.debugv("Mapping exception: {0} - {1}", exception.getErrorCode(), exception.getMessage());
|
||||
return Response.status(exception.getHttpStatus())
|
||||
.entity(new AwsErrorResponse(exception.getErrorCode(), exception.getMessage()))
|
||||
.type(MediaType.APPLICATION_JSON)
|
||||
.entity(new AwsErrorResponse(exception.jsonType(), exception.getMessage()))
|
||||
.build();
|
||||
}
|
||||
}
|
||||
@@ -135,7 +135,7 @@ public class AwsJsonController {
|
||||
} catch (AwsException e) {
|
||||
return Response.status(e.getHttpStatus())
|
||||
.type(MediaType.APPLICATION_JSON)
|
||||
.entity(new AwsErrorResponse(e.getErrorCode(), e.getMessage()))
|
||||
.entity(new AwsErrorResponse(e.jsonType(), e.getMessage()))
|
||||
.build();
|
||||
} catch (Exception e) {
|
||||
LOG.error("Error processing " + serviceName + " JSON request", e);
|
||||
@@ -329,7 +329,7 @@ public class AwsJsonController {
|
||||
private Response cborErrorResponse(AwsException e, String protocolHeader) {
|
||||
try {
|
||||
byte[] errBytes = CBOR_MAPPER.writeValueAsBytes(
|
||||
new AwsErrorResponse(e.getErrorCode(), e.getMessage()));
|
||||
new AwsErrorResponse(e.jsonType(), e.getMessage()));
|
||||
return Response.status(e.getHttpStatus())
|
||||
.header(protocolHeader, "rpc-v2-cbor")
|
||||
.type("application/cbor")
|
||||
|
||||
Reference in new issue
Block a user