diff --git a/hooks/useSummarize.ts b/hooks/useSummarize.ts index 9c99457f..d0a59149 100644 --- a/hooks/useSummarize.ts +++ b/hooks/useSummarize.ts @@ -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); @@ -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); } diff --git a/pages/api/summarize.ts b/pages/api/summarize.ts index a1041b3a..8273172b 100644 --- a/pages/api/summarize.ts +++ b/pages/api/summarize.ts @@ -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; @@ -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; @@ -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, });