# All files / src/browser handleEvents.ts

28% Statements 14/50
20% Branches 5/25
14.29% Functions 1/7
28% Lines 14/50

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
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    1622x
    2x
    2x
    2x
    2x

    2x

    2x

    2x
    2x
    2x

    2x
    1x

    1x

    2x
     import { BREADCRUMBTYPES, ERRORTYPES, globalVar, ERROR_TYPE_RE, HTTP_CODE } from '../common/constant'
    import { resourceTransform, httpTransform } from '../core/transformData'
    import { transportData } from '../core/transportData'
    import { breadcrumb } from '../core/breadcrumb'
    import { getLocationHref, getTimestamp, isError, parseUrlToObj, extractErrorStack, unknownToString } from '../utils/index'
    import { ReportDataType } from '../types/transportData'
    import { Severity } from '../utils/Severity'
    import { Replace } from '../types/replace'
    import { MITOHttp } from '../types/common'

    export interface ResourceErrorTarget {
      src?: string
      href?: string
      localName?: string
    }

    const HandleEvents = {
      /**
       * 处理xhr、fetch回调
       */
      handleHttp(data: MITOHttp, type: BREADCRUMBTYPES): void {
        const isError = data.status === 0 || data.status === HTTP_CODE.BAD_REQUEST || data.status > HTTP_CODE.UNAUTHORIZED
        const result = httpTransform(data)
        breadcrumb.push({
          type,
          category: breadcrumb.getCategory(type),
          data: { ...result },
          level: Severity.Info
        })
        if (isError) {
          breadcrumb.push({
            type,
            category: breadcrumb.getCategory(BREADCRUMBTYPES.CODE_ERROR),
            data: { ...result },
            level: Severity.Error
          })
          transportData.send(result)
        }
      },
      /**
       * 处理window的error的监听回到
       */
      handleError(errorEvent: ErrorEvent) {
        const target = errorEvent.target as ResourceErrorTarget
        if (target.localName) {
          // 资源加载错误 提取有用数据
          const data = resourceTransform(errorEvent.target as ResourceErrorTarget)
          breadcrumb.push({
            type: BREADCRUMBTYPES.RESOURCE,
            category: breadcrumb.getCategory(BREADCRUMBTYPES.RESOURCE),
            data,
            level: Severity.Error
          })
          return transportData.send(data)
        }
        // code error
        const { message, filename, lineno, colno, error } = errorEvent
        let result: ReportDataType
        if (error && isError(error)) {
          result = extractErrorStack(error, Severity.Normal)
        }
        // 处理SyntaxError，stack没有lineno、colno
        result || (result = HandleEvents.handleNotErrorInstance(message, filename, lineno, colno))
        result.type = ERRORTYPES.JAVASCRIPT_ERROR
        breadcrumb.push({
          type: BREADCRUMBTYPES.CODE_ERROR,
          category: breadcrumb.getCategory(BREADCRUMBTYPES.CODE_ERROR),
          data: { ...result },
          level: Severity.Error
        })
        transportData.send(result)
      },
      handleNotErrorInstance(message: string, filename: string, lineno: number, colno: number) {
        let name: string | ERRORTYPES = ERRORTYPES.UNKNOWN
        const url = filename || getLocationHref()
        let msg = message
        const matches = message.match(ERROR_TYPE_RE)
        if (matches[1]) {
          name = matches[1]
          msg = matches[2]
        }
        const element = {
          url,
          func: ERRORTYPES.UNKNOWN_FUNCTION,
          args: ERRORTYPES.UNKNOWN,
          line: lineno,
          col: colno
        }
        return {
          url,
          name,
          message: msg,
          level: Severity.Normal,
          time: getTimestamp(),
          stack: [element]
        }
      },
      handleHistory(data: Replace.IRouter): void {
        const { from, to } = data
        const { relative: parsedFrom } = parseUrlToObj(from)
        const { relative: parsedTo } = parseUrlToObj(to)
        breadcrumb.push({
          type: BREADCRUMBTYPES.ROUTE,
          category: breadcrumb.getCategory(BREADCRUMBTYPES.ROUTE),
          data: {
            from: parsedFrom ? parsedFrom : '/',
            to: parsedTo ? parsedTo : '/'
          },
          level: Severity.Info
        })
      },
      handleHashchange(data: HashChangeEvent): void {
        const { oldURL, newURL } = data
        const { relative: from } = parseUrlToObj(oldURL)
        const { relative: to } = parseUrlToObj(newURL)
        breadcrumb.push({
          type: BREADCRUMBTYPES.ROUTE,
          category: breadcrumb.getCategory(BREADCRUMBTYPES.ROUTE),
          data: {
            from,
            to
          },
          level: Severity.Info
        })
      },
      handleUnhandleRejection(ev: PromiseRejectionEvent): void {
        let data: ReportDataType = {
          type: ERRORTYPES.PROMISE_ERROR,
          message: unknownToString(ev.reason),
          url: getLocationHref(),
          name: ev.type,
          time: getTimestamp(),
          level: Severity.Low
        }
        if (isError(ev.reason)) {
          data = {
            ...data,
            ...extractErrorStack(ev.reason, Severity.Low)
          }
        }
        breadcrumb.push({
          type: BREADCRUMBTYPES.UNHANDLEDREJECTION,
          category: breadcrumb.getCategory(BREADCRUMBTYPES.UNHANDLEDREJECTION),
          data: { ...data },
          level: Severity.Error
        })
        transportData.send(data)
      },
      handleConsole(data: Replace.TriggerConsole): void {
        if (globalVar.isLogAddBreadcrumb) {
          breadcrumb.push({
            type: BREADCRUMBTYPES.CONSOLE,
            category: breadcrumb.getCategory(BREADCRUMBTYPES.CONSOLE),
            data,
            level: Severity.fromString(data.level)
          })
        }
      }
    }

    export { HandleEvents }