/** * 兜底上报 */ import { serializeEventPackage, IEvent, EProject, IEventPackage, IPage, IDevice, EPlatform, EventType, getCollectHost, xUserAgent } from '@jolibox/common'; import { testMode } from '@/utils/env'; function getLocation(): IPage { const urlParams = new URLSearchParams(window.location.search); const params: Record = {}; urlParams.forEach((value, key) => { params[key] = value; }); return { name: 'GamePage', params }; } function getDevice(): IDevice { return { platform: 1000, os: 'h5', appVersion: '__JOLIBOX_LOCAL_SDK_VERSION__', // mock appId: '1', model: 'UnknownModel', brand: 'UnknownBrand', uuid: '1', jsSdkVersion: '__JOLIBOX_LOCAL_SDK_VERSION__', // mock extra: {} }; } const apiBaseURL = () => { const api = getCollectHost(testMode ?? false); return api; }; const API_BASE_URL = apiBaseURL(); const timeoutFn = (ms: number) => { const ctrl = new AbortController(); setTimeout(() => ctrl.abort(), ms); return ctrl.signal; }; (AbortSignal as any).timeout ??= timeoutFn; const getJoliSource = () => { const urlParams = new URLSearchParams(window.location.search); return urlParams.get('joliSource') ?? null; }; const JOLI_SOURCE = getJoliSource(); const postToReporter = async (data: unknown) => { try { const reporterHost = API_BASE_URL; const url = `${reporterHost}/report`; const xua = xUserAgent('__JOLIBOX_LOCAL_SDK_VERSION__'); const joliSource = JOLI_SOURCE; const headers: Record = { 'Content-Type': 'application/json' }; if (xua) headers['x-user-agent'] = xua; if (joliSource) headers['x-joli-source'] = joliSource; const response = await fetch(url, { method: 'POST', headers, body: JSON.stringify(data), signal: timeoutFn(5000) }); return response; } catch (error) { console.error('Error posting to reporter:', error); // Optionally rethrow or return a default response return null; } }; export async function trackError(errorData: Record) { try { const errEvent: IEvent = { name: 'globalJsError', type: EventType.ErrorTrace, location: getLocation(), target: null, extra: errorData, timestamp: Date.now(), userId: null }; const device = getDevice(); const events: IEvent[] = [errEvent]; const eventPackage: IEventPackage = { protocolVersion: '1.0.0', events, device, project: EProject.WebSDK }; const serializedEventPackage = serializeEventPackage(eventPackage); await postToReporter(serializedEventPackage); } catch (error) { console.error('Failed to track error:', error); // Since this is already an error tracking function, we might want to // have a fallback reporting mechanism here or just silently fail } }