-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
9 changed files
with
156 additions
and
43 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
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 |
---|---|---|
@@ -1,5 +1,7 @@ | ||
import React, { ErrorInfo } from "react"; | ||
import { AppState, SetAppState } from "../appState"; | ||
import { AppState, SetAppState, initialAppState } from "../appState"; | ||
import { Dialog, Modal } from "react-aria-components"; | ||
import { formatBytes } from "../helpers"; | ||
|
||
interface Props { | ||
appState: AppState; | ||
|
@@ -10,6 +12,18 @@ interface Props { | |
export class ErrorBoundary extends React.Component<Props> { | ||
constructor(props: Props) { | ||
super(props); | ||
this.handlePromiseRejection = this.handlePromiseRejection.bind(this); | ||
} | ||
|
||
override componentDidMount(): void { | ||
window.addEventListener("unhandledrejection", this.handlePromiseRejection); | ||
} | ||
|
||
override componentWillUnmount(): void { | ||
window.removeEventListener( | ||
"unhandledrejection", | ||
this.handlePromiseRejection, | ||
); | ||
} | ||
|
||
override componentDidCatch(error: Error, errorInfo: ErrorInfo) { | ||
|
@@ -30,41 +44,113 @@ export class ErrorBoundary extends React.Component<Props> { | |
}; | ||
} | ||
|
||
getMailTo(error?: Error) { | ||
let mailTo = `mailto:TODO@TODO?subject=SDT2%20crash`; | ||
handlePromiseRejection(e: PromiseRejectionEvent) { | ||
this.props.setAppState((previous) => { | ||
return { | ||
...previous, | ||
client: { | ||
...previous.client, | ||
error: new Error(e.reason), | ||
errorInfo: e.reason, | ||
}, | ||
}; | ||
}); | ||
} | ||
|
||
getMailTo(error?: Error, details = "") { | ||
let mailTo = `mailto:[email protected]?subject=SDT2%20Issue`; | ||
|
||
if (error) { | ||
mailTo = `${mailTo}:%20${error.message}&body=${encodeURI( | ||
error.stack || "", | ||
)}`; | ||
mailTo = `${mailTo}:%20${error.message}&body=${encodeURI(details)}`; | ||
} | ||
|
||
return mailTo; | ||
} | ||
|
||
getIssueUrl(error?: Error, details = "") { | ||
const url = new URL("https://github.com/SDT-org/SDT2/issues/new"); | ||
|
||
if (!error) { | ||
return url.toString(); | ||
} | ||
|
||
url.searchParams.set("labels", "bug"); | ||
url.searchParams.set("title", error.message); | ||
url.searchParams.set("body", details); | ||
|
||
return url.toString(); | ||
} | ||
|
||
override render() { | ||
const error = this.props.appState.client.error; | ||
const errorInfo = this.props.appState.client.errorInfo; | ||
const platform = this.props.appState.platform; | ||
const stats = this.props.appState.compute_stats; | ||
const objectToHuman = (obj?: Object) => | ||
Object.entries(obj ?? {}) | ||
.map((v) => `${v[0].toUpperCase()}: ${v[1]}`) | ||
.join("\n"); | ||
|
||
const errorDetails = [ | ||
errorInfo?.stack, | ||
error?.stack, | ||
errorInfo?.componentStack, | ||
objectToHuman({ ...platform, memory: formatBytes(platform.memory) }), | ||
objectToHuman({ | ||
...stats, | ||
available_memory: formatBytes(stats?.available_memory || 0), | ||
required_memory: formatBytes(stats?.required_memory || 0), | ||
}), | ||
] | ||
.filter(Boolean) | ||
.join("\n\n---\n\n"); | ||
|
||
const resetAppError = () => | ||
this.props.setAppState((previous) => { | ||
return { | ||
...previous, | ||
client: { | ||
...previous.client, | ||
error: null, | ||
errorInfo: null, | ||
}, | ||
}; | ||
}); | ||
|
||
const resetApp = () => this.props.setAppState(initialAppState); | ||
|
||
if (error) { | ||
return ( | ||
<div className="app-main centered error-screen"> | ||
<h1>Something went wrong.</h1> | ||
<p> | ||
Please{" "} | ||
<a href={this.getMailTo(error)} target="_blank"> | ||
send us an email | ||
</a>{" "} | ||
with the error details. | ||
</p> | ||
<details open={true}> | ||
<summary>{error?.message.toString()}</summary> | ||
<pre>{this.props.appState.client.errorInfo?.componentStack}</pre> | ||
</details> | ||
<details> | ||
<summary>App State</summary> | ||
<pre>{JSON.stringify(this.props.appState, null, 2)}</pre> | ||
</details> | ||
</div> | ||
<Modal isDismissable isOpen={true} onOpenChange={resetAppError}> | ||
<Dialog> | ||
<div className="error-modal"> | ||
<h1>Something went wrong.</h1> | ||
<p> | ||
Please{" "} | ||
<a href={this.getMailTo(error, errorDetails)} target="_blank"> | ||
send us an email | ||
</a>{" "} | ||
with the error details, or{" "} | ||
<a href={this.getIssueUrl(error, errorDetails)} target="_blank"> | ||
open an issue | ||
</a> | ||
. | ||
</p> | ||
<details open={true}> | ||
<summary>{error?.message.toString()}</summary> | ||
<pre>{errorDetails}</pre> | ||
</details> | ||
<details> | ||
<summary>App State</summary> | ||
<pre>{JSON.stringify(this.props.appState, null, 2)}</pre> | ||
</details> | ||
</div> | ||
<div className="actions space-between"> | ||
<button onClick={resetApp}>Reset</button> | ||
<button onClick={resetAppError}>Continue</button> | ||
</div> | ||
</Dialog> | ||
</Modal> | ||
); | ||
} | ||
|
||
|
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
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