mirror of
https://github.com/reiyua/github-profile-trophy-reiyua-mirror.git
synced 2026-11-17 06:02:09 +11:00
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:
parent
115908e043
commit
ac3f80b5cb
2 changed files with 18 additions and 2 deletions
9
index.ts
9
index.ts
|
|
@ -3,6 +3,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 "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();
|
||||||
|
|
@ -41,10 +42,14 @@ export default async (req: ServerRequest) => {
|
||||||
).map((r) => r.trim());
|
).map((r) => r.trim());
|
||||||
|
|
||||||
if (username === null) {
|
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(
|
req.respond(
|
||||||
{
|
{
|
||||||
body: "Can not find a query parameter: username",
|
body: error.render(),
|
||||||
status: 404,
|
status: error.status,
|
||||||
headers: new Headers({ "Content-Type": "text" }),
|
headers: new Headers({ "Content-Type": "text" }),
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
|
||||||
11
src/error_page.ts
Normal file
11
src/error_page.ts
Normal 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;
|
||||||
|
}
|
||||||
Loading…
Add table
Reference in a new issue