HonoH
Hono2y ago
Davis

How to set up and use a structured error format?

Hi;

I am not strong in API design, and had a question about structuring error formats.

I wish to create error formats, that are extensions of the
Error
instance, let's all it MY_API_ERROR_INSTANCE

Something like:

c.json({
  message: "The route/endpoint you are looking for does not exist. It may have been removed or moved to a different location.",
  type: "Not Found",
  types: ["Not Found", "Error"],
  status: 404,
  cause: { ...recurse for nested any exceptions... }
})


But I don't wish to always type this out, and I also want to create a type-safe and generic one, in that it can accommodate different types of responses & statuses.

Has anyone implemented something like this, or has any resources they can share about this kind of API design?

I appreciate the help.

P.S. For context, I am building targeting Cloudflare Workers, Neon, Drizzle... if that helps with some context.
Was this page helpful?