error.ts 839 B

123456789101112131415161718192021222324252627282930313233343536
  1. import { resolver } from "hono-openapi"
  2. import z from "zod"
  3. import { Storage } from "../storage/storage"
  4. export const ERRORS = {
  5. 400: {
  6. description: "Bad request",
  7. content: {
  8. "application/json": {
  9. schema: resolver(
  10. z
  11. .object({
  12. data: z.any(),
  13. errors: z.array(z.record(z.string(), z.any())),
  14. success: z.literal(false),
  15. })
  16. .meta({
  17. ref: "BadRequestError",
  18. }),
  19. ),
  20. },
  21. },
  22. },
  23. 404: {
  24. description: "Not found",
  25. content: {
  26. "application/json": {
  27. schema: resolver(Storage.NotFoundError.Schema),
  28. },
  29. },
  30. },
  31. } as const
  32. export function errors(...codes: number[]) {
  33. return Object.fromEntries(codes.map((code) => [code, ERRORS[code as keyof typeof ERRORS]]))
  34. }