MCPcopy
hub / github.com/vitejs/vite / errorMiddleware

Function errorMiddleware

packages/vite/src/node/server/middlewares/error.ts:62–108  ·  view source on GitHub ↗
(
  server: ViteDevServer,
  allowNext = false,
)

Source from the content-addressed store, hash-verified

60}
61
62export 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}

Callers 1

_createServerFunction · 0.90

Calls 3

logErrorFunction · 0.85
nextFunction · 0.85
prepareErrorFunction · 0.85

Tested by

no test coverage detected