Conversation
Summary of ChangesHello @lrst6963, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! 此拉取请求旨在全面改进 Last.fm 认证流程的健壮性、错误处理和用户体验。通过引入更精细的错误消息、重构 API 请求以更好地处理服务器端错误,并优化认证逻辑,用户在连接 Last.fm 时将获得更清晰的反馈和更顺畅的体验,从而减少因认证问题而导致的困惑。 Highlights
Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
| lastfmClient.interceptors.response.use( | ||
| (response) => response, | ||
| (error: AxiosError<LastfmErrorResponse>) => { | ||
| const response = error.response; | ||
| if (!response) { | ||
| window.$message.error("Last.fm 请求失败,请检查网络连接"); | ||
| return Promise.reject(error); | ||
| } | ||
|
|
||
| const { status, data } = response; | ||
|
|
||
| switch (status) { | ||
| case 403: { | ||
| const code = data?.error; | ||
| if (code === 9 || code === 4 || code === 26) { | ||
| window.$message.error("Last.fm 认证失败,需要重新授权,已断开与 Last.fm 的连接!"); | ||
| disconnect(); | ||
| } else { | ||
| window.$message.error("Last.fm 认证失败,可能需要重新授权"); | ||
| } | ||
| break; | ||
| const code = data?.error; | ||
| if (typeof code === "number") { | ||
| if (code === 14 || code === 15) { | ||
| return Promise.reject(error); | ||
| } | ||
| case 401: | ||
| window.$message.error("Last.fm 未授权,已断开与 Last.fm 的连接!"); | ||
| if (shouldDisconnect(code)) { | ||
| window.$message.error("Last.fm 认证失败,需要重新授权,已断开与 Last.fm 的连接!"); | ||
| disconnect(); | ||
| break; | ||
| case 429: | ||
| window.$message.error("Last.fm 请求过于频繁,请稍后再试"); | ||
| break; | ||
| case 500: | ||
| case 502: | ||
| case 503: | ||
| window.$message.error("Last.fm 服务暂时不可用,请稍后再试"); | ||
| break; | ||
| default: | ||
| window.$message.error("Last.fm 请求失败"); | ||
| break; | ||
| } else { | ||
| window.$message.error(getLastfmErrorMessage(code, data?.message || "Last.fm 请求失败")); | ||
| } | ||
| } else if (status === 401) { | ||
| window.$message.error("Last.fm 未授权,已断开与 Last.fm 的连接!"); | ||
| disconnect(); | ||
| } | ||
| return Promise.reject(error); | ||
| }, |
There was a problem hiding this comment.
新的错误拦截器在处理 Last.fm 特定 API 错误方面做得很好。然而,它似乎移除了旧代码中对通用网络错误和标准 HTTP 错误状态(如 429、5xx)的处理。这可能导致在这些情况下出现静默失败,用户不会收到任何反馈。
例如:
- 如果发生网络错误(
error.response为假值),不会显示任何消息,而旧代码会显示“请检查网络连接”。 - 如果服务器返回 500 错误并附带 HTML 页面,
data.error将是undefined,状态也不是 401,因此不会显示任何消息。旧代码对此有后备处理。
建议重新引入对这些情况的处理,以确保在所有错误条件下都能提供稳健的用户体验。
lastfmClient.interceptors.response.use(
(response) => response,
(error: AxiosError<LastfmErrorResponse>) => {
const response = error.response;
if (!response) {
window.$message.error("Last.fm 请求失败,请检查网络连接");
return Promise.reject(error);
}
const { status, data } = response;
const code = data?.error;
if (typeof code === "number") {
if (code === 14 || code === 15) {
return Promise.reject(error);
}
if (shouldDisconnect(code)) {
window.$message.error("Last.fm 认证失败,需要重新授权,已断开与 Last.fm 的连接!");
disconnect();
} else {
window.$message.error(getLastfmErrorMessage(code, data?.message || "Last.fm 请求失败"));
}
} else {
switch (status) {
case 401:
window.$message.error("Last.fm 未授权,已断开与 Last.fm 的连接!");
disconnect();
break;
case 429:
window.$message.error("Last.fm 请求过于频繁,请稍后再试");
break;
case 500:
case 502:
case 503:
window.$message.error("Last.fm 服务暂时不可用,请稍后再试");
break;
default:
if (status >= 400) {
window.$message.error(`Last.fm 请求失败 (${status})`);
}
break;
}
}
return Promise.reject(error);
},
);There was a problem hiding this comment.
Pull request overview
该 PR 旨在改进 Last.fm 的认证流程与错误处理,使用户在授权、失败与超时等场景下获得更清晰的反馈,并增强 API 层对错误码的识别与提示能力。
Changes:
- 在设置页的 Last.fm 连接流程中细化错误码处理与提示,并延长授权超时等待时间。
- 在 Last.fm API 封装中新增错误码到中文提示的映射,并为
getAuthToken/getSession增加明确的返回类型。 - 在 API 请求层对“2xx 但响应体包含 error”的场景进行显式抛错处理。
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
src/components/Setting/config/network.ts |
调整 Last.fm 授权轮询与错误提示逻辑、超时策略与兜底提示 |
src/api/lastfm.ts |
增加错误码提示映射、泛型返回类型、以及对响应体 error 的统一抛错处理 |
Comments suppressed due to low confidence (1)
src/components/Setting/config/network.ts:171
- window.open 可能因浏览器/系统弹窗拦截而返回 null。当前在 authWindow 为 null 的情况下仍会启动轮询并最终超时,但用户看不到授权页面也没有明确提示。建议在 open 结果为空时立即终止流程、恢复 loading,并提示用户允许弹窗或改用外部浏览器打开。
if (typeof window !== "undefined") {
const authWindow = window.open(authUrl, "_blank", "width=800,height=600");
const checkAuth = setInterval(async () => {
if (authWindow?.closed) {
| const code = data?.error; | ||
| if (typeof code === "number") { | ||
| if (code === 14 || code === 15) { | ||
| return Promise.reject(error); | ||
| } | ||
| case 401: | ||
| window.$message.error("Last.fm 未授权,已断开与 Last.fm 的连接!"); | ||
| if (shouldDisconnect(code)) { |
There was a problem hiding this comment.
这里仅在 data.error 为 number 时才处理错误码;但调用方(如 Setting 里的 connectLastfm)已在用 Number(...) 做兼容,说明 error 字段可能是 string。若这里严格判断 number,会导致部分错误既不提示也不触发 disconnect。建议对 string/number 都解析为 number(并校验非 NaN)后再走分支。
| const data = response.data as LastfmErrorResponse | T; | ||
| if (data && typeof data === "object" && "error" in data) { | ||
| const apiError = new Error( | ||
| typeof data.message === "string" && data.message ? data.message : "Last.fm 请求失败", | ||
| ) as AxiosError<LastfmErrorResponse>; | ||
| apiError.response = { | ||
| status: response.status, | ||
| data: data as LastfmErrorResponse, | ||
| statusText: response.statusText, | ||
| headers: response.headers, | ||
| config: response.config, | ||
| }; | ||
| throw apiError; | ||
| } | ||
| return data as T; |
There was a problem hiding this comment.
lastfmRequest 在 HTTP 成功响应里检测到 { error, message } 后会手动 throw,但这条错误不会进入 axios 的 response error interceptor,因此 shouldDisconnect/disconnect 逻辑不会执行。像 session key 失效(9)这类需要自动断开连接的情况,可能会导致状态不一致(仍然保留旧 sessionKey)。建议在这里检测到 error 时复用 shouldDisconnect 并主动 disconnect(以及按需提示)。
| const data = response.data as LastfmErrorResponse | T; | ||
| if (data && typeof data === "object" && "error" in data) { | ||
| const apiError = new Error( | ||
| typeof data.message === "string" && data.message ? data.message : "Last.fm 请求失败", | ||
| ) as AxiosError<LastfmErrorResponse>; | ||
| apiError.response = { | ||
| status: response.status, | ||
| data: data as LastfmErrorResponse, | ||
| statusText: response.statusText, | ||
| headers: response.headers, | ||
| config: response.config, | ||
| }; | ||
| throw apiError; | ||
| } | ||
| return data as T; |
There was a problem hiding this comment.
lastfmPostRequest 同样在 2xx 响应体里发现 { error, message } 时手动 throw,这会绕过 axios 的 response error interceptor,从而跳过 shouldDisconnect/disconnect 逻辑(例如 code=9 时不会自动清理 sessionKey)。建议与 lastfmRequest 一样在此处复用 shouldDisconnect 并执行 disconnect,保证行为一致。
| if (errorData && errorData.error) { | ||
| const code = Number(errorData.error); | ||
| if (code !== 14 && code !== 15) { | ||
| lastfmAuthLoading.value = false; | ||
| const errorMessage = errorData.message || "未知错误"; | ||
| window.$message.error(getLastfmErrorMessage(code, `认证失败: ${errorMessage}`)); | ||
| return; | ||
| } | ||
| } |
There was a problem hiding this comment.
这里把 Last.fm 错误码 15(token 过期/无效)当作“继续流程/继续轮询”的情况处理:初次预检时 code===15 仍会继续打开授权页,轮询时 code===15 也会一直等待到超时。这样会导致用户永远无法完成授权。建议对 15 单独处理:停止轮询/关闭窗口并重新获取 token(或提示用户重试)。
| const code = Number(errorData.error); | ||
| if (code === 14) { | ||
| window.$message.info("等待在 Last.fm 授权页面完成授权"); | ||
| return; |
There was a problem hiding this comment.
轮询中遇到错误码 14 时每 2 秒都会调用一次 $message.info,容易刷屏并影响使用体验。建议加一个本地 flag/节流逻辑,仅在首次检测到 14 时提示一次(后续静默等待即可)。
No description provided.