From 3544e23c1384d62a773fb31b4ad8e46374f32414 Mon Sep 17 00:00:00 2001 From: ryo-ma Date: Fri, 11 Feb 2022 02:49:45 +0900 Subject: [PATCH] Support to 404 error --- index.ts | 9 ++++++--- src/error_page.ts | 5 +++++ src/github_api_client.ts | 3 +++ 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/index.ts b/index.ts index f42c2a9..a1ab91a 100644 --- a/index.ts +++ b/index.ts @@ -2,7 +2,7 @@ import { GithubAPIClient } from "./src/github_api_client.ts"; import { Card } from "./src/card.ts"; import { CONSTANTS, parseParams } from "./src/utils.ts"; import { COLORS, Theme } from "./src/theme.ts"; -import { Error400 } from "./src/error_page.ts"; +import { Error400, Error404 } from "./src/error_page.ts"; import "https://deno.land/x/dotenv@v0.5.0/load.ts"; const client = new GithubAPIClient(); @@ -58,10 +58,13 @@ export default async (req: Request) => { const token = Deno.env.get("GITHUB_TOKEN"); const userInfo = await client.requestUserInfo(token, username); if (userInfo === null) { + const error = new Error404( + "Can not find a user with username: " + username, + ); return new Response( - "Can not find a user with userID: " + username, + error.render(), { - status: 404, + status: error.status, headers: new Headers({ "Content-Type": "text" }), }, ); diff --git a/src/error_page.ts b/src/error_page.ts index 608b594..f2e0ce4 100644 --- a/src/error_page.ts +++ b/src/error_page.ts @@ -13,3 +13,8 @@ export class Error400 extends BaseError { readonly status = 400; readonly message = "Bad Reuquest"; } + +export class Error404 extends BaseError { + readonly status = 404; + readonly message = "Not Found"; +} diff --git a/src/github_api_client.ts b/src/github_api_client.ts index 5333d8c..4ca5e1f 100644 --- a/src/github_api_client.ts +++ b/src/github_api_client.ts @@ -21,6 +21,9 @@ export class GithubAPIClient { this.requestUserPullRequest(token, username), this.requestUserRepository(token, username), ]); + if (results.some((r) => r == null)) { + return null; + } return new UserInfo(results[0]!, results[1]!, results[2]!, results[3]!); } private async requestUserActivity(