Skip to content

Commit

Permalink
fix: add error report when json is invalid (#33)
Browse files Browse the repository at this point in the history
Fixes #32

Signed-off-by: Frost Ming <[email protected]>
  • Loading branch information
frostming authored Nov 13, 2024
1 parent dc8e019 commit 560b535
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 19 deletions.
5 changes: 4 additions & 1 deletion src/answer.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -98,6 +98,9 @@ const questionTemplate = createTemplate`
export async function answer(id: string, redirect: boolean, env: Env): Promise<string> {
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<Answer>();
const createdTime = new Date(data.created_time * 1000);

Expand Down
7 changes: 5 additions & 2 deletions src/article.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { fixImagesAndLinks, createTemplate, extractReference } from "./lib";
import { fixImagesAndLinks, createTemplate, extractReference, FetchError } from "./lib";

export type Article = {
title: string;
Expand Down Expand Up @@ -73,7 +73,7 @@ const template = createTemplate`
</head>
<body style="max-width: 1000px; margin: 0 auto; padding: 0 1em 0 1em;" class="yue">
<header>
<img class="origin_image" src="${"image_url"}"/>
<img class="origin_image" src="${"image_url"}"/>
<h1><a href="${"url"}">${"title"}</a></h1>
<div class="author">
<img class="avatar" id="avatar" src="${"avatar_url"}" />
Expand Down Expand Up @@ -103,6 +103,9 @@ const template = createTemplate`
export async function article(id: string, redirect: boolean, env: Env): Promise<string> {
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<Article>();
const createdTime = new Date(data.created * 1000);

Expand Down
42 changes: 27 additions & 15 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
5 changes: 4 additions & 1 deletion src/question.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createTemplate } from "./lib";
import { createTemplate, FetchError } from "./lib";

export type Question = {
type: 'question';
Expand Down Expand Up @@ -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<Question>();
const createdTime = new Date(data.created * 1000);
Expand Down

0 comments on commit 560b535

Please sign in to comment.