All files / modules/10-common/components/ErrorHandler ErrorHandler.tsx

97.14% Statements 34/35
64.71% Branches 11/17
100% Functions 6/6
96.97% Lines 32/33

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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131              291x 291x 291x 291x   291x 291x 291x                     291x             3x 3x 3x 3x 5x 1x         1x 1x 4x 1x 3x 1x     3x             291x 2x     2x               2x                         291x 3x 3x 3x   3x                   1x 1x                                                                
/*
 * 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, { useMemo } from 'react'
import cx from 'classnames'
import { Layout, Text, Icon, IconName, Container } from '@wings-software/uicore'
import { Color } from '@harness/design-system'
import type { ResponseMessage } from 'services/cd-ng'
import { useStrings } from 'framework/strings'
import { LinkifyText } from '@common/components/LinkifyText/LinkifyText'
import css from '@common/components/ErrorHandler/ErrorHandler.module.scss'
 
export interface ErrorHandlerProps {
  responseMessages: ResponseMessage[]
  width?: number | string
  height?: number | string
  skipUrlsInErrorHeader?: boolean
  className?: string
  errorHintsRenderer?: (item: ResponseMessage[]) => React.ReactElement
}
 
const extractInfo = (
  responseMessages: ErrorHandlerProps['responseMessages']
): {
  error?: ResponseMessage
  explanations?: ResponseMessage[]
  hints?: ResponseMessage[]
}[] => {
  const errorObjects = []
  let explanations: ResponseMessage[] = []
  let hints: ResponseMessage[] = []
  for (const message of responseMessages) {
    if (message.level === 'ERROR') {
      errorObjects.push({
        error: message,
        explanations,
        hints
      })
      explanations = []
      hints = []
    } else if (message.code === 'HINT') {
      hints.push(message)
    } else if (message.code === 'EXPLANATION') {
      explanations.push(message)
    }
  }
  return errorObjects
}
 
const ErrorList: React.FC<{
  items: ResponseMessage[]
  header: string
  icon: IconName
}> = props => {
  Iif (!props.items.length) {
    return null
  }
  return (
    <Layout.Horizontal margin={{ bottom: 'xlarge' }}>
      <Icon name={props.icon} margin={{ right: 'small' }} />
      <Layout.Vertical className={cx(css.errorListTextContainer, css.shrink)}>
        <Text font={{ weight: 'semi-bold', size: 'small' }} color={Color.BLACK} margin={{ bottom: 'xsmall' }}>
          {props.header}
        </Text>
        {props.items.map((item, index) => (
          <Container margin={{ bottom: 'xsmall' }} key={index}>
            <LinkifyText
              content={`- ${item.message}`}
              textProps={{ color: Color.BLACK, font: { size: 'small' }, className: css.text }}
              linkStyles={cx(css.link, css.linkSmall)}
            />
          </Container>
        ))}
      </Layout.Vertical>
    </Layout.Horizontal>
  )
}
 
export const ErrorHandler: React.FC<ErrorHandlerProps> = props => {
  const { responseMessages, width, height, errorHintsRenderer, skipUrlsInErrorHeader = false, className = '' } = props
  const errorObjects = useMemo(() => extractInfo(responseMessages), [responseMessages])
  const { getString } = useStrings()
 
  return (
    <Layout.Vertical
      background={Color.RED_100}
      padding={{ top: 'medium', bottom: 'medium', left: 'medium', right: 'medium' }}
      className={cx(css.container, css.shrink, className)}
      width={width}
      height={height}
    >
      <Container className={css.scroll}>
        {errorObjects.map((errorObject, index) => {
          const { error = {}, explanations = [], hints = [] } = errorObject
          return (
            <Layout.Vertical key={index} className={css.shrink}>
              <Container margin={{ bottom: 'medium' }}>
                {skipUrlsInErrorHeader ? (
                  <Text font={{ weight: 'bold' }} color={Color.RED_700} className={css.text}>
                    {error.message}
                  </Text>
                ) : (
                  <LinkifyText
                    content={error.message}
                    textProps={{ font: { weight: 'bold' }, color: Color.RED_700, className: css.text }}
                    linkStyles={css.link}
                  />
                )}
              </Container>
              {<ErrorList items={explanations} header={getString('common.errorHandler.issueCouldBe')} icon={'info'} />}
              {errorHintsRenderer ? (
                errorHintsRenderer(hints)
              ) : (
                <ErrorList
                  items={hints}
                  header={getString('common.errorHandler.tryTheseSuggestions')}
                  icon={'lightbulb'}
                />
              )}
            </Layout.Vertical>
          )
        })}
      </Container>
    </Layout.Vertical>
  )
}