package com.kri.common.feature.http; import java.util.Collections; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; /** An upstream HTTP error whose response can be relayed without rewriting its body. */ public class ExternalApiResponseException extends RuntimeException { private final int statusCode; private final Map> headers; private final byte[] body; public ExternalApiResponseException(int statusCode, Map> headers, byte[] body) { super("External API returned HTTP " + statusCode); this.statusCode = statusCode; Map> copy = new LinkedHashMap<>(); if (headers != null) { headers.forEach((name, values) -> { if (name != null && values != null) { copy.put(name, List.copyOf(values)); } }); } this.headers = Collections.unmodifiableMap(copy); this.body = body == null ? new byte[0] : body.clone(); } public int getStatusCode() { return statusCode; } public Map> getHeaders() { return headers; } public byte[] getBody() { return body.clone(); } }