-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ADDON-34750: feat: ucc_ui_lib: add dynamic data loading and error bou…
…ndary
- Loading branch information
1 parent
200792a
commit b30ea13
Showing
7 changed files
with
266 additions
and
602 deletions.
There are no files selected for viewing
60 changes: 60 additions & 0 deletions
60
splunk_add_on_ucc_framework/ucc_ui_lib/src/main/webapp/components/ErrorBoundary.jsx
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,60 @@ | ||
import React from 'react'; | ||
import Heading from '@splunk/react-ui/Heading'; | ||
import Message from '@splunk/react-ui/Message'; | ||
|
||
import errorCodes from '../constants/errorCodes'; | ||
|
||
class ErrorBoundary extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
this.state = { errorCode: null, error: null, errorInfo: null }; | ||
} | ||
|
||
static getDerivedStateFromError(error) { | ||
// Update state so the next render will show the fallback UI. | ||
return { errorCode: error.ucc_err_code }; | ||
} | ||
|
||
componentDidCatch(error, errorInfo) { | ||
// Catch errors in any components below and re-render with error message | ||
this.setState({ | ||
error: error, | ||
errorInfo: errorInfo, | ||
}); | ||
// You can also log error messages to an error reporting service here | ||
} | ||
|
||
render() { | ||
if (this.state.errorCode) { | ||
// Error path | ||
return ( | ||
<> | ||
<Heading level={2}> | ||
Something went wrong! (ERROR_CODE: {this.state.errorCode}) | ||
</Heading> | ||
<Message type="info">{errorCodes[this.state.errorCode]}</Message> | ||
<details style={{ whiteSpace: 'pre-wrap' }}> | ||
{this.state.error && this.state.error.toString()} | ||
<br /> | ||
{this.state.errorInfo && this.state.errorInfo.componentStack} | ||
</details> | ||
</> | ||
); | ||
} else if (this.state.error) { | ||
return ( | ||
<> | ||
<Heading level={2}>Something went wrong!</Heading> | ||
<details style={{ whiteSpace: 'pre-wrap' }}> | ||
{this.state.error && this.state.error.toString()} | ||
<br /> | ||
{this.state.errorInfo && this.state.errorInfo.componentStack} | ||
</details> | ||
</> | ||
); | ||
} | ||
// Normally, just render children | ||
return this.props.children; | ||
} | ||
} | ||
|
||
export default ErrorBoundary; |
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.