-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: display the source of client error
- Loading branch information
1 parent
8dae00b
commit c42ab54
Showing
13 changed files
with
347 additions
and
292 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
packages/error-overlay/src/middleware/helper/createOriginalStackFrame.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import { codeFrameColumns } from '@babel/code-frame'; | ||
import { StackFrame } from 'stacktrace-parser'; | ||
|
||
import type webpack from '@shuvi/toolpack/lib/webpack'; | ||
|
||
import { getSourcePath } from './getSourcePath'; | ||
import { getModuleById } from './getModuleById'; | ||
import { findOriginalSourcePositionAndContent } from './findOriginalSourcePositionAndContent'; | ||
|
||
export type OriginalStackFrameResponse = { | ||
originalStackFrame: StackFrame; | ||
originalCodeFrame: string | null; | ||
}; | ||
|
||
export async function createOriginalStackFrame({ | ||
line, | ||
column, | ||
source, | ||
modulePath, | ||
frame, | ||
errorMessage, | ||
compilation | ||
}: { | ||
line: number; | ||
column: number | null; | ||
source: any; | ||
modulePath?: string; | ||
frame: any; | ||
errorMessage?: string; | ||
compilation?: webpack.Compilation; | ||
}): Promise<OriginalStackFrameResponse | null> { | ||
const match = errorMessage?.match(/'([^']+)' module/); | ||
const moduleNotFound = match && match[1]; | ||
const result = | ||
moduleNotFound && compilation | ||
? getModuleById( | ||
modulePath, | ||
compilation! | ||
)?.buildInfo?.importLocByPath?.get(moduleNotFound) ?? null | ||
: await findOriginalSourcePositionAndContent(source, { | ||
line, | ||
column | ||
}); | ||
|
||
if (result === null) { | ||
return null; | ||
} | ||
|
||
const { sourcePosition, sourceContent } = result; | ||
|
||
if (!sourcePosition.source) { | ||
return null; | ||
} | ||
|
||
const filePath = getSourcePath(sourcePosition.source) || modulePath || ''; | ||
|
||
const originalFrame: StackFrame = { | ||
file: sourceContent ? filePath : sourcePosition.source, | ||
lineNumber: sourcePosition.line, | ||
column: sourcePosition.column, | ||
methodName: frame.methodName, | ||
arguments: [] | ||
}; | ||
|
||
const originalCodeFrame: string | null = | ||
!(originalFrame.file?.includes('node_modules') ?? true) && | ||
sourceContent && | ||
sourcePosition.line | ||
? (codeFrameColumns( | ||
sourceContent, | ||
{ | ||
start: { | ||
line: sourcePosition.line, | ||
column: sourcePosition.column ?? 0 | ||
} | ||
}, | ||
{ forceColor: true } | ||
) as string) | ||
: null; | ||
|
||
return { | ||
originalStackFrame: originalFrame, | ||
originalCodeFrame | ||
}; | ||
} |
88 changes: 88 additions & 0 deletions
88
packages/error-overlay/src/middleware/helper/getOriginalStackFrame.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import type { StackFrame } from 'stacktrace-parser'; | ||
import type webpack from '@shuvi/toolpack/lib/webpack'; | ||
import type { Source } from './getSourceById'; | ||
import type { OriginalStackFrame } from '../../view/helpers/stack-frame'; | ||
import { createOriginalStackFrame } from './createOriginalStackFrame'; | ||
|
||
export async function getOriginalStackFrame( | ||
frame: StackFrame, | ||
cache: Map<string, Source>, | ||
resolveBuildFile: (...paths: string[]) => string, | ||
buildDefaultDir: string, | ||
errorMessage?: string, | ||
compilation?: webpack.Compilation | ||
): Promise<OriginalStackFrame> { | ||
if ( | ||
!( | ||
frame.file?.startsWith('webpack-internal:') || | ||
frame.file?.startsWith('file:') | ||
) | ||
) { | ||
return { | ||
error: false, | ||
reason: null, | ||
external: true, | ||
expanded: false, | ||
sourceStackFrame: frame, | ||
originalStackFrame: null, | ||
originalCodeFrame: null | ||
}; | ||
} | ||
|
||
if (cache.get(frame.file) === null) { | ||
return { | ||
error: true, | ||
reason: 'No Content', | ||
external: false, | ||
expanded: false, | ||
sourceStackFrame: frame, | ||
originalStackFrame: null, | ||
originalCodeFrame: null | ||
}; | ||
} | ||
|
||
const frameLine = parseInt(frame.lineNumber?.toString() ?? '', 10); | ||
let frameColumn: number | null = parseInt(frame.column?.toString() ?? '', 10); | ||
if (!frameColumn) { | ||
frameColumn = null; | ||
} | ||
const originalStackFrameResponse = await createOriginalStackFrame({ | ||
line: frameLine, | ||
column: frameColumn, | ||
source: cache.get(frame.file), | ||
frame, | ||
modulePath: resolveBuildFile( | ||
buildDefaultDir, | ||
frame.file.replace(/^(file:\/\/)/, '') | ||
), | ||
errorMessage, | ||
compilation | ||
}); | ||
if (originalStackFrameResponse === null) { | ||
return { | ||
error: true, | ||
reason: 'No Content', | ||
external: false, | ||
expanded: false, | ||
sourceStackFrame: frame, | ||
originalStackFrame: null, | ||
originalCodeFrame: null | ||
}; | ||
} | ||
return { | ||
error: false, | ||
reason: null, | ||
external: false, | ||
expanded: !Boolean( | ||
/* collapsed */ | ||
(frame.file?.includes('node_modules') || | ||
originalStackFrameResponse.originalStackFrame?.file?.includes( | ||
'node_modules' | ||
)) ?? | ||
true | ||
), | ||
sourceStackFrame: frame, | ||
originalStackFrame: originalStackFrameResponse.originalStackFrame, | ||
originalCodeFrame: originalStackFrameResponse.originalCodeFrame || null | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.