Skip to content

Commit

Permalink
fix: better error message
Browse files Browse the repository at this point in the history
  • Loading branch information
JimmyLv committed Mar 3, 2023
1 parent e6f2af0 commit 3fcb02c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
5 changes: 3 additions & 2 deletions hooks/useSummarize.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useState } from "react";
import { toast } from "react-hot-toast";
import { RATE_LIMIT_COUNT } from "../utils/constants";

export function useSummarize() {
const [loading, setLoading] = useState(false);
Expand Down Expand Up @@ -30,9 +31,9 @@ export function useSummarize() {
if (!response.ok) {
console.log("error", response);
if (response.status === 501) {
toast.error("啊叻?视频字幕不见了?!");
toast.error("啊叻?视频字幕不见了?!\n(这个视频太短了...\n或者还没有字幕哦!)");
} else if (response.status === 504) {
toast.error("网站访问量大,每日限额使用 5 次哦!");
toast.error(`网站访问量大,每日限额使用 ${RATE_LIMIT_COUNT} 次哦!`);
} else {
toast.error(response.statusText);
}
Expand Down
25 changes: 15 additions & 10 deletions pages/api/summarize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const run = async (bvId: string) => {
const json = await response.json();
const subtitleList = json.data?.subtitle?.list;
if (!subtitleList || subtitleList?.length < 1) {
throw new Error(response.statusText);
throw new Error('no subtitle');
}

return json;
Expand All @@ -41,14 +41,19 @@ export default async function handler(
if (!bvId) {
return new Response("No bvid in the request", { status: 500 });
}
const res = await pRetry(() => run(bvId), {
onFailedAttempt: (error) => {
console.log(
`Attempt ${error.attemptNumber} failed. There are ${error.retriesLeft} retries left.`
);
},
retries: 3,
});
let res
try {
res = await pRetry(() => run(bvId), {
onFailedAttempt: (error) => {
console.log(
`Attempt ${error.attemptNumber} failed. There are ${error.retriesLeft} retries left.`
);
},
retries: 3,
});
} catch (e) {
return new Response("No subtitle in the video", { status: 501 });
}
// @ts-ignore
const title = res.data?.title;
const subtitleList = res.data?.subtitle?.list;
Expand Down Expand Up @@ -99,7 +104,7 @@ export default async function handler(

return NextResponse.json(result);
} catch (error: any) {
console.log(error);
console.log('API error', error, error.message);
return NextResponse.json({
errorMessage: error.message,
});
Expand Down

1 comment on commit 3fcb02c

@vercel
Copy link

@vercel vercel bot commented on 3fcb02c Mar 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.