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

Commit

Permalink
feat: c.exception() (#169)
Browse files Browse the repository at this point in the history
  • Loading branch information
boywithkeyboard authored Aug 14, 2023
1 parent 12ca740 commit a2b97e0
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 2 deletions.
67 changes: 65 additions & 2 deletions context.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,35 @@
// Copyright 2023 Samuel Kopp. All rights reserved. Apache-2.0 license.
import { ZodType } from 'https://deno.land/x/zod@v3.21.4/types.ts'
import { RequestContext } from './request_context.ts'
import { ResponseContext } from './response_context.ts'
import { AppContext } from './cheetah.ts'
import { ObjectType, Payload } from './handler.ts'
import { RequestContext } from './request_context.ts'
import { ResponseContext } from './response_context.ts'

const HTTP_MESSAGES = {
'Bad Request': 400,
'Unauthorized': 401,
'Access Denied': 403,
'Not Found': 404,
'Method Not Allowed': 405,
'Not Acceptable': 406,
'Request Timeout': 408,
'Conflict': 409,
'Gone': 410,
'Length Required': 411,
'Precondition Failed': 412,
'Upload Limit Exceeded': 413,
'URI Too Long': 414,
'Unsupported Media Type': 415,
'Range Not Satisfiable': 416,
'Expectation Failed': 417,
'Teapot': 418,
'Misdirected': 421,
'Upgrade Required': 426,
'Precondition Required': 428,
'Rate Limit Exceeded': 429,
'Regional Ban': 451,
'Something Went Wrong': 500,
}

export class Context<
Params extends Record<string, unknown> = Record<string, unknown>,
Expand Down Expand Up @@ -91,4 +117,41 @@ export class Context<
get runtime() {
return this.#a.runtime
}

exception(error: keyof typeof HTTP_MESSAGES, description?: string) {
const code = HTTP_MESSAGES[error]

return new Exception(error, description, code)
}
}

export class Exception {
public response

constructor(
error: string,
description: string | undefined,
code: number,
) {
this.response = (request: Request) => {
const a = request.headers.get('accept')

const json = a
? a.indexOf('application/json') > -1 ||
a.indexOf('*/*') > -1
: false

return new Response(
json ? JSON.stringify({ error, description, code }) : error,
{
headers: {
'content-type': `${
json ? 'application/json' : 'text/plain'
}; charset=utf-8;`,
},
status: code,
},
)
}
}
}
2 changes: 2 additions & 0 deletions exception.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
* throw new Exception('Too Many Requests')
* throw new Exception(429)
* ```
*
* @deprecated please use `c.exception()` instead!
*/
export class Exception {
public response
Expand Down

0 comments on commit a2b97e0

Please sign in to comment.