🚧 Map Response headers when proxy exception

This commit is contained in:
2026-07-04 12:10:09 +02:00
parent 30665fdaa5
commit 82fa768527
4 changed files with 37 additions and 7 deletions
@@ -94,7 +94,7 @@ public class Resource
catch (ProxyHttpException e) catch (ProxyHttpException e)
{ {
accessLog.log(AccessLog.Type.ERROR, context, PathConverter.toPath(requestSegments), route.target(), e.getStatusCode(), e.getTime()); accessLog.log(AccessLog.Type.ERROR, context, PathConverter.toPath(requestSegments), route.target(), e.getStatusCode(), e.getTime());
ResponseHandler.error(context, e.getStatusCode()); ResponseHandler.error(context, e.getStatusCode(), e.getHeaders(), e.getBody());
} }
catch (ProxyAuthenticationFailedException e) catch (ProxyAuthenticationFailedException e)
{ {
@@ -119,8 +119,8 @@ public class Resource
} }
catch (IOException e) catch (IOException e)
{ {
LOG.error("IO error during proxy request, returning 502.", e); LOG.error("IO error during proxy request, returning 500.", e);
ResponseHandler.error(context, 502); ResponseHandler.error(context, 500);
} }
} }
} }
@@ -43,7 +43,7 @@ public class ForwardService
} }
else else
{ {
throw new ProxyHttpException(time, response.statusCode()); throw new ProxyHttpException(time, response.statusCode(), response.headers().map(), response.body());
} }
} }
@@ -27,10 +27,23 @@ public class ResponseHandler
context.response().send(Buffer.buffer(response.body())); context.response().send(Buffer.buffer(response.body()));
} }
public static void error(RoutingContext context, int statusCode) public static void error(RoutingContext context, int statusCode, Map<String, List<String>> headers, byte[] body)
{ {
context.response().setStatusCode(statusCode); context.response().setStatusCode(statusCode);
context.response().send(); for (Map.Entry<String, List<String>> header : headers.entrySet())
{
String headerName = header.getKey();
for (String value : header.getValue())
{
context.response().putHeader(headerName, value);
}
}
context.response().send(Buffer.buffer(body));
}
public static void error(RoutingContext context, int statusCode)
{
error(context, statusCode, Map.of(), new byte[0]);
} }
public static void notFound(RoutingContext context) public static void notFound(RoutingContext context)
@@ -1,14 +1,21 @@
package dev.dinauer.oidcproxy.proxy.exception; package dev.dinauer.oidcproxy.proxy.exception;
import java.util.List;
import java.util.Map;
public class ProxyHttpException extends Exception public class ProxyHttpException extends Exception
{ {
private final long time; private final long time;
private final int statusCode; private final int statusCode;
private final Map<String, List<String>> headers;
private final byte[] body;
public ProxyHttpException(long time, int statusCode) public ProxyHttpException(long time, int statusCode, Map<String, List<String>> headers, byte[] body)
{ {
this.time = time; this.time = time;
this.statusCode = statusCode; this.statusCode = statusCode;
this.headers = headers;
this.body = body;
} }
public long getTime() public long getTime()
@@ -20,4 +27,14 @@ public class ProxyHttpException extends Exception
{ {
return statusCode; return statusCode;
} }
public Map<String, List<String>> getHeaders()
{
return headers;
}
public byte[] getBody()
{
return body;
}
} }