How to handle "Client"-Error thrown internally from better-call
So, I made the request
GET /auth/get-session
but (accidentally) included a body with the request. This in turn threw TypeError: Request with GET/HEAD method cannot have body
in the backend (I found out that this gets thrown internally from better-call and specifically when creating a native "Request" object with the incorrect parameters; in this case GET with request body). Now, I have a contract that my api resonses must conform to from an open api specification, so I would like to catch this error and format it accordingly. However, how do I know that a certain TypeError
is a client-error or an internal server-error? I need to distinguish between these to provide useful error messages for the client and also not expose internals of the server.3 Replies
One idea I thought of is that if I can catch the TypeError closer to better-auth (instead of in a global exception handler), then I indirectly know a bit more context around the error. If I also can be certain that better-auth never throws a TypeError from any server-related logic, then I can be certain that it is a client error. But yeah, I can't find any documentation on errors thrown by better-auth other than API Error
get requets can't have a body as the rror suggests. the get session endpoint expects query params not a body
Yeah I know, but that doesn't prevent a client from making an invalid
GET
request to that endpoint (or other endpoints)
And I need to handle the exception that is thrown if a client does specifically that
Btw, adding the body there was accidental. I forgot to disable it in postman
Which is kinda part of my point, because that crashed the server when it should've returned an "error response"