# All files / src/wx-mini handleWxEvents.ts

25.76% Statements 17/66
0% Branches 0/10
0% Functions 0/15
20.97% Lines 13/62

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
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    2461x
    1x

    1x
    1x
    1x
    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x
     import { BREADCRUMBTYPES, ERRORTYPES, HTTPTYPE } from '@/common/constant'
    import { breadcrumb, httpTransform, transportData } from '../core'
    import { ReportDataType } from '@/types'
    import { WxLifeCycleBreadcrumb, WxOnShareAppMessageBreadcrumb, WxOnTabItemTapBreadcrumb } from '@/types/breadcrumb'
    import { Replace } from '@/types/replace'
    import { extractErrorStack, getTimestamp, isError, isHttpFail, setUrlQuery, parseErrorString, unknownToString } from '@/utils'
    import { Severity } from '@/utils/Severity'
    import { getCurrentRoute, targetAsString } from './utils'
    import { HandleEvents } from '@/browser/handleEvents'
    import { MITOHttp } from '@/types/common'
    import { MiniRoute } from './types'
    import { ELinstenerTypes } from './constant'

    const HandleWxAppEvents = {
      onLaunch(options: WechatMiniprogram.App.LaunchShowOption) {
        const data: WxLifeCycleBreadcrumb = {
          path: options.path,
          query: options.query
        }
        breadcrumb.push({
          category: breadcrumb.getCategory(BREADCRUMBTYPES.APP_ON_LAUNCH),
          type: BREADCRUMBTYPES.APP_ON_LAUNCH,
          data,
          level: Severity.Info
        })
      },
      onShow(options: WechatMiniprogram.App.LaunchShowOption) {
        const data: WxLifeCycleBreadcrumb = {
          path: options.path,
          query: options.query
        }
        breadcrumb.push({
          category: breadcrumb.getCategory(BREADCRUMBTYPES.APP_ON_SHOW),
          type: BREADCRUMBTYPES.APP_ON_SHOW,
          data,
          level: Severity.Info
        })
      },
      onHide() {
        breadcrumb.push({
          category: breadcrumb.getCategory(BREADCRUMBTYPES.APP_ON_HIDE),
          type: BREADCRUMBTYPES.APP_ON_HIDE,
          data: null,
          level: Severity.Info
        })
      },
      onError(error: string) {
        const parsedError = parseErrorString(error)
        const data: ReportDataType = {
          ...parsedError,
          time: getTimestamp(),
          level: Severity.Normal,
          url: getCurrentRoute(),
          type: ERRORTYPES.JAVASCRIPT_ERROR
        }
        breadcrumb.push({
          category: breadcrumb.getCategory(BREADCRUMBTYPES.CODE_ERROR),
          type: BREADCRUMBTYPES.CODE_ERROR,
          level: Severity.Error,
          data
        })
        transportData.send(data)
      },
      onUnhandledRejection(ev: WechatMiniprogram.OnUnhandledRejectionCallbackResult) {
        let data: ReportDataType = {
          type: ERRORTYPES.PROMISE_ERROR,
          message: unknownToString(ev.reason),
          url: getCurrentRoute(),
          name: 'unhandledrejection', // 小程序当初onUnhandledRejection回调中无type参数，故写死
          time: getTimestamp(),
          level: Severity.Low
        }
        if (isError(ev.reason)) {
          data = {
            ...data,
            ...extractErrorStack(ev.reason, Severity.Low),
            url: getCurrentRoute()
          }
        }
        breadcrumb.push({
          type: BREADCRUMBTYPES.UNHANDLEDREJECTION,
          category: breadcrumb.getCategory(BREADCRUMBTYPES.UNHANDLEDREJECTION),
          data: data,
          level: Severity.Error
        })
        transportData.send(data)
      },
      onPageNotFound(data: WechatMiniprogram.OnPageNotFoundCallbackResult) {
        breadcrumb.push({
          category: breadcrumb.getCategory(BREADCRUMBTYPES.ROUTE),
          type: BREADCRUMBTYPES.ROUTE,
          data,
          level: Severity.Error
        })
      }
    }

    const HandleWxPageEvents = {
      onShow() {
        const page = getCurrentPages().pop()
        const data: WxLifeCycleBreadcrumb = {
          path: page.route,
          query: page.options
        }
        breadcrumb.push({
          category: breadcrumb.getCategory(BREADCRUMBTYPES.PAGE_ON_SHOW),
          type: BREADCRUMBTYPES.PAGE_ON_SHOW,
          data,
          level: Severity.Info
        })
      },
      onHide() {
        // console.log('page onHide')
        const page = getCurrentPages().pop()
        const data: WxLifeCycleBreadcrumb = {
          path: page.route,
          query: page.options
        }
        breadcrumb.push({
          category: breadcrumb.getCategory(BREADCRUMBTYPES.PAGE_ON_HIDE),
          type: BREADCRUMBTYPES.PAGE_ON_HIDE,
          data,
          level: Severity.Info
        })
      },
      onShareAppMessage(options: WechatMiniprogram.Page.IShareAppMessageOption) {
        // console.log('page onShareAppMessage')
        const page = getCurrentPages().pop()
        const data: WxOnShareAppMessageBreadcrumb = {
          path: page.route,
          query: page.options,
          options
        }
        breadcrumb.push({
          category: breadcrumb.getCategory(BREADCRUMBTYPES.PAGE_ON_SHARE_APP_MESSAGE),
          type: BREADCRUMBTYPES.PAGE_ON_SHARE_APP_MESSAGE,
          data,
          level: Severity.Info
        })
      },
      onShareTimeline() {
        // console.log('page onShareTimeline')
        const page = getCurrentPages().pop()
        const data: WxLifeCycleBreadcrumb = {
          path: page.route,
          query: page.options
        }
        breadcrumb.push({
          category: breadcrumb.getCategory(BREADCRUMBTYPES.PAGE_ON_SHARE_TIMELINE),
          type: BREADCRUMBTYPES.PAGE_ON_SHARE_TIMELINE,
          data,
          level: Severity.Info
        })
      },
      onTabItemTap(options: WechatMiniprogram.Page.ITabItemTapOption) {
        // console.log('page onTabItemTap')
        const page = getCurrentPages().pop()
        const data: WxOnTabItemTapBreadcrumb = {
          path: page.route,
          query: page.options,
          options
        }
        breadcrumb.push({
          category: breadcrumb.getCategory(BREADCRUMBTYPES.PAGE_ON_TAB_ITEM_TAP),
          type: BREADCRUMBTYPES.PAGE_ON_TAB_ITEM_TAP,
          data,
          level: Severity.Info
        })
      },
      onAction(e: WechatMiniprogram.BaseEvent) {
        let type = BREADCRUMBTYPES.TOUCHMOVE
        if (e.type === ELinstenerTypes.Tap) {
          type = BREADCRUMBTYPES.TAP
        }
        breadcrumb.push({
          category: breadcrumb.getCategory(type),
          type,
          data: targetAsString(e),
          level: Severity.Info
        })
      }
    }

    const HandleWxConsoleEvents = {
      console(data: Replace.TriggerConsole) {
        HandleEvents.handleConsole(data)
      }
    }

    const HandleNetworkEvents = {
      handleRequest(data: MITOHttp): void {
        const result = httpTransform(data)
        result.url = getCurrentRoute()
        if (data.status === undefined) {
          result.message = data.errMsg
        }
        const type = BREADCRUMBTYPES.XHR
        breadcrumb.push({
          type,
          category: breadcrumb.getCategory(type),
          data: result,
          level: Severity.Info
        })
        if (isHttpFail(data.status)) {
          breadcrumb.push({
            type,
            category: breadcrumb.getCategory(BREADCRUMBTYPES.CODE_ERROR),
            data: result,
            level: Severity.Error
          })
          transportData.send(result)
        }
      }
    }

    const HandleWxEvents = {
      handleRoute(data: MiniRoute) {
        if (data.isFail) {
          breadcrumb.push({
            type: BREADCRUMBTYPES.ROUTE,
            category: breadcrumb.getCategory(BREADCRUMBTYPES.CODE_ERROR),
            data,
            level: Severity.Error
          })

          const reportData = {
            type: ERRORTYPES.ROUTE_ERROR,
            message: data.message,
            url: data.to,
            name: 'MINI_' + ERRORTYPES.ROUTE_ERROR,
            level: Severity.Error
          }

          return transportData.send(reportData)
        }
        breadcrumb.push({
          type: BREADCRUMBTYPES.ROUTE,
          category: breadcrumb.getCategory(BREADCRUMBTYPES.ROUTE),
          data,
          level: Severity.Info
        })
      }
    }

    export { HandleWxAppEvents, HandleWxPageEvents, HandleWxConsoleEvents, HandleNetworkEvents, HandleWxEvents }