import ansiHtml from 'ansi-html';
import { htmlEscape } from 'escape-goat';
import TEMPLATE_ERROR_HTML from '../template/error.html.js';
import { createDebugger } from '../util/create-debugger.js';

const middlewareError = ({ getError }) => {
  const debug = createDebugger("live-preview");
  return (req, res, next) => {
    const error = getError();
    if (!error)
      return next();
    if (!req.headers.accept?.includes("html")) {
      res.statusCode = 500;
      res.end();
      debug?.(`served empty error response for "${req.url}".`);
      return;
    }
    const message = ansiHtml(htmlEscape(error.message));
    const html = TEMPLATE_ERROR_HTML.replace(/(?=<\/body>)|$/iu, `<pre class="error"><code>${message}</code></pre>
`);
    res.statusCode = 200;
    res.setHeader("Content-Type", "text/html");
    res.setHeader("Content-Length", Buffer.byteLength(html, "utf8"));
    res.end(html);
    debug?.(`served error page for "${req.url}".`);
  };
};

export { middlewareError as default };
//# sourceMappingURL=error.js.map
