Response in Feign Error Decoder not getting Zalando Problem custom property

Viewed 457

I have two Spring Boot components, one service that is requested, and the main component that sends request through a Feign Client. The service component uses Zalando problem to build an exception and this is mapped into a structure in the main component error decoder.

My service component problem creation (in kotlin):

    @ExceptionHandler
fun handle(ex: ErrorException, request: NativeWebRequest): ResponseEntity<Problem> {

    return when (ex.code) {

        ErrorCodes.OPERATION_NOT_ALLOWED,
        ErrorCodes.QUERY_ERROR -> create(Status.BAD_REQUEST, Problem.builder()
                .withDetail(ex.message)
                .with("code", ex.code.name)
                .withStatus(Status.BAD_REQUEST)
                .build(), request)
    }
}

My error decoder class in main component:

public class ClientErrorDecoder implements ErrorDecoder {
private final ObjectMapper mapper = new ObjectMapper();

@Override
public Exception decode(String methodKey, Response response) {
    try {
        ClientExceptionDTO clientExDTO = mapper.readValue(response.body().asInputStream(), ClientExDTO.class);
        log.debug("Exception from external service: " + clientExDTO);
        return new ErrorException(clientExDTO.getDetail(), ErrorCodes.EXTERNAL_SERVICE_ERROR);
    } catch (IOException e) {
        throw new ErrorException("Failed to process response body.", ErrorCodes.UNPROCESSABLE_RESPONSE);
    }
}

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
private static class ClientExDTO {
    private String title;
    private String code;
    private String detail;
    private String status;
    
}

}

The problem I am having is that the Response object of main component is not containing the custom property "code": {"title":"Bad Request","status":400,"detail":"Exception occurred in ext service"}

What am I missing here with Zalando Problem along with Feign Client?

0 Answers
Related