From ac3f80b5cb6f795360d163b9d801b4e5846d8171 Mon Sep 17 00:00:00 2001 From: Spenser Black Date: Thu, 10 Feb 2022 10:33:03 -0500 Subject: [PATCH 1/4] Provide descriptive error when `username` missing Adds more text to the error to hopefully inform users that they failed to provide a proper URL, and not that the site is broken. Error changed to generic 400 since the problem is that the user didn't make a good request, not that the page itself is missing. --- index.ts | 9 +++++++-- src/error_page.ts | 11 +++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 src/error_page.ts diff --git a/index.ts b/index.ts index c4b1d16..20a990d 100644 --- a/index.ts +++ b/index.ts @@ -3,6 +3,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 "https://deno.land/x/dotenv@v0.5.0/load.ts"; const client = new GithubAPIClient(); @@ -41,10 +42,14 @@ export default async (req: ServerRequest) => { ).map((r) => r.trim()); if (username === null) { + const [base] = req.url.split("?"); + const error = new Error400(`

username is a required query parameter

+

The URL should look like ${base}?username=USERNAME, where +USERNAME is your GitHub username.`); req.respond( { - body: "Can not find a query parameter: username", - status: 404, + body: error.render(), + status: error.status, headers: new Headers({ "Content-Type": "text" }), }, ); diff --git a/src/error_page.ts b/src/error_page.ts new file mode 100644 index 0000000..862f6a7 --- /dev/null +++ b/src/error_page.ts @@ -0,0 +1,11 @@ +abstract class BaseError { + readonly status!: number; + constructor(readonly content?: string) {} + render(): string { + return `

${this.status}

${this.content ?? ''}`; + } +} + +export class Error400 extends BaseError { + readonly status = 400; +} From 1ea1949d86802d931f4741681b7362c60f1c1ff5 Mon Sep 17 00:00:00 2001 From: ryo-ma Date: Fri, 11 Feb 2022 01:51:10 +0900 Subject: [PATCH 2/4] Update vercel-deno version to 1.1.1 --- debug.ts | 7 ++----- deps.ts | 1 - index.ts | 45 ++++++++++++++++++++++----------------------- src/utils.ts | 6 ++---- vercel.json | 2 +- 5 files changed, 27 insertions(+), 34 deletions(-) diff --git a/debug.ts b/debug.ts index cf1ef32..ab64cc8 100644 --- a/debug.ts +++ b/debug.ts @@ -1,7 +1,4 @@ -import { serve } from "https://deno.land/std@0.66.0/http/server.ts"; +import { serve } from "https://deno.land/std@0.125.0/http/server.ts"; import requestHandler from "./index.ts"; -const s = serve({ port: 8080 }); -for await (const req of s) { - requestHandler(req); -} +serve(requestHandler, { port: 8080 }); diff --git a/deps.ts b/deps.ts index aa99f08..5d6175a 100644 --- a/deps.ts +++ b/deps.ts @@ -1,2 +1 @@ -export { ServerRequest } from "https://deno.land/std@0.66.0/http/server.ts"; export { soxa } from "https://deno.land/x/soxa@1.4/mod.ts"; diff --git a/index.ts b/index.ts index 20a990d..6f2bbdb 100644 --- a/index.ts +++ b/index.ts @@ -1,14 +1,13 @@ -import { ServerRequest } from "./deps.ts"; 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 } from "./src/error_page.ts"; import "https://deno.land/x/dotenv@v0.5.0/load.ts"; const client = new GithubAPIClient(); -export default async (req: ServerRequest) => { +export default async (req: Request) => { const params = parseParams(req); const username = params.get("username"); const row = params.getNumberValue("row", CONSTANTS.DEFAULT_MAX_ROW); @@ -43,44 +42,44 @@ export default async (req: ServerRequest) => { if (username === null) { const [base] = req.url.split("?"); - const error = new Error400(`

username is a required query parameter

