Support to 404 error

This commit is contained in:
ryo-ma 2022-02-11 02:49:45 +09:00
parent 5f08ba0e98
commit 3544e23c13
3 changed files with 14 additions and 3 deletions

View file

@ -2,7 +2,7 @@ import { GithubAPIClient } from "./src/github_api_client.ts";
import { Card } from "./src/card.ts"; import { Card } from "./src/card.ts";
import { CONSTANTS, parseParams } from "./src/utils.ts"; import { CONSTANTS, parseParams } from "./src/utils.ts";
import { COLORS, Theme } from "./src/theme.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"; import "https://deno.land/x/dotenv@v0.5.0/load.ts";
const client = new GithubAPIClient(); const client = new GithubAPIClient();
@ -58,10 +58,13 @@ export default async (req: Request) => {
const token = Deno.env.get("GITHUB_TOKEN"); const token = Deno.env.get("GITHUB_TOKEN");
const userInfo = await client.requestUserInfo(token, username); const userInfo = await client.requestUserInfo(token, username);
if (userInfo === null) { if (userInfo === null) {
const error = new Error404(
"Can not find a user with username: " + username,
);
return new Response( return new Response(
"Can not find a user with userID: " + username, error.render(),
{ {
status: 404, status: error.status,
headers: new Headers({ "Content-Type": "text" }), headers: new Headers({ "Content-Type": "text" }),
}, },
); );

View file

@ -13,3 +13,8 @@ export class Error400 extends BaseError {
readonly status = 400; readonly status = 400;
readonly message = "Bad Reuquest"; readonly message = "Bad Reuquest";
} }
export class Error404 extends BaseError {
readonly status = 404;
readonly message = "Not Found";
}

View file

@ -21,6 +21,9 @@ export class GithubAPIClient {
this.requestUserPullRequest(token, username), this.requestUserPullRequest(token, username),
this.requestUserRepository(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]!); return new UserInfo(results[0]!, results[1]!, results[2]!, results[3]!);
} }
private async requestUserActivity( private async requestUserActivity(