-
-
Notifications
You must be signed in to change notification settings - Fork 622
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Vadim Demedes
authored
Jun 18, 2020
1 parent
5990747
commit 2bcb4c0
Showing
5 changed files
with
184 additions
and
9 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import * as fs from 'fs'; | ||
import React from 'react'; | ||
import type {FC} from 'react'; | ||
import StackUtils from 'stack-utils'; | ||
import codeExcerpt, {ExcerptLine} from 'code-excerpt'; | ||
import Box from './Box'; | ||
import Text from './Text'; | ||
|
||
const stackUtils = new StackUtils({ | ||
cwd: process.cwd(), | ||
internals: StackUtils.nodeInternals() | ||
}); | ||
|
||
interface Props { | ||
error: Error; | ||
} | ||
|
||
const ErrorOverview: FC<Props> = ({error}) => { | ||
const stack = error.stack!.split('\n').slice(1); | ||
const origin = stackUtils.parseLine(stack[0]); | ||
let excerpt: ExcerptLine[] | undefined; | ||
let lineWidth = 0; | ||
|
||
if (origin?.file && origin?.line) { | ||
const sourceCode = fs.readFileSync(origin.file, 'utf8'); | ||
excerpt = codeExcerpt(sourceCode, origin.line); | ||
|
||
if (excerpt) { | ||
for (const {line} of excerpt) { | ||
lineWidth = Math.max(lineWidth, String(line).length); | ||
} | ||
} | ||
} | ||
|
||
return ( | ||
<Box flexDirection="column" padding={1}> | ||
<Box> | ||
<Text backgroundColor="red" color="white"> | ||
{' '} | ||
ERROR{' '} | ||
</Text> | ||
|
||
<Text> {error.message}</Text> | ||
</Box> | ||
|
||
{origin && ( | ||
<Box marginTop={1}> | ||
<Text dimColor> | ||
{origin.file}:{origin.line}:{origin.column} | ||
</Text> | ||
</Box> | ||
)} | ||
|
||
{origin && excerpt && ( | ||
<Box marginTop={1} flexDirection="column"> | ||
{excerpt.map(({line, value}) => ( | ||
<Box key={line}> | ||
<Box width={lineWidth + 1}> | ||
<Text | ||
dimColor={line !== origin.line} | ||
backgroundColor={line === origin.line ? 'red' : undefined} | ||
color={line === origin.line ? 'white' : undefined} | ||
> | ||
{String(line).padStart(lineWidth, ' ')}: | ||
</Text> | ||
</Box> | ||
|
||
<Text | ||
key={line} | ||
backgroundColor={line === origin.line ? 'red' : undefined} | ||
color={line === origin.line ? 'white' : undefined} | ||
> | ||
{' ' + value} | ||
</Text> | ||
</Box> | ||
))} | ||
</Box> | ||
)} | ||
|
||
<Box marginTop={1} flexDirection="column"> | ||
{error | ||
.stack!.split('\n') | ||
.slice(1) | ||
.map(line => { | ||
const parsedLine = stackUtils.parseLine(line)!; | ||
|
||
return ( | ||
<Box key={line}> | ||
<Text dimColor>- </Text> | ||
<Text dimColor bold> | ||
{parsedLine.function} | ||
</Text> | ||
<Text dimColor color="gray"> | ||
{' '} | ||
({parsedLine.file}:{parsedLine.line}:{parsedLine.column}) | ||
</Text> | ||
</Box> | ||
); | ||
})} | ||
</Box> | ||
</Box> | ||
); | ||
}; | ||
|
||
export default ErrorOverview; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* eslint-disable unicorn/string-content */ | ||
import React from 'react'; | ||
import test from 'ava'; | ||
import {spy} from 'sinon'; | ||
import patchConsole from 'patch-console'; | ||
import stripAnsi from 'strip-ansi'; | ||
import {render} from '../src'; | ||
|
||
let restore; | ||
|
||
test.before(() => { | ||
restore = patchConsole(); | ||
}); | ||
|
||
test.after(() => { | ||
restore(); | ||
}); | ||
|
||
test('catch and display error', t => { | ||
const stdout = { | ||
columns: 100, | ||
write: spy() | ||
}; | ||
|
||
const Test = () => { | ||
throw new Error('Oh no'); | ||
}; | ||
|
||
render(<Test />, {stdout}); | ||
|
||
t.deepEqual( | ||
stripAnsi(stdout.write.lastCall.args[0]).split('\n').slice(0, 14), | ||
[ | ||
'', | ||
' ERROR Oh no', | ||
'', | ||
' test/errors.tsx:26:9', | ||
'', | ||
' 23: };', | ||
' 24:', | ||
' 25: const Test = () => {', | ||
" 26: throw new Error('Oh no');", | ||
' 27: };', | ||
' 28:', | ||
' 29: render(<Test />, {stdout});', | ||
'', | ||
' - Test (test/errors.tsx:26:9)' | ||
] | ||
); | ||
}); |