File size: 1,719 Bytes
1f21206
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import React from 'react'
import { rawRecordDiagnosticEvent } from '../api/client'

let installed = false

export function installClientDiagnosticsCapture() {
  if (installed || typeof window === 'undefined') return
  installed = true

  window.addEventListener('error', (event) => {
    void reportClientError('client_window_error', event.message || 'Window error', {
      filename: event.filename,
      lineno: event.lineno,
      colno: event.colno,
      error: normalizeError(event.error),
    })
  })

  window.addEventListener('unhandledrejection', (event) => {
    void reportClientError('client_unhandled_rejection', summarizeUnknown(event.reason), {
      reason: normalizeError(event.reason),
    })
  })
}

export function reportReactError(error: unknown, errorInfo: React.ErrorInfo) {
  return reportClientError('client_react_error_boundary', summarizeUnknown(error), {
    error: normalizeError(error),
    componentStack: errorInfo.componentStack,
  })
}

function reportClientError(type: string, summary: string, details: Record<string, unknown>) {
  return rawRecordDiagnosticEvent({
    type,
    severity: 'error',
    summary,
    details: {
      url: window.location.href,
      userAgent: navigator.userAgent,
      ...details,
    },
  })
}

function summarizeUnknown(value: unknown): string {
  if (value instanceof Error) return value.message || value.name
  if (typeof value === 'string') return value
  try {
    return JSON.stringify(value)
  } catch {
    return String(value)
  }
}

function normalizeError(value: unknown): unknown {
  if (value instanceof Error) {
    return {
      name: value.name,
      message: value.message,
      stack: value.stack,
    }
  }
  return value
}