File tree 2 files changed +65
-1
lines changed
packages/react-error-overlay/src
2 files changed +65
-1
lines changed Original file line number Diff line number Diff line change @@ -12,18 +12,40 @@ import Footer from '../components/Footer';
12
12
import Header from '../components/Header' ;
13
13
import CodeBlock from '../components/CodeBlock' ;
14
14
import generateAnsiHTML from '../utils/generateAnsiHTML' ;
15
+ import parseCompileError from '../utils/parseCompileError' ;
16
+ import type { ErrorLocation } from '../utils/parseCompileError' ;
17
+
18
+ const codeAnchorStyle = {
19
+ cursor : 'pointer' ,
20
+ } ;
15
21
16
22
type Props = { |
17
23
error : string ,
18
24
| } ;
19
25
20
26
class CompileErrorContainer extends PureComponent < Props , void > {
27
+ openInEditor ( errorLoc : ErrorLocation ) : void {
28
+ const { filePath, lineNumber } = errorLoc ;
29
+ fetch (
30
+ `/__open-stack-frame-in-editor?fileName=` +
31
+ window . encodeURIComponent ( filePath ) +
32
+ '&lineNumber=' +
33
+ window . encodeURIComponent ( lineNumber || 1 )
34
+ ) . then ( ( ) => { } , ( ) => { } ) ;
35
+ }
36
+
21
37
render ( ) {
22
38
const { error } = this . props ;
39
+ const errLoc = parseCompileError ( error ) ;
23
40
return (
24
41
< ErrorOverlay >
25
42
< Header headerText = "Failed to compile" />
26
- < CodeBlock main = { true } codeHTML = { generateAnsiHTML ( error ) } />
43
+ < a
44
+ onClick = { errLoc ? ( ) => this . openInEditor ( errLoc ) : null }
45
+ style = { errLoc ? codeAnchorStyle : null }
46
+ >
47
+ < CodeBlock main = { true } codeHTML = { generateAnsiHTML ( error ) } />
48
+ </ a >
27
49
< Footer line1 = "This error occurred during the build time and cannot be dismissed." />
28
50
</ ErrorOverlay >
29
51
) ;
Original file line number Diff line number Diff line change
1
+ // @flow
2
+
3
+ export type ErrorLocation = { |
4
+ filePath : string ,
5
+ lineNumber : number ,
6
+ | } ;
7
+
8
+ const filePathRegex = / ^ \. ( \/ [ ^ \/ \n ] + ) + \. [ ^ \/ \n ] + $ / ;
9
+
10
+ // Based on syntax error formating of babylon parser
11
+ // https://github.com/babel/babylon/blob/v7.0.0-beta.22/src/parser/location.js#L19
12
+ const lineNumberRegex = / ^ .* \( ( \d + ) : ( \d + ) \) $ / ;
13
+
14
+ // Based on error formatting of webpack
15
+ // https://github.com/webpack/webpack/blob/v3.5.5/lib/Stats.js#L183-L217
16
+ function parseCompileError ( message : string ) : ?ErrorLocation {
17
+ const lines : Array < string > = message.split('\n');
18
+ let filePath: string = '';
19
+ let lineNumber: number = 0;
20
+
21
+ for (let i = 0; i < lines . length ; i ++ ) {
22
+ const line : string = lines [ i ] ;
23
+ if ( ! line ) continue ;
24
+
25
+ if ( ! filePath && line . match ( filePathRegex ) ) {
26
+ filePath = line ;
27
+ }
28
+
29
+ if ( ! lineNumber ) {
30
+ const match : ?Array < string > = line.match(lineNumberRegex);
31
+ if (match) {
32
+ lineNumber = parseInt ( match [ 1 ] ) ;
33
+ }
34
+ }
35
+
36
+ if ( filePath && lineNumber ) break ;
37
+ }
38
+
39
+ return filePath && lineNumber ? { filePath , lineNumber } : null;
40
+ }
41
+
42
+ export default parseCompileError ;
You can’t perform that action at this time.
0 commit comments