统一前端错误读取和调试日志
This commit is contained in:
@@ -2,6 +2,7 @@ import axios, { type AxiosResponse, type InternalAxiosRequestConfig, type AxiosE
|
||||
import { ElMessage } from 'element-plus'
|
||||
import { showToast } from 'vant'
|
||||
import { apiMonitor } from './monitor'
|
||||
import { debugError, debugLog } from '@/shared/utils/debug'
|
||||
|
||||
function showError(message: string) {
|
||||
const isMobile = window.location.pathname.startsWith('/m')
|
||||
@@ -24,7 +25,7 @@ declare module 'axios' {
|
||||
// 开发环境日志记录
|
||||
function logRequest(config: InternalAxiosRequestConfig) {
|
||||
if (import.meta.env.DEV) {
|
||||
console.log(`[API Request] ${config.method?.toUpperCase()} ${config.url}`, {
|
||||
debugLog(`[API Request] ${config.method?.toUpperCase()} ${config.url}`, {
|
||||
params: config.params,
|
||||
data: config.data,
|
||||
headers: config.headers,
|
||||
@@ -38,7 +39,7 @@ function logRequest(config: InternalAxiosRequestConfig) {
|
||||
function logResponse(response: AxiosResponse) {
|
||||
if (import.meta.env.DEV) {
|
||||
const duration = response.config._startTime ? Date.now() - response.config._startTime : 0
|
||||
console.log(
|
||||
debugLog(
|
||||
`[API Response] ${response.config.method?.toUpperCase()} ${response.config.url} (${duration}ms)`,
|
||||
{
|
||||
status: response.status,
|
||||
@@ -63,7 +64,7 @@ function logResponse(response: AxiosResponse) {
|
||||
function logError(error: AxiosError) {
|
||||
if (import.meta.env.DEV) {
|
||||
const duration = error.config?._startTime ? Date.now() - error.config._startTime : 0
|
||||
console.error(
|
||||
debugError(
|
||||
`[API Error] ${error.config?.method?.toUpperCase()} ${error.config?.url} (${duration}ms)`,
|
||||
{
|
||||
status: error.response?.status,
|
||||
|
||||
@@ -2,6 +2,13 @@
|
||||
* API 性能监控
|
||||
* 记录和分析 API 请求性能
|
||||
*/
|
||||
import {
|
||||
debugGroup,
|
||||
debugGroupEnd,
|
||||
debugLog,
|
||||
debugTable,
|
||||
debugWarn,
|
||||
} from '@/shared/utils/debug'
|
||||
|
||||
interface RequestMetrics {
|
||||
url: string
|
||||
@@ -29,7 +36,7 @@ class ApiMonitor {
|
||||
|
||||
// 开发环境下,慢请求警告
|
||||
if (import.meta.env.DEV && metric.duration > 1000) {
|
||||
console.warn(`[API 慢请求] ${metric.method} ${metric.url} 耗时 ${metric.duration}ms`)
|
||||
debugWarn(`[API 慢请求] ${metric.method} ${metric.url} 耗时 ${metric.duration}ms`)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -105,12 +112,12 @@ class ApiMonitor {
|
||||
* 打印性能报告
|
||||
*/
|
||||
printReport() {
|
||||
console.group('📊 API 性能报告')
|
||||
console.log('总请求数:', this.metrics.length)
|
||||
console.log('平均响应时间:', this.getAverageResponseTime(), 'ms')
|
||||
console.log('成功率:', this.getSuccessRate(), '%')
|
||||
console.log('最慢的 5 个请求:')
|
||||
console.table(
|
||||
debugGroup('API 性能报告')
|
||||
debugLog('总请求数:', this.metrics.length)
|
||||
debugLog('平均响应时间:', this.getAverageResponseTime(), 'ms')
|
||||
debugLog('成功率:', this.getSuccessRate(), '%')
|
||||
debugLog('最慢的 5 个请求:')
|
||||
debugTable(
|
||||
this.getSlowestRequests(5).map(m => ({
|
||||
方法: m.method,
|
||||
URL: m.url,
|
||||
@@ -118,9 +125,9 @@ class ApiMonitor {
|
||||
状态: m.status,
|
||||
}))
|
||||
)
|
||||
console.log('按 URL 统计:')
|
||||
console.table(this.getStatsByUrl())
|
||||
console.groupEnd()
|
||||
debugLog('按 URL 统计:')
|
||||
debugTable(this.getStatsByUrl())
|
||||
debugGroupEnd()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -130,5 +137,5 @@ export const apiMonitor = new ApiMonitor()
|
||||
// 开发环境下暴露到 window 对象,方便调试
|
||||
if (import.meta.env.DEV) {
|
||||
;(window as any).__apiMonitor = apiMonitor
|
||||
console.log('💡 使用 __apiMonitor.printReport() 查看 API 性能报告')
|
||||
debugLog('使用 __apiMonitor.printReport() 查看 API 性能报告')
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { readError } from '@/shared/utils/error'
|
||||
import { onUnmounted, ref } from 'vue'
|
||||
import { showToast } from 'vant'
|
||||
import { sendSmsCode } from '@/features/auth/api/auth'
|
||||
@@ -51,13 +52,6 @@ export function useSmsCountdown() {
|
||||
}
|
||||
}
|
||||
|
||||
function readError(error: unknown, fallback: string) {
|
||||
if (typeof error === 'object' && error && 'response' in error) {
|
||||
const response = (error as { response?: { data?: { message?: string } } }).response
|
||||
return response?.data?.message || fallback
|
||||
}
|
||||
return fallback
|
||||
}
|
||||
|
||||
return {
|
||||
countDown,
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
type DebugConsole = Pick<
|
||||
Console,
|
||||
'log' | 'warn' | 'error' | 'group' | 'groupEnd' | 'table'
|
||||
>
|
||||
|
||||
function runInDev(method: keyof DebugConsole, args: unknown[]) {
|
||||
if (!import.meta.env.DEV) return
|
||||
console[method](...args)
|
||||
}
|
||||
|
||||
export function debugLog(...args: unknown[]) {
|
||||
runInDev('log', args)
|
||||
}
|
||||
|
||||
export function debugWarn(...args: unknown[]) {
|
||||
runInDev('warn', args)
|
||||
}
|
||||
|
||||
export function debugError(...args: unknown[]) {
|
||||
runInDev('error', args)
|
||||
}
|
||||
|
||||
export function debugGroup(...args: unknown[]) {
|
||||
runInDev('group', args)
|
||||
}
|
||||
|
||||
export function debugGroupEnd() {
|
||||
runInDev('groupEnd', [])
|
||||
}
|
||||
|
||||
export function debugTable(data?: unknown, columns?: string[]) {
|
||||
if (!import.meta.env.DEV) return
|
||||
console.table(data, columns)
|
||||
}
|
||||
@@ -3,8 +3,17 @@
|
||||
*/
|
||||
export function readError(error: unknown, fallback: string): string {
|
||||
if (typeof error === 'object' && error && 'response' in error) {
|
||||
const response = (error as { response?: { data?: { message?: string } } }).response
|
||||
return response?.data?.message || fallback
|
||||
const response = (
|
||||
error as {
|
||||
response?: {
|
||||
data?: {
|
||||
message?: string
|
||||
error?: string
|
||||
}
|
||||
}
|
||||
}
|
||||
).response
|
||||
return response?.data?.message || response?.data?.error || fallback
|
||||
}
|
||||
if (error instanceof Error) {
|
||||
return error.message || fallback
|
||||
|
||||
Reference in New Issue
Block a user