private void writeResponse(DiscFilterRequest request, ErrorResponse error, ResponseHandler responseHandler) {
        JsonNode json;
        var mapper = jsonMapper();
        if (error.customJson != null) {
            json = error.customJson;
        } else {
            ObjectNode o = mapper.createObjectNode();
            if (error.errorCodeAsInt != null) o.put("code", error.errorCodeAsInt);
            else if (error.errorCodeAsString != null) o.put("code", error.errorCodeAsString);
            if (error.message != null) o.put("message", error.message);
            json = o;
        }
        error.response.headers().put("Content-Type", "application/json"); // Note: Overwrites header if already exists
        error.response.headers().put("Cache-Control", "must-revalidate,no-cache,no-store");
        try (FastContentWriter writer = ResponseDispatch.newInstance(error.response).connectFastWriter(responseHandler)) {
            String jsonAsStr = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(json);
            log.log(Level.FINE, () -> String.format("Error response for '%s': statusCode=%d, json='%s'",
                    request, error.response.getStatus(), jsonAsStr));
            writer.write(jsonAsStr);
        } catch (JsonProcessingException e) {
            throw new UncheckedIOException(e);
        }
    }
A base class for {@link SecurityRequestFilter} implementations that renders an error response as JSON.

@author bjorncs
jdisc-security-filters/src/main/java/com/yahoo/jdisc/http/filter/security/base/JsonSecurityRequestFilterBase.java