-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: API explorer error boundary (#753)
# Description Adds an error boundary around APIX explorer child components to display a "nice" message should an error occur during component renders (note, error boundaries do not help with asynchronous errors). # Changes 1. The error boundary and its child components. 2. Added support to environment adapter for logging. Only extension api explorer logs at the moment as logging strategy for stand alone apix explorer is TBD. 3, Added ability to use svgs in react code. Had to add `custom.d.ts` file to api-extension and extension-api-extension projects for `tsc` compiler to work. There may be better ways to do this but that can be added later.
- Loading branch information
Showing
19 changed files
with
559 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
72 changes: 72 additions & 0 deletions
72
packages/api-explorer/src/components/ErrorBoundary/ErrorBoundary.spec.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,72 @@ | ||
/* | ||
MIT License | ||
Copyright (c) 2021 Looker Data Sciences, Inc. | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
*/ | ||
import { renderWithTheme } from '@looker/components-test-utils' | ||
import React from 'react' | ||
import { ErrorBoundary } from './ErrorBoundary' | ||
|
||
const ErrorComp = ({ text }: { text: string }) => { | ||
const terminateWithPrejudice = () => { | ||
const badObj: any = {} | ||
return badObj.a.b | ||
} | ||
return <div>{text + terminateWithPrejudice()}</div> | ||
} | ||
|
||
describe('ErrorBoundary', () => { | ||
const header = 'Uh oh! Something went wrong!' | ||
const actionMessage = 'Try refreshing the page the correct the problem.' | ||
const childText = 'Sweet Child' | ||
|
||
beforeEach(() => { | ||
jest.spyOn(console, 'error').mockImplementation() | ||
}) | ||
|
||
it('should render child component', () => { | ||
const logError = jest.fn() | ||
const { getByText, queryByText } = renderWithTheme( | ||
<ErrorBoundary logError={logError}> | ||
<div>{childText}</div> | ||
</ErrorBoundary> | ||
) | ||
expect(queryByText(header)).not.toBeInTheDocument() | ||
expect(queryByText(actionMessage)).not.toBeInTheDocument() | ||
expect(getByText(childText)).toBeVisible() | ||
expect(logError).not.toHaveBeenCalled() | ||
}) | ||
|
||
it('should render error detail on error', () => { | ||
const logError = jest.fn() | ||
const { getByText, queryByText } = renderWithTheme( | ||
<ErrorBoundary logError={logError}> | ||
<ErrorComp text={childText} /> | ||
</ErrorBoundary> | ||
) | ||
expect(queryByText(childText)).not.toBeInTheDocument() | ||
expect(getByText(header)).toBeVisible() | ||
expect(getByText(actionMessage)).toBeVisible() | ||
expect(logError).toHaveBeenCalled() | ||
}) | ||
}) |
72 changes: 72 additions & 0 deletions
72
packages/api-explorer/src/components/ErrorBoundary/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,72 @@ | ||
/* | ||
MIT License | ||
Copyright (c) 2021 Looker Data Sciences, Inc. | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
*/ | ||
|
||
import React, { Component } from 'react' | ||
import type { ReactNode, ErrorInfo } from 'react' | ||
import { SomethingWentWrong } from './components/SomethingWentWrong' | ||
|
||
interface ErrorBoundaryProps { | ||
children: ReactNode | ||
logError: (error: Error, componentStack: string) => void | ||
} | ||
|
||
interface ErrorBoundaryState { | ||
hasError: boolean | ||
errorMessage: Error | ||
} | ||
|
||
export class ErrorBoundary extends Component< | ||
ErrorBoundaryProps, | ||
ErrorBoundaryState | ||
> { | ||
constructor(props: ErrorBoundaryProps) { | ||
super(props) | ||
this.state = { hasError: false, errorMessage: new Error('') } | ||
} | ||
|
||
static getDerivedStateFromError(error: Error) { | ||
return { hasError: true, errorMessage: error } | ||
} | ||
|
||
componentDidCatch(error: Error, errorInfo: ErrorInfo) { | ||
const { logError } = this.props | ||
logError(error, errorInfo.componentStack) | ||
} | ||
|
||
render() { | ||
const { hasError } = this.state | ||
const { children } = this.props | ||
if (hasError) { | ||
return ( | ||
<SomethingWentWrong | ||
header="Uh oh! Something went wrong!" | ||
actionMessage="Try refreshing the page the correct the problem." | ||
/> | ||
) | ||
} | ||
return children || <></> | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
...er/src/components/ErrorBoundary/components/SomethingWentWrong/SomethingWentWrong.spec.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,58 @@ | ||
/* | ||
MIT License | ||
Copyright (c) 2021 Looker Data Sciences, Inc. | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
*/ | ||
import { renderWithTheme } from '@looker/components-test-utils' | ||
import React from 'react' | ||
import { SomethingWentWrong } from './SomethingWentWrong' | ||
|
||
const getMockedComponent = (propOverrides = {}) => { | ||
const defaultProps = { | ||
header: '', | ||
actionMessage: '', | ||
altText: '', | ||
} | ||
const updatedProps = { ...defaultProps, ...propOverrides } | ||
return <SomethingWentWrong {...updatedProps} /> | ||
} | ||
|
||
describe('SomethingWentWrong', () => { | ||
it('Should render proper component with correct texts', () => { | ||
const header = 'Uh oh something went wrong' | ||
const actionMessage = 'User please do this action. It might help' | ||
const altText = '500 error graphic' | ||
|
||
const { getByText, getByAltText } = renderWithTheme( | ||
getMockedComponent({ | ||
header, | ||
actionMessage, | ||
altText, | ||
}) | ||
) | ||
|
||
expect(getByText(header)).toBeVisible() | ||
expect(getByText(actionMessage)).toBeVisible() | ||
expect(getByAltText(altText)).toBeVisible() | ||
}) | ||
}) |
75 changes: 75 additions & 0 deletions
75
...xplorer/src/components/ErrorBoundary/components/SomethingWentWrong/SomethingWentWrong.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,75 @@ | ||
/* | ||
MIT License | ||
Copyright (c) 2021 Looker Data Sciences, Inc. | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
*/ | ||
import React from 'react' | ||
import { Box, Flex } from '@looker/components' | ||
import styled from 'styled-components' | ||
import { SomethingWentWrongGraphic } from './components' | ||
|
||
export interface SomethingWentWrongProps { | ||
header: string | ||
actionMessage: string | ||
altText?: string | ||
} | ||
|
||
export const SomethingWentWrong: React.FC<SomethingWentWrongProps> = ({ | ||
header, | ||
actionMessage, | ||
altText, | ||
}) => ( | ||
<OuterFlex> | ||
<SomethingWentWrongGraphic altText={altText} /> | ||
<HeaderText>{header}</HeaderText> | ||
<Box width={520}> | ||
<ActionMessage>{actionMessage}</ActionMessage> | ||
</Box> | ||
</OuterFlex> | ||
) | ||
|
||
const OuterFlex = styled(Flex)` | ||
width: 100%; | ||
margin-top: ${(props) => props.theme.space.xxxxlarge}; | ||
justify-content: center; | ||
flex-direction: column; | ||
align-items: center; | ||
` | ||
|
||
const HeaderText = styled.h1` | ||
margin-top: ${(props) => props.theme.space.large}; | ||
text-align: center; | ||
font-family: ${(props) => props.theme.fonts.brand}; | ||
font-weight: ${(props) => props.theme.fontWeights.normal}; | ||
font-size: ${(props) => props.theme.fontSizes.xxxxlarge}; | ||
color: ${(props) => props.theme.colors.text5}; | ||
` | ||
|
||
const ActionMessage = styled.h3` | ||
margin-top: ${(props) => props.theme.space.small}; | ||
text-align: center; | ||
font-family: ${(props) => props.theme.fonts.brand}; | ||
font-weight: ${(props) => props.theme.fontWeights.normal}; | ||
font-size: ${(props) => props.theme.fontSizes.large}; | ||
color: ${(props) => props.theme.colors.text3}; | ||
` |
Oops, something went wrong.