| 1 |
import * as React from "react"; |
| 2 |
import { Link, Button } from "@mui/material"; |
| 3 |
import { Trans, withTranslation } from "react-i18next"; |
| 4 |
import { copyToClipboard } from "../app/utils"; |
| 5 |
|
| 6 |
class ErrorBoundaryImpl extends React.Component { |
| 7 |
constructor(props) { |
| 8 |
super(props); |
| 9 |
this.state = { |
| 10 |
error: false, |
| 11 |
stack: null, |
| 12 |
unsupportedIndexedDB: false, |
| 13 |
}; |
| 14 |
} |
| 15 |
|
| 16 |
componentDidCatch(error, info) { |
| 17 |
console.error("[ErrorBoundary] Error caught", error, info); |
| 18 |
|
| 19 |
// Special case for unsupported IndexedDB in Private Browsing mode (Firefox, Safari), see |
| 20 |
// - https://github.com/dexie/Dexie.js/issues/312 |
| 21 |
// - https://bugzilla.mozilla.org/show_bug.cgi?id=781982 |
| 22 |
const isUnsupportedIndexedDB = |
| 23 |
error?.name === "InvalidStateError" || (error?.name === "DatabaseClosedError" && error?.message?.indexOf("InvalidStateError") !== -1); |
| 24 |
|
| 25 |
if (isUnsupportedIndexedDB) { |
| 26 |
this.handleUnsupportedIndexedDB(); |
| 27 |
} else { |
| 28 |
this.handleError(error, info); |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
handleError(error, info) { |
| 33 |
const componentStack = info.componentStack |
| 34 |
.trim() |
| 35 |
.split("\n") |
| 36 |
.map((line) => ` at ${line}`) |
| 37 |
.join("\n"); |
| 38 |
const parts = [error.toString()]; |
| 39 |
if (error.stack) parts.push(error.stack); |
| 40 |
parts.push(componentStack); |
| 41 |
this.setState({ |
| 42 |
error: true, |
| 43 |
stack: parts.join("\n"), |
| 44 |
}); |
| 45 |
} |
| 46 |
|
| 47 |
handleUnsupportedIndexedDB() { |
| 48 |
this.setState({ |
| 49 |
error: true, |
| 50 |
unsupportedIndexedDB: true, |
| 51 |
}); |
| 52 |
} |
| 53 |
|
| 54 |
copyStack() { |
| 55 |
copyToClipboard(`${this.state.stack}\n`); |
| 56 |
} |
| 57 |
|
| 58 |
renderUnsupportedIndexedDB() { |
| 59 |
const { t } = this.props; |
| 60 |
return ( |
| 61 |
<div style={{ margin: "20px" }}> |
| 62 |
<h2>{t("error_boundary_unsupported_indexeddb_title")} 😮</h2> |
| 63 |
<p style={{ maxWidth: "600px" }}> |
| 64 |
<Trans |
| 65 |
i18nKey="error_boundary_unsupported_indexeddb_description" |
| 66 |
components={{ |
| 67 |
githubLink: <Link href="https://github.com/binwiederhier/ntfy/issues/208" />, |
| 68 |
discordLink: <Link href="https://discord.gg/cT7ECsZj9w" />, |
| 69 |
matrixLink: <Link href="https://matrix.to/#/#ntfy:matrix.org" />, |
| 70 |
}} |
| 71 |
/> |
| 72 |
</p> |
| 73 |
</div> |
| 74 |
); |
| 75 |
} |
| 76 |
|
| 77 |
renderError() { |
| 78 |
const { t } = this.props; |
| 79 |
return ( |
| 80 |
<div style={{ margin: "20px" }}> |
| 81 |
<h2>{t("error_boundary_title")} 😮</h2> |
| 82 |
<p> |
| 83 |
<Trans |
| 84 |
i18nKey="error_boundary_description" |
| 85 |
components={{ |
| 86 |
githubLink: <Link href="https://github.com/binwiederhier/ntfy/issues" />, |
| 87 |
discordLink: <Link href="https://discord.gg/cT7ECsZj9w" />, |
| 88 |
matrixLink: <Link href="https://matrix.to/#/#ntfy:matrix.org" />, |
| 89 |
}} |
| 90 |
/> |
| 91 |
</p> |
| 92 |
<div style={{ display: "flex", gap: 5 }}> |
| 93 |
<Button variant="outlined" onClick={() => this.copyStack()}> |
| 94 |
{t("error_boundary_button_copy_stack_trace")} |
| 95 |
</Button> |
| 96 |
|
| 97 |
<Button variant="outlined" onClick={() => window.location.reload()}> |
| 98 |
{t("error_boundary_button_reload_ntfy")} |
| 99 |
</Button> |
| 100 |
</div> |
| 101 |
<h3>{t("error_boundary_stack_trace")}</h3> |
| 102 |
<pre>{this.state.stack}</pre> |
| 103 |
</div> |
| 104 |
); |
| 105 |
} |
| 106 |
|
| 107 |
render() { |
| 108 |
if (this.state.error) { |
| 109 |
if (this.state.unsupportedIndexedDB) { |
| 110 |
return this.renderUnsupportedIndexedDB(); |
| 111 |
} |
| 112 |
return this.renderError(); |
| 113 |
} |
| 114 |
return this.props.children; |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
const ErrorBoundary = withTranslation()(ErrorBoundaryImpl); // Adds props.t |
| 119 |
export default ErrorBoundary; |