mirror of
https://github.com/facebook/react-native.git
synced 2026-11-18 20:19:07 +00:00
Summary: This diff adds handling for syntax errors. ## Strategy To do this we introduce a new log level type syntax, giving us these levels with semantics: - `warn` - console warns, show collapsed, dismissible - `error` - console errors, show collapsed, dismissible - `fatal` - thrown exceptions, show expanded, not dismissible - `syntax` - thrown exceptions for invalid syntax, show expanded, not dismissible Syntax errors shows expanded, covers all other errors, and are only dismissible when the syntax error is fixed and updated with Fast Refresh. Once the syntax error is fixed, it reveals any previously covered fatals, errors, or warnings behind it In many ways, this makes syntax errors the highest level error. ## Visuals Syntax errors also have their own display formatting. Stack traces for syntax errors don't make sense, so we don't show them. Instead, we show the syntax error message and a code frame for the error. The code frame is also updated so that is doesn't wrap and is horizontally scrollable, making it easier to read. ## Detecting syntax errors To detect syntax errors we've updated `LogBoxData.addException` to call the parse function `parseLogBoxException`. This method will perform a regex on the error message to detect: - file name - location - error message - codeframe If this regex fails for any reason to find all four parts, we'll fall back to a fatal. Over time we'll update this regex to be more robust and handle more cases we've missed. Changelog: [Internal] Reviewed By: motiz88 Differential Revision: D18278862 fbshipit-source-id: 59069aba38a27c44787e5248b2973c3a345c4a0a
136 lines
4.0 KiB
JavaScript
136 lines
4.0 KiB
JavaScript
/**
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*
|
|
* @flow strict-local
|
|
* @format
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
import LogBoxInspectorCodeFrame from './LogBoxInspectorCodeFrame';
|
|
import Platform from '../../Utilities/Platform';
|
|
import * as React from 'react';
|
|
import ScrollView from '../../Components/ScrollView/ScrollView';
|
|
import StyleSheet from '../../StyleSheet/StyleSheet';
|
|
import View from '../../Components/View/View';
|
|
import * as LogBoxData from '../Data/LogBoxData';
|
|
import LogBoxInspectorFooter from './LogBoxInspectorFooter';
|
|
import LogBoxInspectorMessageHeader from './LogBoxInspectorMessageHeader';
|
|
import LogBoxInspectorReactFrames from './LogBoxInspectorReactFrames';
|
|
import LogBoxInspectorStackFrames from './LogBoxInspectorStackFrames';
|
|
import LogBoxInspectorMeta from './LogBoxInspectorMeta';
|
|
import LogBoxInspectorHeader from './LogBoxInspectorHeader';
|
|
import * as LogBoxStyle from './LogBoxStyle';
|
|
|
|
import type LogBoxLog from '../Data/LogBoxLog';
|
|
|
|
type Props = $ReadOnly<{|
|
|
onDismiss: () => void,
|
|
onChangeSelectedIndex: (index: number) => void,
|
|
onMinimize: () => void,
|
|
logs: $ReadOnlyArray<LogBoxLog>,
|
|
selectedIndex: number,
|
|
hasFatal: boolean,
|
|
|}>;
|
|
|
|
function LogBoxInspector(props: Props): React.Node {
|
|
const {logs, selectedIndex} = props;
|
|
|
|
const log = logs[selectedIndex];
|
|
React.useEffect(() => {
|
|
// Symbolicate the visible log if it hasn't been already.
|
|
if (log != null && log.symbolicated.status !== 'COMPLETE') {
|
|
LogBoxData.symbolicateLogNow(log);
|
|
}
|
|
}, [log]);
|
|
|
|
React.useEffect(() => {
|
|
// Optimistically symbolicate the last and next logs.
|
|
if (logs.length > 1) {
|
|
const selected = selectedIndex;
|
|
const lastIndex = logs.length - 1;
|
|
const prevIndex = selected - 1 < 0 ? lastIndex : selected - 1;
|
|
const nextIndex = selected + 1 > lastIndex ? 0 : selected + 1;
|
|
LogBoxData.symbolicateLogLazy(logs[prevIndex]);
|
|
LogBoxData.symbolicateLogLazy(logs[nextIndex]);
|
|
}
|
|
}, [logs, selectedIndex]);
|
|
|
|
function _handleRetry() {
|
|
LogBoxData.retrySymbolicateLogNow(log);
|
|
}
|
|
|
|
if (log == null) {
|
|
return null;
|
|
}
|
|
|
|
return (
|
|
<View style={styles.root}>
|
|
<LogBoxInspectorHeader
|
|
onSelectIndex={props.onChangeSelectedIndex}
|
|
selectedIndex={selectedIndex}
|
|
total={logs.length}
|
|
level={log.level}
|
|
/>
|
|
<LogBoxInspectorBody log={log} onRetry={_handleRetry} />
|
|
<LogBoxInspectorFooter
|
|
onDismiss={props.onDismiss}
|
|
onMinimize={props.onMinimize}
|
|
level={log.level}
|
|
/>
|
|
</View>
|
|
);
|
|
}
|
|
|
|
function LogBoxInspectorBody(props) {
|
|
const [collapsed, setCollapsed] = React.useState(true);
|
|
if (collapsed) {
|
|
return (
|
|
<>
|
|
<LogBoxInspectorMessageHeader
|
|
collapsed={collapsed}
|
|
onPress={() => setCollapsed(!collapsed)}
|
|
message={props.log.message}
|
|
level={props.log.level}
|
|
/>
|
|
<ScrollView style={styles.scrollBody}>
|
|
<LogBoxInspectorCodeFrame codeFrame={props.log.codeFrame} />
|
|
<LogBoxInspectorReactFrames log={props.log} />
|
|
<LogBoxInspectorStackFrames log={props.log} onRetry={props.onRetry} />
|
|
<LogBoxInspectorMeta />
|
|
</ScrollView>
|
|
</>
|
|
);
|
|
}
|
|
return (
|
|
<ScrollView style={styles.scrollBody}>
|
|
<LogBoxInspectorMessageHeader
|
|
collapsed={collapsed}
|
|
onPress={() => setCollapsed(!collapsed)}
|
|
message={props.log.message}
|
|
level={props.log.level}
|
|
/>
|
|
<LogBoxInspectorReactFrames log={props.log} />
|
|
<LogBoxInspectorStackFrames log={props.log} onRetry={props.onRetry} />
|
|
<LogBoxInspectorMeta />
|
|
</ScrollView>
|
|
);
|
|
}
|
|
|
|
const styles = StyleSheet.create({
|
|
root: {
|
|
backgroundColor: LogBoxStyle.getTextColor(1),
|
|
elevation: Platform.OS === 'android' ? Number.MAX_SAFE_INTEGER : undefined,
|
|
height: '100%',
|
|
},
|
|
scrollBody: {
|
|
backgroundColor: LogBoxStyle.getBackgroundColor(0.9),
|
|
flex: 1,
|
|
},
|
|
});
|
|
|
|
export default LogBoxInspector;
|