-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
When the addon crashes due to unexpected configuration or something, the entire Storybook UI crashes and page turns to blank. To reduce the damage, I added ErrorBoundary so only addon UIs die. #164
- Loading branch information
Showing
3 changed files
with
84 additions
and
2 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
67 changes: 67 additions & 0 deletions
67
packages/storybook-addon-designs/src/register/components/ErrorBoundary.tsx
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,67 @@ | ||
/** @jsx jsx */ | ||
import { Component, ErrorInfo, Fragment } from "react"; | ||
import { Link, Placeholder } from "@storybook/components"; | ||
import { jsx } from "@storybook/theming"; | ||
|
||
type State = | ||
| { | ||
hasError: false; | ||
} | ||
| { | ||
hasError: true; | ||
error: unknown; | ||
}; | ||
|
||
export class ErrorBoundary extends Component { | ||
public state: State = { hasError: false }; | ||
|
||
static getDerivedStateFromError(error: unknown): State { | ||
return { | ||
hasError: true, | ||
error, | ||
}; | ||
} | ||
|
||
override componentDidCatch(error: unknown, info: ErrorInfo): void { | ||
console.group( | ||
"An error occurred during rendering Addon panel of storybook-addon-designs" | ||
); | ||
console.log("--- Error ---"); | ||
console.error(error); | ||
console.log("--- React Component Stack ---"); | ||
console.error(info.componentStack); | ||
console.groupEnd(); | ||
} | ||
|
||
override render() { | ||
if (this.state.hasError) { | ||
return ( | ||
<Placeholder> | ||
<Fragment>Failed to render addon UI</Fragment> | ||
<Fragment> | ||
<p> | ||
Sorry, this addon has crashed due to the below error has thrown | ||
during rendering the addon UI. | ||
</p> | ||
<pre>{String(this.state.error)}</pre> | ||
<p> | ||
See console log for more details. To clear the error state, please | ||
reload the page.{" "} | ||
<Link | ||
href="https://github.com/pocka/storybook-addon-designs/issues/new?assignees=&labels=category%3A+bug&template=bug_report.yml" | ||
target="_blank" | ||
rel="noopener" | ||
withArrow | ||
cancel={false} | ||
> | ||
Bug report | ||
</Link> | ||
</p> | ||
</Fragment> | ||
</Placeholder> | ||
); | ||
} | ||
|
||
return this.props.children; | ||
} | ||
} |
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