Skip to content

Commit

Permalink
feat: Define error-handling when it is failed on requrest to GitHub.
Browse files Browse the repository at this point in the history
  • Loading branch information
attakei committed Nov 13, 2024
1 parent ecf28af commit ecc0236
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import { sentry } from '@hono/sentry';
import { Octokit } from '@octokit/rest';
import markdownToHtml from 'zenn-markdown-html';

type Props = {
body?: string;
};

const app = new Hono<{ Bindings: CloudflareBindings }>();

app.use('*', (c: Context, next: any) => {
Expand All @@ -14,6 +18,7 @@ app.use('*', (c: Context, next: any) => {
});

app.get('/:slug', async (c) => {
let props: Props = {};
const octokit = new Octokit({
auth: c.env.REPO_PAT,
});
Expand All @@ -23,9 +28,18 @@ app.get('/:slug', async (c) => {
path: `articles/${c.req.param('slug')}.md`,
ref: c.req.query('ref'),
};
const resp = await octokit.rest.repos.getContent(params);
const md = Buffer.from(resp.data.content, 'base64').toString();
const html = `
try {
const resp = await octokit.rest.repos.getContent(params);
const md = Buffer.from(resp.data.content, 'base64').toString();
props.body = markdownToHtml(md);
} catch (error) {
if (error.status) {
console.error(error);
c.status(404);
return c.text('Content is not found.');
}
}
return c.html(`
<!DOCTYPE html>
<html lang="ja">
<head>
Expand All @@ -35,12 +49,11 @@ app.get('/:slug', async (c) => {
</head>
<body>
<div class="znc">
${markdownToHtml(md)}
${props.body}
</div>
</body>
</html>
`;
return c.html(html);
`);
});

export default app;

0 comments on commit ecc0236

Please sign in to comment.