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.
This commit is contained in:
Spenser Black 2022-02-10 10:33:03 -05:00
parent 115908e043
commit ac3f80b5cb
No known key found for this signature in database
GPG key ID: D91539A650567082
2 changed files with 18 additions and 2 deletions

View file

@ -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(`<h2><code>username</code> is a required query parameter</h2>
<p>The URL should look like <code>${base}?username=USERNAME</code>, where
<code>USERNAME</code> is <em>your GitHub username.</em>`);
req.respond(
{
body: "Can not find a query parameter: username",
status: 404,
body: error.render(),
status: error.status,
headers: new Headers({ "Content-Type": "text" }),
},
);

11
src/error_page.ts Normal file
View file

@ -0,0 +1,11 @@
abstract class BaseError {
readonly status!: number;
constructor(readonly content?: string) {}
render(): string {
return `<!DOCTYPE html><html><body><h1>${this.status}</h1>${this.content ?? ''}</body></html>`;
}
}
export class Error400 extends BaseError {
readonly status = 400;
}