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:
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.1 Reply
Hi, a solution for this problem is to catch all errors and format them in one place, like this :
So when you throw an error like this :
The error is formatted to your app standard error format.
Another one is create some classes that extends HTTPException, if you want type safety and more complex error objects.