switch to monorepo structure
This commit is contained in:
22
packages/server/src/http-handler.ts
Normal file
22
packages/server/src/http-handler.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { HttpError } from "./error.ts"
|
||||
|
||||
function httpHandler<Route extends string>(
|
||||
handler: (request: Bun.BunRequest<Route>) => Promise<Response>,
|
||||
): (request: Bun.BunRequest<Route>) => Promise<Response> {
|
||||
return async (request) => {
|
||||
try {
|
||||
const response = await handler(request)
|
||||
return response
|
||||
} catch (error) {
|
||||
if (error instanceof HttpError) {
|
||||
if (error.message) {
|
||||
return Response.json({ message: error.message }, { status: error.status })
|
||||
}
|
||||
return new Response(undefined, { status: error.status })
|
||||
}
|
||||
return new Response(undefined, { status: 500 })
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export { httpHandler }
|
Reference in New Issue
Block a user