-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#1297 - Handle errors during render and other scenarios where it was…
… not being reported to user. Provide copy error option and get full stack trace in all scenarios.
- Loading branch information
1 parent
88e4e7e
commit dda3712
Showing
18 changed files
with
381 additions
and
158 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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
23 changes: 23 additions & 0 deletions
23
packages/openchs-android/src/framework/errorHandling/AvniError.js
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,23 @@ | ||
import _ from 'lodash'; | ||
|
||
class AvniError { | ||
userMessage; | ||
reportingText; | ||
|
||
static create(userMessage, reportingText) { | ||
const avniError = new AvniError(); | ||
avniError.userMessage = userMessage; | ||
avniError.reportingText = reportingText; | ||
return avniError; | ||
} | ||
|
||
static createFromUserMessageAndStackTrace(userMessage, stackTraceString) { | ||
return AvniError.create(userMessage, `${userMessage}\n${stackTraceString}`); | ||
} | ||
|
||
getDisplayMessage() { | ||
return _.truncate(this.userMessage, {length: 80}); | ||
} | ||
} | ||
|
||
export default AvniError; |
27 changes: 27 additions & 0 deletions
27
packages/openchs-android/src/framework/errorHandling/AvniErrorBoundary.js
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,27 @@ | ||
import React, {Component} from 'react'; | ||
import UnhandledErrorView from "./UnhandledErrorView"; | ||
import ErrorUtil from "./ErrorUtil"; | ||
|
||
export default class AvniErrorBoundary extends Component { | ||
constructor(props) { | ||
super(props); | ||
this.state = {avniError: null}; | ||
} | ||
|
||
static getDerivedStateFromError(error) { | ||
const avniError = ErrorUtil.getAvniErrorSync(error); | ||
return {avniError: avniError}; | ||
} | ||
|
||
componentDidCatch(error: Error, errorInfo: React.ErrorInfo) { | ||
console.log("AvniErrorBoundary", "componentDidCatch"); | ||
} | ||
|
||
render() { | ||
console.log("AvniErrorBoundary", "render"); | ||
if (this.state.avniError) { | ||
return <UnhandledErrorView avniError={this.state.avniError}/>; | ||
} | ||
return this.props.children; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
packages/openchs-android/src/framework/errorHandling/ErrorDisplay.js
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,29 @@ | ||
import Config from "../Config"; | ||
import {Alert, Clipboard, ToastAndroid, View} from "react-native"; | ||
import React from 'react'; | ||
import RNRestart from "react-native-restart"; | ||
import {JSONStringify} from "../../utility/JsonStringify"; | ||
|
||
export function ErrorDisplay({avniError}) { | ||
console.log("ErrorDisplay", "render", Config.allowServerURLConfig); | ||
if (!Config.allowServerURLConfig) { | ||
Alert.alert("App will restart now", avniError.getDisplayMessage(), | ||
[ | ||
{ | ||
text: "copyErrorAndRestart", | ||
onPress: () => { | ||
console.log("ErrorDisplay", JSONStringify(avniError)); | ||
Clipboard.setString(avniError.reportingText); | ||
RNRestart.Restart(); | ||
} | ||
}, | ||
{ | ||
text: "restart", | ||
onPress: () => RNRestart.Restart() | ||
} | ||
], | ||
{cancelable: true} | ||
); | ||
} | ||
return <View/>; | ||
} |
31 changes: 31 additions & 0 deletions
31
packages/openchs-android/src/framework/errorHandling/ErrorUtil.js
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,31 @@ | ||
import StackTrace from "stacktrace-js"; | ||
import AvniError from "./AvniError"; | ||
import ErrorStackParser from "error-stack-parser"; | ||
|
||
function createNavigableStackTrace(stackFrames) { | ||
return stackFrames.map((sf) => { | ||
return `at ${sf.toString()}`; | ||
}).join('\n'); | ||
} | ||
|
||
class ErrorUtil { | ||
//Errors can potentially un-streamed | ||
static createBugsnagStackFrames(error) { | ||
return StackTrace.fromError(error, {offline: true}) | ||
.then((x) => { | ||
return x.map((row) => Object.defineProperty(row, 'fileName', { | ||
value: `${row.fileName}:${row.lineNumber || 0}:${row.columnNumber || 0}` | ||
})); | ||
}); | ||
} | ||
|
||
static getAvniErrorSync(error) { | ||
return AvniError.createFromUserMessageAndStackTrace(error.message, createNavigableStackTrace(ErrorStackParser.parse(error))); | ||
} | ||
|
||
static getNavigableStackTraceSync(error) { | ||
return createNavigableStackTrace(ErrorStackParser.parse(error)) | ||
} | ||
} | ||
|
||
export default ErrorUtil; |
20 changes: 20 additions & 0 deletions
20
packages/openchs-android/src/framework/errorHandling/UnhandledErrorView.js
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,20 @@ | ||
import React, {Component} from 'react'; | ||
import {Image, View} from "react-native"; | ||
import PropTypes from "prop-types"; | ||
import {ErrorDisplay} from "./ErrorDisplay"; | ||
import {JSONStringify} from "../../utility/JsonStringify"; | ||
|
||
export default class UnhandledErrorView extends Component { | ||
static propTypes = { | ||
avniError: PropTypes.object.isRequired | ||
}; | ||
|
||
render() { | ||
console.log("UnhandledErrorView", JSONStringify(this.props.avniError)); | ||
return <View style={{flex: 1}}> | ||
<Image source={{uri: `asset:/logo.png`}} | ||
style={{height: 120, width: 120, alignSelf: 'center'}} resizeMode={'center'}/> | ||
<ErrorDisplay avniError={this.props.avniError}/> | ||
</View> | ||
} | ||
} |
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
Oops, something went wrong.