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

Function formatError

packages/vite/src/node/plugins/forwardConsole.ts:54–144  ·  view source on GitHub ↗
(
  payload: Extract<
    ForwardConsolePayload,
    { type: 'error' | 'unhandled-rejection' }
  >,
  environment: DevEnvironment,
  sourceMapCache: Map<string, any>,
)

Source from the content-addressed store, hash-verified

52}
53
54function formatError(
55 payload: Extract<
56 ForwardConsolePayload,
57 { type: 'error' | 'unhandled-rejection' }
58 >,
59 environment: DevEnvironment,
60 sourceMapCache: Map<string, any>,
61) {
62 const error = payload.data
63 const stacks = parseErrorStacktrace(error, {
64 getUrlId(id) {
65 const moduleGraph = environment.moduleGraph
66 const mod = moduleGraph.getModuleById(id)
67 if (mod) {
68 return id
69 }
70 const resolvedPath = normalizePath(
71 path.resolve(environment.config.root, id.slice(1)),
72 )
73 const modUrl = moduleGraph.getModuleById(resolvedPath)
74 if (modUrl) {
75 return resolvedPath
76 }
77 // some browsers (looking at you, safari) don't report queries in stack traces
78 // the next best thing is to try the first id that this file resolves to
79 const files = moduleGraph.getModulesByFile(resolvedPath)
80 if (files && files.size) {
81 return files.values().next().value!.id!
82 }
83 return id
84 },
85 getSourceMap(id) {
86 if (sourceMapCache.has(id)) {
87 return sourceMapCache.get(id)
88 }
89
90 const result = environment.moduleGraph.getModuleById(id)?.transformResult
91 // handle non-inline source map such as pre-bundled deps in node_modules/.vite
92 if (result && !result.map) {
93 try {
94 const filePath = id.split('?')[0]
95 const extracted = extractSourcemapFromFile(
96 result.code,
97 filePath,
98 environment.config.logger,
99 )
100 sourceMapCache.set(id, extracted?.map)
101 return extracted?.map
102 } catch {
103 sourceMapCache.set(id, null)
104 return null
105 }
106 }
107
108 sourceMapCache.set(id, result?.map)
109 return result?.map
110 },
111 // override it to empty since vitest uses this option to skip internal files by default.

Callers 1

configureServerFunction · 0.85

Calls 3

generateCodeFrameFunction · 0.90
normalizePathFunction · 0.85
getModulesByFileMethod · 0.45

Tested by

no test coverage detected