/
githubmirror
/
components
Обзор
Документация
Войти
/
githubmirror
/
components
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
docs/src/app/shared/analytics/format-error.ts
50 строк
2 KB
Kristiyan Kostadinov
build: reuse tslint rules for docs site and fix failures
29 апр 2025, 15:02
29 апр 2025, 15:02
da9627d
Код
Авторство
О чём код?
/** * @license * Copyright Google LLC All Rights Reserved. * * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://angular.dev/license */ /** * Formats an `ErrorEvent` to a human-readable string that can * be sent to Google Analytics. */ export function formatErrorEventForAnalytics(event: ErrorEvent): string { const {message, filename, colno, lineno, error} = event; if (error instanceof Error) { return formatErrorForAnalytics(error); } return `${stripErrorMessagePrefix(message)}\n${filename}:${lineno || '?'}:${colno || '?'}`; } /** * Formats an `Error` to a human-readable string that can be sent * to Google Analytics. */ export function formatErrorForAnalytics(error: Error): string { let stack = '<no-stack>'; if (error.stack) { stack = stripErrorMessagePrefix(error.stack) // strip the message from the stack trace, if present .replace(error.message + '\n', '') // strip leading spaces .replace(/^ +/gm, '') // strip all leading "at " for each frame .replace(/^at /gm, '') // replace long urls with just the last segment: `filename:line:column` .replace(/(?: \(|@)http.+\/([^/)]+)\)?(?:\n|$)/gm, '@$1\n') // replace "eval code" in Edge .replace(/ *\(eval code(:\d+:\d+)\)(?:\n|$)/gm, '@???$1\n'); } return `${error.message}\n${stack}`; } /** Strips the error message prefix from a message or stack trace. */ function stripErrorMessagePrefix(input: string): string { return input.replace(/^(Uncaught )?Error: /, ''); }