@SuppressWarnings("nls")
    @Override
    public void write(ApiRequest request, Throwable error, IApiClientResponse response) {
        boolean isXml = false;
        if (request != null && request.getApi() != null && "xml".equals(request.getApi().getEndpointContentType())) {
            isXml = true;
        }
        String message = createErrorMessage(request, error);
        LOGGER.error(message, error);
        response.setHeader("X-Gateway-Error", message);

        int statusCode = 500;
        if (error instanceof IStatusCode) {
            statusCode = ((IStatusCode) error).getStatusCode();
        }
        response.setStatusCode(statusCode);

        // #createErrorResponse can be overriden by subclasses (e.g. trace). So need to be careful.
        EngineErrorResponse eer = createErrorResponse(error, message, statusCode);
        if (isXml) {
            response.setHeader("Content-Type", "application/xml");
            try {
                Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
                StringWriter sw = new StringWriter();
                jaxbMarshaller.marshal(eer, sw);
                response.write(sw.getBuffer());
            } catch (Exception e) {
                LOGGER.error(e);
            }
        } else {
            response.setHeader("Content-Type", "application/json");
            try {
                StringWriter sw = new StringWriter();
                mapper.writer().writeValue(sw, eer);
                response.write(sw.getBuffer());
            } catch (Exception e) {
                LOGGER.error(e);
            }
        }
    }
@see io.apiman.gateway.engine.IPolicyErrorWriter#write(io.apiman.gateway.engine.beans.ApiRequest, java.lang.Throwable, io.apiman.gateway.engine.IApiClientResponse)
gateway/engine/core/src/main/java/io/apiman/gateway/engine/impl/DefaultPolicyErrorWriter.java