diff --git a/src/runtime/html-validate/nitro.plugin.ts b/src/runtime/html-validate/nitro.plugin.ts index bb391be..55b6bae 100644 --- a/src/runtime/html-validate/nitro.plugin.ts +++ b/src/runtime/html-validate/nitro.plugin.ts @@ -43,24 +43,31 @@ export default function (nitro) { nitro.hooks.hook('render:response', async (response, { event }) => { if (typeof response.body === 'string' && (response.headers?.['Content-Type'] || response.headers?.['content-type'])?.includes('html')) { - const formattedBody = await format(response.body, { plugins: [html], parser: 'html' }) - const results = await validator.validateString(formattedBody) + try { + const formattedBody = await format(response.body, { plugins: [html], parser: 'html' }) + const results = await validator.validateString(formattedBody) - if (response.body && results.errorCount > 0) { - const id = randomUUID() - const data: HtmlValidateReport = { - id, - path: event.path, - html: formattedBody, - results: results.results, + if (response.body && results.errorCount > 0) { + const id = randomUUID() + const data: HtmlValidateReport = { + id, + path: event.path, + html: formattedBody, + results: results.results, + } + response.body = addBeforeBodyEndTag( + response.body, + ``, + ) + nitro.hooks.callHook(HTML_VALIDATE_REPORT_HOOK, data).catch((error) => { + nitro.captureError(error instanceof Error ? error : new Error(String(error)), { event }) + }) } - response.body = addBeforeBodyEndTag( - response.body, - ``, - ) - nitro.hooks.callHook(HTML_VALIDATE_REPORT_HOOK, data).catch((error) => { - nitro.captureError(error instanceof Error ? error : new Error(String(error)), { event }) - }) + } + catch (error) { + // https://github.com/nuxt/hints/issues/360 + // html validate can throw errors for some html + console.warn('HTML Validate error:', error instanceof Error ? error.message : String(error)) } } })