( server: ViteDevServer, allowNext = false, )
| 60 | } |
| 61 | |
| 62 | export function errorMiddleware( |
| 63 | server: ViteDevServer, |
| 64 | allowNext = false, |
| 65 | ): Connect.ErrorHandleFunction { |
| 66 | // note the 4 args must be kept for connect to treat this as error middleware |
| 67 | // Keep the named function. The name is visible in debug logs via `DEBUG=connect:dispatcher ...` |
| 68 | return function viteErrorMiddleware(err: RollupError, _req, res, next) { |
| 69 | logError(server, err) |
| 70 | |
| 71 | if (allowNext) { |
| 72 | next() |
| 73 | } else { |
| 74 | res.statusCode = 500 |
| 75 | res.end(` |
| 76 | <!DOCTYPE html> |
| 77 | <html lang="en"> |
| 78 | <head> |
| 79 | <meta charset="UTF-8" /> |
| 80 | <title>Error</title> |
| 81 | <script type="module"> |
| 82 | const error = ${JSON.stringify(prepareError(err)).replace( |
| 83 | /</g, |
| 84 | '\\u003c', |
| 85 | )} |
| 86 | try { |
| 87 | const { ErrorOverlay } = await import(${JSON.stringify(path.posix.join(server.config.base, CLIENT_PUBLIC_PATH))}) |
| 88 | document.body.appendChild(new ErrorOverlay(error)) |
| 89 | } catch { |
| 90 | const h = (tag, text) => { |
| 91 | const el = document.createElement(tag) |
| 92 | el.textContent = text |
| 93 | return el |
| 94 | } |
| 95 | document.body.appendChild(h('h1', 'Internal Server Error')) |
| 96 | document.body.appendChild(h('h2', error.message)) |
| 97 | document.body.appendChild(h('pre', error.stack)) |
| 98 | document.body.appendChild(h('p', '(Error overlay failed to load)')) |
| 99 | } |
| 100 | </script> |
| 101 | </head> |
| 102 | <body> |
| 103 | </body> |
| 104 | </html> |
| 105 | `) |
| 106 | } |
| 107 | } |
| 108 | } |
no test coverage detected