"correct" way to use both negative and positive responses #2219
Replies: 5 comments 4 replies
-
Thank you for sharing, @HenriJ . I encourage making custom client solutions by providing the In my personal frontend engineering experience the status code is not as important as a binary fact of the response success. Based on that experience I made the Regarding the errors.
I'm not sure I understand what exactly you mean and what the issue is. |
Beta Was this translation helpful? Give feedback.
-
But maybe there can be another variant that does not combine all positive/negative responses, describing each depending on the associated status code instead. That could ease making such a client that involves the status code as the discriminator between the payloads. interface EndpointNegativeResponse {
401: { authMessage: string },
500: { internalMessage: string },
}; However, I'd personally don't go that way, because to me it feels like an assumption. type EndpointNegativeResponse = { authMessage: string } | { internalMessage: string };
if ("authMessage" in payload) {
// that's for 401
}
if ("internalMessage" in payload) {
// that's for 500
} |
Beta Was this translation helpful? Give feedback.
-
Should this interface help you to build a status code based client? |
Beta Was this translation helpful? Give feedback.
-
Yes, @HenriJ : express-zod-api/example/factories.ts Line 65 in 63ea6e9 both |
Beta Was this translation helpful? Give feedback.
-
@HenriJ , |
Beta Was this translation helpful? Give feedback.
-
Hi @RobinTail , this topic is to help me better understand the "correct" way to use both negative and positive responses. I'd also like to share how I currently use express-zod-api to gain your insights.
1. Using http exceptions
My current understanding is that to trigger a negative response (i.e. a non-2xx status code), I need to throw an HTTP exception. This approach is common in many Node.js web frameworks (e.g., Nest), but it has always felt odd to me in scenarios where the situation isn't truly exceptional — such as returning a 404 for a non-existent resource. By using exceptions I am loosing the type-safety of return types (and probably reducing overall performance as exceptions come with some weight).
Because of this, I currently try to limit non-2xx responses to a very specific subset of cases. However, it can still be a challenge—for instance, during authentication, where I need to return different status codes for a 401 response and want them to be strongly typed without throwing errors (which does not seem possible ?).
I'm not sure if this approach would make sense for express-zod-api, but I recall using a framework where handlers explicitly returned a type like
{status: number, headers, body}
, which helped address this issue.2. "RESTier" body return type
Right now, my result handler looks like
meaning when it is a 2xx, simply return the object and otherwise just share some untyped information about the error.
If my understanding is correct, we already know by using the HTTP status code if we are in the positive or negative case and don't need an additional
status
discriminator in the body for that.The existing documentation is very helpful regarding this. I guess, my only feedback, is that the default behavior is a little bit surprising.
3. Generated client and positive/negative responses
In the generated client, the HTTP status code is not taken into account at all, although it could be a discriminator between positive and negative response.
I overloaded the client in my case to use the status code and always return the
PositiveResponse
as any other http status code (it's great you added thesplitResponse
flag for that!).I guess, it could also make sense to overload the client to return
{status: HTTPSTATUSCODE, body: WE_KNOW_IF_POSITIVE_OR_NEGATIVE}
Beta Was this translation helpful? Give feedback.
All reactions