-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: David Edler <david.edler@canonical.com>
- Loading branch information
Showing
2 changed files
with
100 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import React from "react"; | ||
import { | ||
CodeSnippet, | ||
CodeSnippetBlockAppearance, | ||
Notification, | ||
Strip, | ||
} from "@canonical/react-components"; | ||
import type { PropsWithChildren } from "react"; | ||
import { Component } from "react"; | ||
|
||
type Props = PropsWithChildren; | ||
|
||
type State = { | ||
error?: Error; | ||
hasError: boolean; | ||
}; | ||
|
||
export default class ErrorBoundary extends Component<Props, State> { | ||
constructor(props: Props) { | ||
super(props); | ||
this.state = { hasError: false }; | ||
} | ||
|
||
static getDerivedStateFromError(error: Error) { | ||
return { | ||
hasError: true, | ||
error, | ||
}; | ||
} | ||
|
||
render() { | ||
const { error, hasError } = this.state; | ||
const { children } = this.props; | ||
const body = encodeURIComponent( | ||
`\`\`\`\n${error?.stack ?? "No stack track"}\n\`\`\``, | ||
); | ||
const url = `https://github.com/canonical/lxd-ui/issues/new?labels=bug&title=Error%20report&body=${body}`; | ||
if (hasError) { | ||
return ( | ||
<Strip> | ||
<Notification severity="negative" title="Error"> | ||
Something has gone wrong. If this issue persists,{" "} | ||
<a href={url} rel="noopener noreferrer" target="_blank"> | ||
please raise an issue on GitHub. | ||
</a> | ||
</Notification> | ||
<CodeSnippet | ||
blocks={[ | ||
...(error?.message | ||
? [ | ||
{ | ||
title: "Error", | ||
appearance: CodeSnippetBlockAppearance.NUMBERED, | ||
wrapLines: true, | ||
code: error.message, | ||
}, | ||
] | ||
: []), | ||
...(error?.stack | ||
? [ | ||
{ | ||
title: "Stack trace", | ||
appearance: CodeSnippetBlockAppearance.NUMBERED, | ||
wrapLines: true, | ||
code: error.stack, | ||
}, | ||
] | ||
: []), | ||
]} | ||
/> | ||
</Strip> | ||
); | ||
} | ||
|
||
return <>{children}</>; | ||
} | ||
} |