+ const error = new Error400( + `

username is a required query parameter

The URL should look like ${base}?username=USERNAME, where -USERNAME is your GitHub username.`); - req.respond( +USERNAME is your GitHub username.`, + ); + return new Response( + error.render(), { - body: error.render(), status: error.status, headers: new Headers({ "Content-Type": "text" }), }, ); - return; } const token = Deno.env.get("GITHUB_TOKEN"); const userInfo = await client.requestUserInfo(token, username); if (userInfo === null) { - req.respond( + return new Response( + "Can not find a user with userID: " + username, { - body: "Can not find a user with userID: " + username, status: 404, headers: new Headers({ "Content-Type": "text" }), }, ); - return; } // Success Response - req.respond( + return new Response( + new Card( + titles, + ranks, + column, + row, + CONSTANTS.DEFAULT_PANEL_SIZE, + marginWidth, + paddingHeight, + noBackground, + noFrame, + ).render(userInfo, theme), { - body: new Card( - titles, - ranks, - column, - row, - CONSTANTS.DEFAULT_PANEL_SIZE, - marginWidth, - paddingHeight, - noBackground, - noFrame, - ).render(userInfo, theme), headers: new Headers( { "Content-Type": "image/svg+xml", diff --git a/src/utils.ts b/src/utils.ts index e0a1a2c..e4067a1 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,5 +1,3 @@ -import { ServerRequest } from ".././deps.ts"; - export class CustomURLSearchParams extends URLSearchParams { constructor( init?: string[][] | Record | string | URLSearchParams, @@ -31,13 +29,13 @@ export class CustomURLSearchParams extends URLSearchParams { getBooleanValue(key: string, defaultValue: boolean): boolean { if (super.has(key)) { const param = super.get(key); - return param !== null && param.toString() === 'true'; + return param !== null && param.toString() === "true"; } return defaultValue; } } -export function parseParams(req: ServerRequest): CustomURLSearchParams { +export function parseParams(req: Request): CustomURLSearchParams { const splittedURL = req.url.split("?"); if (splittedURL.length < 2) { return new CustomURLSearchParams(); diff --git a/vercel.json b/vercel.json index f3e8dea..9bceb93 100644 --- a/vercel.json +++ b/vercel.json @@ -1,7 +1,7 @@ { "builds":[{ "src": "**/index.ts", - "use": "vercel-deno@0.7.10" + "use": "vercel-deno@1.1.1" }], "routes": [{ "src": "/.*", "dest": "/" }] } From 5f08ba0e982330b27b8fe28f2b02f3d369a94449 Mon Sep 17 00:00:00 2001 From: ryo-ma Date: Fri, 11 Feb 2022 02:45:31 +0900 Subject: [PATCH 3/4] Fix page format --- index.ts | 2 +- src/error_page.ts | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/index.ts b/index.ts index 6f2bbdb..f42c2a9 100644 --- a/index.ts +++ b/index.ts @@ -43,7 +43,7 @@ export default async (req: Request) => { if (username === null) { const [base] = req.url.split("?"); const error = new Error400( - `

username is a required query parameter

+ `

"username" is a required query parameter

The URL should look like ${base}?username=USERNAME, where USERNAME is your GitHub username.`, ); diff --git a/src/error_page.ts b/src/error_page.ts index 862f6a7..608b594 100644 --- a/src/error_page.ts +++ b/src/error_page.ts @@ -1,11 +1,15 @@ abstract class BaseError { readonly status!: number; + readonly message!: string; constructor(readonly content?: string) {} render(): string { - return `

${this.status}

${this.content ?? ''}`; + return `

${this.status} - ${this.message}

${ + this.content ?? "" + }`; } } export class Error400 extends BaseError { readonly status = 400; + readonly message = "Bad Reuquest"; } From 3544e23c1384d62a773fb31b4ad8e46374f32414 Mon Sep 17 00:00:00 2001 From: ryo-ma Date: Fri, 11 Feb 2022 02:49:45 +0900 Subject: [PATCH 4/4] 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(