Skip to content

Commit

Permalink
feat: API explorer error boundary (#753)
Browse files Browse the repository at this point in the history
# 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
bryans99 authored Jul 14, 2021
1 parent dcbf642 commit 3321bf1
Show file tree
Hide file tree
Showing 19 changed files with 559 additions and 43 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@
"ts-jest": "^26.4.4",
"ts-node": "^9.1.1",
"typescript": "^4.1.3",
"url-loader": "^4.1.1",
"webpack": "^5.10.0",
"webpack-bundle-analyzer": "^4.4.1",
"webpack-merge": "^5.7.3"
Expand Down
75 changes: 38 additions & 37 deletions packages/api-explorer/src/ApiExplorer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,14 @@ import styled, { createGlobalStyle } from 'styled-components'
import { Aside, ComponentsProvider, Layout, Page } from '@looker/components'
import { Looker40SDK, Looker31SDK } from '@looker/sdk'
import { SpecList } from '@looker/sdk-codegen'

import {
SearchContext,
LodeContext,
defaultLodeContextValue,
EnvAdaptorContext,
} from './context'
import { EnvAdaptorConstants, getLoded, IApixEnvAdaptor } from './utils'
import { Header, SideNav } from './components'
import { Header, SideNav, ErrorBoundary } from './components'
import {
specReducer,
initDefaultSpecState,
Expand Down Expand Up @@ -125,42 +124,44 @@ const ApiExplorer: FC<ApiExplorerProps> = ({
loadGoogleFonts={loadGoogleFonts}
themeCustomizations={themeCustomizations}
>
<EnvAdaptorContext.Provider value={{ envAdaptor }}>
<LodeContext.Provider value={{ ...lode }}>
<SearchContext.Provider
value={{ searchSettings, setSearchSettings }}
>
<Page style={{ overflow: 'hidden' }}>
{!headless && (
<Header
specs={specs}
spec={spec}
specDispatch={specDispatch}
toggleNavigation={toggleNavigation}
/>
)}
<Layout hasAside height="100%">
{hasNavigation && (
<AsideBorder pt="large" width="20rem">
<SideNav
headless={headless}
specs={specs}
spec={spec}
specDispatch={specDispatch}
/>
</AsideBorder>
<ErrorBoundary logError={envAdaptor.logError.bind(envAdaptor)}>
<EnvAdaptorContext.Provider value={{ envAdaptor }}>
<LodeContext.Provider value={{ ...lode }}>
<SearchContext.Provider
value={{ searchSettings, setSearchSettings }}
>
<Page style={{ overflow: 'hidden' }}>
{!headless && (
<Header
specs={specs}
spec={spec}
specDispatch={specDispatch}
toggleNavigation={toggleNavigation}
/>
)}
<AppRouter
api={spec.api}
specKey={spec.key}
specs={specs}
toggleNavigation={toggleNavigation}
/>
</Layout>
</Page>
</SearchContext.Provider>
</LodeContext.Provider>
</EnvAdaptorContext.Provider>
<Layout hasAside height="100%">
{hasNavigation && (
<AsideBorder pt="large" width="20rem">
<SideNav
headless={headless}
specs={specs}
spec={spec}
specDispatch={specDispatch}
/>
</AsideBorder>
)}
<AppRouter
api={spec.api}
specKey={spec.key}
specs={specs}
toggleNavigation={toggleNavigation}
/>
</Layout>
</Page>
</SearchContext.Provider>
</LodeContext.Provider>
</EnvAdaptorContext.Provider>
</ErrorBoundary>
</ComponentsProvider>
{!headless && <BodyOverride />}
</>
Expand Down
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()
})
})
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 || <></>
}
}
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()
})
})
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};
`
Loading

0 comments on commit 3321bf1

Please sign in to comment.