/
kreyndelHam
/
Konstructorium
Обзор
Документация
Войти
/
kreyndelHam
/
Konstructorium
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
1
CI/CD
Аналитика
Безопасность
dev
frontend/src/utils/showUserError.js
98 строк
3 KB
MihahamYT
Errors ids added
17 май 2026, 10:53
17 май 2026, 10:53
484ef2f
Код
Авторство
О чём код?
import { toast } from 'react-toastify' import { defaultLanguage, t as translate } from '../services/translations' import { trackUserError } from '../services/webAnalyticsTracker' import { getActiveUserId } from './getActiveUserId' import { formatUserErrorToast, normalizeUserError } from './userError' /** * Resolve active UI language for error strings outside React tree. * * @returns {string} */ const getErrorLanguage = () => { if (typeof window === 'undefined') { return defaultLanguage } return localStorage.getItem('language') || defaultLanguage } /** @type {(key: string, params?: Record<string, string>) => string} */ const translateFn = (key, params = {}) => translate(key, getErrorLanguage(), params) /** * Show user-facing error toast and report incident to analytics (always). * * @param {unknown} error * @param {{skipToast?: boolean, source?: string}} [options] * @returns {{incidentId: string, errorCode: string, userMessage: string}|null} */ export const showUserError = (error, options = {}) => { if (error && typeof error === 'object' && error.__userErrorHandled) { return error.__userErrorResult || null } const alreadyReported = error && typeof error === 'object' && error.__userErrorReported const info = normalizeUserError(error, translateFn) if (options.source) { info.source = options.source } if (!alreadyReported) { trackUserError({ incidentId: info.incidentId, errorCode: info.errorCode, userMessage: info.userMessage, path: info.path, source: info.source, statusCode: info.statusCode, apiService: info.apiService, userId: getActiveUserId(), }) if (error && typeof error === 'object') { error.__userErrorReported = true } } if (!options.skipToast) { toast.error(formatUserErrorToast(info, translateFn), { autoClose: 8000, style: { whiteSpace: 'pre-line' }, }) } if (error && typeof error === 'object') { error.__userErrorHandled = true error.__userErrorResult = info } return info } /** * Report user error to analytics without showing a toast. * * @param {unknown} error * @param {{source?: string}} [options] * @returns {{incidentId: string, errorCode: string, userMessage: string}|null} */ export const reportUserError = (error, options = {}) => { return showUserError(error, { ...options, skipToast: true }) } /** * Build display props for inline error UI (e.g. react-query isError). * * @param {unknown} error * @returns {{message: string, incidentId: string, errorCode: string}|null} */ export const getUserErrorDisplay = (error) => { if (!error) { return null } const info = normalizeUserError(error, translateFn) return { message: info.userMessage, incidentId: info.incidentId, errorCode: info.errorCode, } }