class HttpResponse implements PrometheusHttpResponse {

        @Override
        public void setHeader(String name, String value) {
            httpExchange.getResponseHeaders().set(name, value);
        }

        @Override
        public OutputStream sendHeadersAndGetBody(int statusCode, int contentLength) throws IOException {
            if (responseSent) {
                throw new IOException("Cannot send multiple HTTP responses for a single HTTP exchange.");
            }
            responseSent = true;
            httpExchange.sendResponseHeaders(statusCode, contentLength);
            return httpExchange.getResponseBody();
        }
    }

    @Override
    public HttpRequest getRequest() {
        return request;
    }

    @Override
    public HttpResponse getResponse() {
        return response;
    }

    @Override
    public void handleException(IOException e) throws IOException {
        sendErrorResponseWithStackTrace(e);
    }

    @Override
    public void handleException(RuntimeException e) {
        sendErrorResponseWithStackTrace(e);
    }

    private void sendErrorResponseWithStackTrace(Exception requestHandlerException) {<FILL_FUNCTION_BODY>

        if (!responseSent) {
            responseSent = true;
            try {
                StringWriter stringWriter = new StringWriter();
                PrintWriter printWriter = new PrintWriter(stringWriter);
                printWriter.write("An Exception occurred while scraping metrics: ");
                requestHandlerException.printStackTrace(new PrintWriter(printWriter));
                byte[] stackTrace = stringWriter.toString().getBytes(StandardCharsets.UTF_8);
                httpExchange.getResponseHeaders().set("Content-Type", "text/plain; charset=utf-8");
                httpExchange.sendResponseHeaders(500, stackTrace.length);
                httpExchange.getResponseBody().write(stackTrace);
            } catch (Exception errorWriterException) {
                // We want to avoid logging so that we don't mess with application logs when the HTTPServer is used in a Java agent.
                // However, if we can't even send an error response to the client there's nothing we can do but logging a message.
                Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, "The Prometheus metrics HTTPServer caught an Exception during scrape and failed to send an error response to the client.", errorWriterException);
                Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, "Original Exception that caused the Prometheus scrape error:", requestHandlerException);
            }
        } else {
            // If the exception occurs after response headers have been sent, it's too late to respond with HTTP 500.
            Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, "The Prometheus metrics HTTPServer caught an Exception while trying to send the metrics response.", requestHandlerException);
        }
    
<no_super_class>