flat: ts + 接口错误处理
This commit is contained in:
@@ -98,7 +98,7 @@ export function setupLiveHandlers() {
|
||||
contextIsolation: false,
|
||||
},
|
||||
});
|
||||
liveWindow.webContents.openDevTools();
|
||||
// liveWindow.webContents.openDevTools();
|
||||
liveWindow.on("closed", () => {
|
||||
liveWindow = null;
|
||||
});
|
||||
@@ -134,6 +134,33 @@ async function getSessionId(requestBody: object) {
|
||||
body: JSON.stringify(requestBody),
|
||||
});
|
||||
|
||||
// 首先检查响应内容类型
|
||||
const contentType = response.headers.get("content-type");
|
||||
if (!contentType || !contentType.includes("application/json")) {
|
||||
// 如果不是JSON,读取原始文本进行调试
|
||||
const rawText = await response.text();
|
||||
console.warn("服务器返回非JSON响应:", rawText);
|
||||
|
||||
// 尝试解析可能的JSON响应(即使Content-Type不正确)
|
||||
try {
|
||||
const data = JSON.parse(rawText);
|
||||
if (response.ok && data.sessionid) {
|
||||
return { success: true, sessionId: data.sessionid };
|
||||
} else {
|
||||
return {
|
||||
success: false,
|
||||
error: data.message || "服务器返回非JSON格式",
|
||||
};
|
||||
}
|
||||
} catch (parseError) {
|
||||
return {
|
||||
success: false,
|
||||
error: `服务器响应格式错误: ${rawText.substring(0, 100)}...`,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// 如果是JSON,正常解析
|
||||
const data = await response.json();
|
||||
|
||||
if (response.ok && data.sessionid) {
|
||||
|
||||
Reference in New Issue
Block a user