All files / framework/utils/AppErrorBoundary AppErrorBoundary.tsx

86.67% Statements 13/15
81.82% Branches 18/22
75% Functions 3/4
86.67% Lines 13/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68              119x 119x 119x             3x     2x 2x 1x   2x       5x   5x 2x                                                               3x       119x  
/*
 * Copyright 2021 Harness Inc. All rights reserved.
 * Use of this source code is governed by the PolyForm Shield 1.0.0 license
 * that can be found in the licenses directory at the root of this repository, also available at
 * https://polyformproject.org/wp-content/uploads/2020/06/PolyForm-Shield-1.0.0.txt.
 */
 
import React from 'react'
import { Text, Layout, Container } from '@wings-software/uicore'
import i18n from './AppErrorBoundary.i18n.json'
 
interface AppErrorBoundaryState {
  error?: Error
}
 
class AppErrorBoundary extends React.Component<unknown, AppErrorBoundaryState> {
  state: AppErrorBoundaryState = { error: undefined }
 
  componentDidCatch(error: Error): boolean {
    this.setState({ error })
    if (window?.bugsnagClient?.notify) {
      window?.bugsnagClient?.notify(error)
    }
    return false
  }
 
  render(): React.ReactNode {
    const { error } = this.state
 
    if (error) {
      return (
        <Layout.Vertical spacing="medium" padding="large">
          <Text>{i18n.title}</Text>
          <Text>{i18n.subtitle}</Text>
          <Text>
            {i18n.please}
            <a
              href="#"
              onClick={e => {
                e.preventDefault()
                window.location.reload()
              }}
            >
              {i18n.refresh}
            </a>
            {i18n.continue}
          </Text>
          {__DEV__ && (
            <React.Fragment>
              <Text font="small">{error.message}</Text>
              <Container>
                <details>
                  <summary>{i18n.stackTrace}</summary>
                  <pre>{error.stack}</pre>
                </details>
              </Container>
            </React.Fragment>
          )}
        </Layout.Vertical>
      )
    }
 
    return <>{this.props.children}</>
  }
}
 
export default AppErrorBoundary