将自定义数据添加到 500 个服务器错误响应中

问题描述 投票:0回答:1

我正在尝试修改在出现不受控制的错误(状态代码 >= 500)时返回的响应,在这种情况下,我想向响应添加相关 ID,以便在分布式应用程序中更轻松地进行错误日志聚合。对于状态代码 <= 499 this works fine with

@ServerResponseFilter

@ServerResponseFilter
public void modifyResponse(ContainerResponseContext responseContext) {
    Log.debugf("Calling modifyResponse with context %s", responseContext);
    if (responseContext.getStatus() >= 400) {
        if (responseContext.getEntity() instanceof String) {
            responseContext.setEntity("Error with Correlation-ID " + MDC.get("correlation_id") + ": " + responseContext.getEntity());
        }
    }
}

但是,对于状态代码 >= 500 的响应,不会调用

modifyResponse
,即使使用例如“手动”抛出时也是如此。
throw new WebApplicationException("abort!", 500)

有没有办法可以在出现 500 服务器错误时使用一些自定义数据来丰富 HTTP 响应?

java quarkus resteasy
1个回答
0
投票

您所看到的实际上是预期的行为。

如果抛出的异常由异常映射器处理,则不会运行任何响应过滤器。

© www.soinside.com 2019 - 2024. All rights reserved.