From 880400ba0bc9f9401a288654e69fca9c10269785 Mon Sep 17 00:00:00 2001 From: Samuel Kopp <62482066+boywithkeyboard@users.noreply.github.com> Date: Wed, 16 Aug 2023 19:34:04 +0200 Subject: [PATCH] refactor: use new `c.exception()` everywhere --- cheetah.ts | 6 +++--- context.ts | 7 ++++--- oauth/handle_callback.ts | 7 +++---- request_context.ts | 19 ++++++++++++------- test/deps.ts | 1 + x/x.test.tsx | 4 ++-- 6 files changed, 25 insertions(+), 19 deletions(-) diff --git a/cheetah.ts b/cheetah.ts index feef2e1..c763267 100644 --- a/cheetah.ts +++ b/cheetah.ts @@ -2,12 +2,12 @@ import { base, Method } from './base.ts' import { Collection } from './collection.ts' import { Context } from './context.ts' -import { Exception } from './exception.ts' import { Extension, validExtension } from './extensions.ts' import { Handler, HandlerOrSchema, Payload } from './handler.ts' import { OAuthStore } from './oauth/mod.ts' import { OAuthSessionData } from './oauth/types.ts' import { ResponseContext } from './response_context.ts' +import { Exception } from './context.ts' export type AppContext = { env: Record | undefined @@ -341,7 +341,7 @@ export class cheetah extends base() { if (!route) { if (!this.#notFound) { - throw new Exception(404) + throw new Exception('Not Found', undefined, 404) } if (req.method !== 'HEAD') { @@ -391,7 +391,7 @@ export class cheetah extends base() { if (this.#error) { res = await this.#error(err, req) } else { - res = new Exception('Something Went Wrong').response(req) + res = new Exception('Something Went Wrong', undefined, 500).response(req) } } diff --git a/context.ts b/context.ts index 241fe06..a1e1966 100644 --- a/context.ts +++ b/context.ts @@ -18,7 +18,7 @@ const HTTP_MESSAGES = { 'Gone': 410, 'Length Required': 411, 'Precondition Failed': 412, - 'Upload Limit Exceeded': 413, + 'Content Too Large': 413, 'URI Too Long': 414, 'Unsupported Media Type': 415, 'Range Not Satisfiable': 416, @@ -29,7 +29,7 @@ const HTTP_MESSAGES = { 'Precondition Required': 428, 'Rate Limit Exceeded': 429, 'Regional Ban': 451, - 'Something Went Wrong': 500, + 'Something Went Wrong': 500 } export class Context< @@ -110,7 +110,7 @@ export class Context< return this.#req } - this.#req = new RequestContext(this.#a, this.#p, this.#r, this.#s) + this.#req = new RequestContext(this.#a, this.#p, this.#r, this.#s, this.exception) return this.#req } @@ -136,6 +136,7 @@ export class Context< } } +/** @private */ export class Exception { public response diff --git a/oauth/handle_callback.ts b/oauth/handle_callback.ts index 90b2aa6..d6ab8ad 100644 --- a/oauth/handle_callback.ts +++ b/oauth/handle_callback.ts @@ -6,7 +6,6 @@ import { getUser, } from 'https://deno.land/x/authenticus@v2.0.3/mod.ts' import { Context } from '../context.ts' -import { Exception } from '../exception.ts' import { getVariable } from '../x/env.ts' import { sign, verify } from '../x/jwt.ts' import { LocationData } from '../x/location_data.ts' @@ -31,7 +30,7 @@ export async function handleCallback( typeof c.req.query.state !== 'string' || typeof c.req.query.code !== 'string' ) { - throw new Exception('Bad Request') + throw c.exception('Bad Request') } // validate state @@ -43,7 +42,7 @@ export async function handleCallback( ) if (!payload || payload.ip !== c.req.ip) { - throw new Exception('Access Denied') + throw c.exception('Access Denied') } try { @@ -125,6 +124,6 @@ export async function handleCallback( ...data, } } catch (_err) { - throw new Exception('Bad Request') + throw c.exception('Bad Request') } } diff --git a/request_context.ts b/request_context.ts index 57d8d60..f93825c 100644 --- a/request_context.ts +++ b/request_context.ts @@ -11,7 +11,7 @@ import { } from 'https://deno.land/x/zod@v3.21.4/types.ts' import { Method } from './base.ts' import { BaseType, ObjectType } from './handler.ts' -import { AppContext, Exception } from './mod.ts' +import { AppContext, Context } from './mod.ts' type Static = T extends ZodType ? z.infer : never @@ -30,6 +30,7 @@ export class RequestContext< #q: Record | undefined #r #s + #e constructor( a: AppContext, @@ -42,11 +43,13 @@ export class RequestContext< query?: ObjectType | undefined [key: string]: unknown } | null, + e: Context['exception'], ) { this.#a = a this.#p = p this.#r = r this.#s = s + this.#e = e } get ip(): string { @@ -127,13 +130,15 @@ export class RequestContext< } } } catch (err: unknown) { - throw new Exception(err instanceof DeadlineError ? 413 : 400) + throw this.#e( + err instanceof DeadlineError ? 'Content Too Large' : 'Bad Request', + ) } const result = this.#s.body.safeParse(body) if (!result.success) { - throw new Exception(400) + throw this.#e('Bad Request') } return result.data @@ -153,7 +158,7 @@ export class RequestContext< const header = this.#r.headers.get('cookies') ?? '' if (header.length > 1000) { - throw new Exception(413) + throw this.#e('Content Too Large') } this.#c = header @@ -173,7 +178,7 @@ export class RequestContext< const isValid = this.#s.cookies.safeParse(this.#c).success if (!isValid) { - throw new Exception(400) + throw this.#e('Bad Request') } return this.#c as [ValidatedCookies] extends [never] ? never @@ -212,7 +217,7 @@ export class RequestContext< const isValid = this.#s.headers.safeParse(this.#h).success if (!isValid) { - throw new Exception(400) + throw this.#e('Bad Request') } } @@ -262,7 +267,7 @@ export class RequestContext< const isValid = this.#s.query.safeParse(this.#q).success if (!isValid) { - throw new Exception(400) + throw this.#e('Bad Request') } } diff --git a/test/deps.ts b/test/deps.ts index 084009f..794e987 100644 --- a/test/deps.ts +++ b/test/deps.ts @@ -1,2 +1,3 @@ export { assertEquals } from 'https://deno.land/std@0.198.0/assert/assert_equals.ts' +export { assertInstanceOf } from 'https://deno.land/std@0.198.0/assert/assert_instance_of.ts' export { z } from 'https://deno.land/x/zod@v3.21.4/mod.ts' diff --git a/x/x.test.tsx b/x/x.test.tsx index 7c92dcc..857f4dc 100644 --- a/x/x.test.tsx +++ b/x/x.test.tsx @@ -1,10 +1,10 @@ // Copyright 2023 Samuel Kopp. All rights reserved. Apache-2.0 license. /** @jsx h */ +import { cheetah } from '../cheetah.ts' import { assertEquals, assertInstanceOf, -} from 'https://deno.land/std@0.198.0/testing/asserts.ts' -import { cheetah } from '../cheetah.ts' +} from '../test/deps.ts' import { h, jsx } from './jsx.tsx' import { createKey, importKey, sign, verify } from './jwt.ts'