diff --git a/src/app/music/page.tsx b/src/app/music/page.tsx index c390e38..f9688e2 100644 --- a/src/app/music/page.tsx +++ b/src/app/music/page.tsx @@ -15,6 +15,15 @@ const SPECTRUM_REFERENCE_VOLUME = 10; const SPECTRUM_MIN_VOLUME = 5; const SPECTRUM_MAX_REFERENCE_VOLUME = 15; +function getApiErrorMessage(error: unknown, fallback: string): string { + if (typeof error === 'string') return error; + if (error && typeof error === 'object' && 'message' in error) { + const message = (error as { message?: unknown }).message; + if (typeof message === 'string' && message.trim()) return message; + } + return fallback; +} + type MusicSource = 'wy' | 'tx' | 'kw' | 'kg' | 'mg'; type MusicQuality = '128k' | '320k' | 'flac' | 'flac24bit'; @@ -1164,7 +1173,7 @@ export default function MusicPage() { } else { const data = await response.json(); setToast({ - message: data.error || '删除失败', + message: getApiErrorMessage(data.error, '删除失败'), type: 'error', onClose: () => setToast(null), }); @@ -1217,7 +1226,7 @@ export default function MusicPage() { } else { const data = await response.json(); setToast({ - message: data.error || '移除失败', + message: getApiErrorMessage(data.error, '移除失败'), type: 'error', onClose: () => setToast(null), }); diff --git a/src/components/AddToPlaylistModal.tsx b/src/components/AddToPlaylistModal.tsx index a6effdd..c9cd5ad 100644 --- a/src/components/AddToPlaylistModal.tsx +++ b/src/components/AddToPlaylistModal.tsx @@ -31,6 +31,15 @@ interface AddToPlaylistModalProps { onError?: (message: string) => void; } +function getApiErrorMessage(error: unknown, fallback: string): string { + if (typeof error === 'string') return error; + if (error && typeof error === 'object' && 'message' in error) { + const message = (error as { message?: unknown }).message; + if (typeof message === 'string' && message.trim()) return message; + } + return fallback; +} + function MusicLoadingIndicator({ text, size = 'md', @@ -139,7 +148,7 @@ export default function AddToPlaylistModal({ await loadPlaylists(); } else { const data = await response.json(); - onError?.(data.error || '创建歌单失败'); + onError?.(getApiErrorMessage(data.error, '创建歌单失败')); } } catch (error) { console.error('创建歌单失败:', error); @@ -175,7 +184,7 @@ export default function AddToPlaylistModal({ onClose(); } else { const data = await response.json(); - onError?.(data.error || '添加失败'); + onError?.(getApiErrorMessage(data.error, '添加失败')); } } catch (error) { console.error('添加到歌单失败:', error);