42 lines
1.1 KiB
Java
42 lines
1.1 KiB
Java
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<String, List<String>> headers;
|
|
private final byte[] body;
|
|
|
|
public ExternalApiResponseException(int statusCode, Map<String, List<String>> headers, byte[] body) {
|
|
super("External API returned HTTP " + statusCode);
|
|
this.statusCode = statusCode;
|
|
Map<String, List<String>> 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<String, List<String>> getHeaders() {
|
|
return headers;
|
|
}
|
|
|
|
public byte[] getBody() {
|
|
return body.clone();
|
|
}
|
|
}
|