feat: implement error handling and validation middleware, enhance result handling
Co-authored-by: Copilot <copilot@github.com>
This commit is contained in:
20
apps/backend/src/lib/http/handle-result.ts
Normal file
20
apps/backend/src/lib/http/handle-result.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import type { Context } from 'hono'
|
||||
import type { ContentfulStatusCode } from 'hono/utils/http-status'
|
||||
import type { Result } from '@/lib/result'
|
||||
import { mapAppErrorToProblem } from './problem-mapper'
|
||||
|
||||
export function handleResult<T>(
|
||||
c: Context,
|
||||
result: Result<T>,
|
||||
successStatus: ContentfulStatusCode = 200
|
||||
) {
|
||||
if (!result.ok) {
|
||||
const requestId = c.get('requestId') as string | undefined
|
||||
const instance = requestId ? `/requests/${requestId}` : undefined
|
||||
const problem = mapAppErrorToProblem(result.error, instance)
|
||||
|
||||
return c.json(problem, problem.status)
|
||||
}
|
||||
|
||||
return c.json(result.value, successStatus)
|
||||
}
|
||||
Reference in New Issue
Block a user