-
Notifications
You must be signed in to change notification settings - Fork 148
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Runtime Client: Can't inspect swagger error response body unless DEBUG flag is True
#89
Comments
The error should be in the error type. I may be misunderstanding the issue though |
Yes, I mean the error response from client which contains the validation error message. The server contains just a single path for POST request with required params I run test with
Without debug flag:
Because response body is closed as I said above, client's consumer have no way to know that request failed due to required parameters. |
I met the same issue at writing a client. There is no way to get a proper error message if an API server returns an error in a response body without DEBUG flag. It's very painful to find out an issue in usual operation. For me, @meomap suggestion that the function reads a response body and wraps it in |
I think the spec is wrong. If you define a default response you're good to go. |
Ah, I see, got it. The spec should define an error response, that's true. Thanks for pointing it out. But what do you think keeping the original body in the I think keeping it is not so difficult, just moving
|
For solving this particular item that is correct however consider the rest of the method. |
I see, your concern is reasonable. I didn't think about that. Thanks for taking time to look this. |
I've run into the same problem. Is there a solution? If the concern is the stream size, maybe perform a check before calling DumpResponse? |
streams are usually produced by Transfer-Encoding: chunked, without a content length specified. |
Thanks @casualjim , but what if you want to be a little more specific about what caused the error. Say, 400 Bad Request. There could be multiple reasons why it's a bad request. Shouldn't that be delivered in the Response Body? And shouldn't that be able to be read? |
You can define responses for each error type (status code) you want to return. For a fairly complete example, see: https://github.com/go-openapi/kvstore/blob/master/swagger/swagger.yml#L103-L150 OpenAPI definitions describe the contract for your service, the more exhaustive it is the better informed your clients are. |
@calavera thank you! I thought we'd already had 400 in the swagger definition for those endpoints, but it turns out we did not. The default response is great suggestion. Works now! |
Part of client code is like this
https://github.com/go-openapi/runtime/blob/master/client/runtime.go#L297
With flag
DEBUG=true
,ioutil.nopCloser
returned and consumer can unwrapClientResponse.Body()
to check for validation error message like this"{\"code\":602,\"message\":\"name in body is required\"}"
.For client in production environment and I don't want
DEBUG
flag on, response body is closed,http.bodyEOFSignal
returned and can't do nothing to check for validation error.Maybe there's option to retain response body or turn response body into
ioutil.nopCloser
likehttputil.DumpResponse
did ?The text was updated successfully, but these errors were encountered: