Skip to content
This repository has been archived by the owner on Jun 27, 2024. It is now read-only.

Commit

Permalink
refactor: use new c.exception() everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
boywithkeyboard committed Aug 16, 2023
1 parent b69de26 commit 880400b
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 19 deletions.
6 changes: 3 additions & 3 deletions cheetah.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, unknown> | undefined
Expand Down Expand Up @@ -341,7 +341,7 @@ export class cheetah extends base<cheetah>() {

if (!route) {
if (!this.#notFound) {
throw new Exception(404)
throw new Exception('Not Found', undefined, 404)
}

if (req.method !== 'HEAD') {
Expand Down Expand Up @@ -391,7 +391,7 @@ export class cheetah extends base<cheetah>() {
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)
}
}

Expand Down
7 changes: 4 additions & 3 deletions context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<
Expand Down Expand Up @@ -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
}
Expand All @@ -136,6 +136,7 @@ export class Context<
}
}

/** @private */
export class Exception {
public response

Expand Down
7 changes: 3 additions & 4 deletions oauth/handle_callback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -125,6 +124,6 @@ export async function handleCallback(
...data,
}
} catch (_err) {
throw new Exception('Bad Request')
throw c.exception('Bad Request')
}
}
19 changes: 12 additions & 7 deletions request_context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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> = T extends ZodType ? z.infer<T>
: never
Expand All @@ -30,6 +30,7 @@ export class RequestContext<
#q: Record<string, unknown> | undefined
#r
#s
#e

constructor(
a: AppContext,
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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')
}
}

Expand Down Expand Up @@ -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')
}
}

Expand Down
1 change: 1 addition & 0 deletions test/deps.ts
Original file line number Diff line number Diff line change
@@ -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'
4 changes: 2 additions & 2 deletions x/x.test.tsx
Original file line number Diff line number Diff line change
@@ -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'

Expand Down

0 comments on commit 880400b

Please sign in to comment.