From 560b535fbf2010470ee1396bad3062abf236fd53 Mon Sep 17 00:00:00 2001 From: Frost Ming Date: Wed, 13 Nov 2024 13:16:52 +0800 Subject: [PATCH] fix: add error report when json is invalid (#33) Fixes #32 Signed-off-by: Frost Ming --- src/answer.ts | 5 ++++- src/article.ts | 7 +++++-- src/index.ts | 42 +++++++++++++++++++++++++++--------------- src/lib.ts | 10 ++++++++++ src/question.ts | 5 ++++- 5 files changed, 50 insertions(+), 19 deletions(-) diff --git a/src/answer.ts b/src/answer.ts index 69b5caf..65dee9a 100644 --- a/src/answer.ts +++ b/src/answer.ts @@ -1,5 +1,5 @@ import { Question } from "./question"; -import { fixImagesAndLinks, createTemplate, extractReference } from "./lib"; +import { fixImagesAndLinks, createTemplate, extractReference, FetchError } from "./lib"; export type Answer = { content: string; @@ -98,6 +98,9 @@ const questionTemplate = createTemplate` export async function answer(id: string, redirect: boolean, env: Env): Promise { const url = `https://api.zhihu.com/v4/answers/${id}?include=content%2Cexcerpt%2Cauthor%2Cvoteup_count%2Ccomment_count%2Cquestion%2Ccreated_time%2Cquestion.detail`; const response = await fetch(url); + if (!response.ok) { + throw new FetchError(response.statusText, response); + } const data = await response.json(); const createdTime = new Date(data.created_time * 1000); diff --git a/src/article.ts b/src/article.ts index 164cd51..dfe4da8 100644 --- a/src/article.ts +++ b/src/article.ts @@ -1,4 +1,4 @@ -import { fixImagesAndLinks, createTemplate, extractReference } from "./lib"; +import { fixImagesAndLinks, createTemplate, extractReference, FetchError } from "./lib"; export type Article = { title: string; @@ -73,7 +73,7 @@ const template = createTemplate`
- +

${"title"}

@@ -103,6 +103,9 @@ const template = createTemplate` export async function article(id: string, redirect: boolean, env: Env): Promise { const url = new URL(id, `https://api.zhihu.com/article/`); const response = await fetch(url); + if (!response.ok) { + throw new FetchError(response.statusText, response); + } const data = await response.json
(); const createdTime = new Date(data.created * 1000); diff --git a/src/index.ts b/src/index.ts index 3d6b99a..01c7c48 100644 --- a/src/index.ts +++ b/src/index.ts @@ -44,31 +44,43 @@ Allow: /answer/* let match = path.match(/^(?:\/question\/\d+)?\/answer\/(\d+)\/?$/); if (match) { const answerId = match[1]; - return new Response(await answer(answerId, redirect, env), { - headers: { - 'Content-Type': 'text/html', - }, - }); + try { + return new Response(await answer(answerId, redirect, env), { + headers: { + 'Content-Type': 'text/html', + }, + }); + } catch (e: any) { + return e.response || new Response(e.message, { status: 500 }); + } } match = path.match(/^\/p\/(\d+)\/?$/); if (match) { const articleId = match[1]; - return new Response(await article(articleId, redirect, env), { - headers: { - 'Content-Type': 'text/html', - }, - }); + try { + return new Response(await article(articleId, redirect, env), { + headers: { + 'Content-Type': 'text/html', + }, + }); + } catch (e: any) { + return e.response || new Response(e.message, { status: 500 }); + } } match = path.match(/^\/question\/(\d+)\/?$/); if (match) { const questionId = match[1]; - return new Response(await question(questionId, redirect, env), { - headers: { - 'Content-Type': 'text/html', - }, - }); + try { + return new Response(await question(questionId, redirect, env), { + headers: { + 'Content-Type': 'text/html', + }, + }); + } catch (e: any) { + return e.response || new Response(e.message, { status: 500 }); + } } // Redirect to the same URL under zhihu.com diff --git a/src/lib.ts b/src/lib.ts index 6588abf..36d67f0 100644 --- a/src/lib.ts +++ b/src/lib.ts @@ -42,3 +42,13 @@ export function extractReference(html: string): string { } return ""; } + + +export class FetchError extends Error { + response?: Response; + + constructor(message: string, response?: Response) { + super(message); + this.response = response; + } +} diff --git a/src/question.ts b/src/question.ts index 68021c7..f868831 100644 --- a/src/question.ts +++ b/src/question.ts @@ -1,4 +1,4 @@ -import { createTemplate } from "./lib"; +import { createTemplate, FetchError } from "./lib"; export type Question = { type: 'question'; @@ -55,6 +55,9 @@ export async function question(id: string, redirect: boolean, env: Env): Promise 'user-agent': 'node' }, }); + if (!response.ok) { + throw new FetchError(response.statusText, response); + } const data = await response.json(); const createdTime = new Date(data.created * 1000);