-
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
feat: collect errors from the browser #20487
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -92,7 +92,8 @@ | |
import { ModuleGraph } from './mixedModuleGraph' | ||
import type { ModuleNode } from './mixedModuleGraph' | ||
import { notFoundMiddleware } from './middlewares/notFound' | ||
import { errorMiddleware } from './middlewares/error' | ||
import { errorIngestMiddleware } from './middlewares/errorIngest' | ||
import type { HmrOptions, NormalizedHotChannel } from './hmr' | ||
import { handleHMRUpdate, updateModules } from './hmr' | ||
import { openBrowser as _openBrowser } from './openBrowser' | ||
|
@@ -910,6 +911,9 @@ | |
// open in editor support | ||
middlewares.use('/__open-in-editor', launchEditorMiddleware()) | ||
|
||
// error ingest handler | ||
middlewares.use(errorIngestMiddleware(server)) | ||
|
||
// ping request handler | ||
// Keep the named function. The name is visible in debug logs via `DEBUG=connect:dispatcher ...` | ||
middlewares.use(function viteHMRPingMiddleware(req, res, next) { | ||
|
@@ -954,7 +958,7 @@ | |
} | ||
|
||
// error handler | ||
middlewares.use(errorMiddleware(server, !!middlewareMode)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This removal was accidental, will fix it! |
||
middlewares.use(errorIngestMiddleware(server)) | ||
|
||
// httpServer.listen can be called multiple times | ||
// when port when using next port number | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import path from 'node:path' | ||
import repl from 'node:repl' | ||
import { readFileSync } from 'fs'; | ||
Check failure on line 3 in packages/vite/src/node/server/middlewares/errorIngest.ts
|
||
import { codeToANSI } from '@shikijs/cli'; | ||
|
||
import { stripVTControlCharacters as strip } from 'node:util' | ||
Check failure on line 6 in packages/vite/src/node/server/middlewares/errorIngest.ts
|
||
import colors from 'picocolors' | ||
import type { RollupError } from 'rollup' | ||
import bodyParser from 'body-parser' | ||
import type { Connect } from 'dep-types/connect' | ||
import type { ErrorPayload } from 'types/hmrPayload' | ||
import { pad } from '../../utils' | ||
import type { ViteDevServer } from '../..' | ||
import { CLIENT_ERROR_RELAY_PATH } from '../../constants' | ||
|
||
export function logBrowserError( | ||
server: ViteDevServer, | ||
stack: string, | ||
fragment: string | ||
): void { | ||
const msg = `${colors.magenta('[browser]')} ${colors.red(stack)}` | ||
server.config.logger.error(`${msg}\n\n${fragment}`, { | ||
clear: true, | ||
timestamp: true, | ||
}) | ||
} | ||
|
||
export function errorIngestMiddleware( | ||
server: ViteDevServer, | ||
): Connect.ErrorHandleFunction { | ||
const jsonParser = bodyParser.json() | ||
return function viteErrorIngestMiddleware(req, res, next) { | ||
if (req.url === CLIENT_ERROR_RELAY_PATH) { | ||
jsonParser(req, res, async () => { | ||
const fragment = await getErrorFragment(req.body) | ||
logBrowserError(server, req.body.stack, fragment) | ||
res.statusCode = 201 | ||
res.end('') | ||
}) | ||
} else { | ||
next() | ||
} | ||
} | ||
} | ||
|
||
async function getErrorFragment (info) { | ||
const res = await fetch(info.filename) | ||
const source = await res.text() | ||
const filtered = source.split(/\r?\n/g) | ||
.map((line, i) => [i, line]) | ||
.slice(Math.max(1, info.lineno - 2), info.lineno + 3) | ||
.filter(([_,line]) => !line.startsWith('//# sourceMappingURL')) | ||
let fragment = "" | ||
let padding = String(info.lineno).length | ||
for (const [lineno, line] of filtered) { | ||
fragment += `${ | ||
lineno === (info.lineno - 1) ? colors.red('>') : ' ' | ||
}${String(lineno).padStart(padding)} | ${ | ||
await codeToANSI(line, 'typescript', 'slack-dark') | ||
}` | ||
if (lineno === (info.lineno - 1)) { | ||
const leftPadding = Math.max(0, (info.colno - 1) + padding + 5) | ||
fragment += `${new Array(leftPadding).fill('').join(' ')}${colors.red('^')}\n` | ||
} | ||
} | ||
return fragment | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<body> | ||
<h4>Test Client Error Ingestion</h4> | ||
<input type="button" value="Create error"> | ||
<script type="module" src="/index.js"></script> | ||
</body> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
|
||
const input = document.querySelector('input') | ||
|
||
input.addEventListener('click', handleClick) | ||
|
||
function handleClick () { | ||
throw new Error('Something went wrong') | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"name": "@vitejs/test-client-errors", | ||
"private": true, | ||
"version": "0.0.0", | ||
"type": "module", | ||
"scripts": { | ||
"debug": "node --inspect-brk ../../packages/vite/bin/vite", | ||
"dev": "vite", | ||
"build": "vite build", | ||
"preview": "vite preview" | ||
}, | ||
"devDependencies": { | ||
"vite": "workspace:^" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { defineConfig } from 'vite' | ||
|
||
export default defineConfig({ | ||
server: {}, | ||
}) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.