Skip to content

Commit

Permalink
feat: 优雅的报错提示
Browse files Browse the repository at this point in the history
  • Loading branch information
14790897 committed Feb 24, 2024
1 parent f5bdd4f commit 93f8889
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 9 deletions.
15 changes: 11 additions & 4 deletions components/QuillEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,8 @@ import { debounce } from "lodash";
//i18n
import { useTranslation } from "@/app/i18n/client";
//notification
import { ToastContainer } from "react-toastify";
import { ToastContainer, toast } from "react-toastify";
import "react-toastify/dist/ReactToastify.css";
import NotifyButton from "@/components/Notification"; // 确保路径正确

const toolbarOptions = [
["bold", "italic", "underline", "strike"], // 加粗、斜体、下划线和删除线
Expand Down Expand Up @@ -431,6 +430,10 @@ const QEditor = ({ lng }) => {
quill!,
800
)},搜索到的论文内容:${trimmedMessage},需要完成的论文主题:${topic},请根据搜索到的论文内容完成用户的论文`;
// toast.info(`搜索论文完成,内容:${content}`, {
// position: "top-right",
// autoClose: 5000,
// });
await sendMessageToOpenAI(
content,
quill!,
Expand Down Expand Up @@ -465,9 +468,13 @@ const QEditor = ({ lng }) => {
offset += 2;
setGenerateNumber(i + 1);
} catch (error) {
// console.error("Error fetching data:", error);
console.error("Paper2AI出现错误", error);
// 在处理错误后,再次抛出这个错误
throw new Error(`Paper2AI出现错误: ${error}`);
// throw new Error(`Paper2AI出现错误: ${error}`);
toast.error(`Paper2AI出现错误: ${error}`, {
position: "top-center",
autoClose: 5000,
});
}
}
setOpenProgressBar(false);
Expand Down
15 changes: 11 additions & 4 deletions components/chatAI.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,16 @@ const sendMessageToOpenAI = async (
(upsreamUrl || process.env.NEXT_PUBLIC_AI_URL) + "/v1/chat/completions",
requestOptions
);
if (!response.ok || !response.body) {
throw new Error("");
// 检查响应状态码是否为429
if (response.status === 429) {
// 可以在这里处理429错误,例如通过UI通知用户
throw new Error("请求过于频繁,请稍后再试。");
} else if (!response.ok) {
// 处理其他类型的HTTP错误
throw new Error(`HTTP错误,状态码:${response.status}`);
}
if (useEditorFlag && editor && cursorPosition !== null) {
const reader = response.body.getReader();
const reader = response.body!.getReader();
const decoder = new TextDecoder();
//开始前先进行换行
// editor.focus();
Expand All @@ -106,7 +111,9 @@ const sendMessageToOpenAI = async (
// 如果有响应,返回响应的原始内容
if (response) {
const rawResponse = await response.text();
throw new Error(`请求发生错误: ${error}, Response: ${rawResponse}`);
throw new Error(
`请求发生错误: ${error.message}, Response: ${rawResponse}`
);
}
// 如果没有响应,只抛出错误
throw error;
Expand Down
20 changes: 19 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"@sentry/nextjs": "^7.101.1",
"@supabase/ssr": "latest",
"@supabase/supabase-js": "latest",
"@types/react-toastify": "^4.1.0",
"add": "^2.0.6",
"autoprefixer": "10.4.15",
"axios": "^1.6.5",
Expand Down

0 comments on commit 93f8889

Please sign in to comment.