-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
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
[BUG][GO][Client] Returned err is never nil #8263
Comments
|
What if we return a |
pointer to a struct (or any variable) will never be nil. https://play.golang.org/p/53vSwOYcCV0 even if it's an empty struct. The return type was earlier |
But one can (knowing that the generated code's |
The 5.0.0-beta3 version has native for go developers behaviour – methods return It would be awesome if you will alow users to configure generator to select desired behaviour: generate methods with custom error or with error interface that covered custom error type. |
Excuse me, but what does it mean? Which one this error is then? It implements standard lib |
Since there seems to be some interest here, I may be able to offer a PR reverting the return type back to |
@thiagoarrais I believe there should not be a problem. In most of the cases we don't need access to the response body if we got an error. |
For my use case I do use the For now I'm using the strategy I've delineated in the playground link above and that has been enough for me. I've thought about publishing a detailed error interface within the generated client: type OpenapiError interface {
Error() string
Model() interface{}
Body() []byte
} and returning it instead of func (a *MyService) MyOperationExecute(r MyOperationRequest) (MyResponse, *_nethttp.Response, OpenapiError) {
// ...
} But I'm not really sure if that will be confusing. It would work like an everyday |
@cheshir Body is required for non-2xx status code response because it will have the message which will indicate the cause to the failure. Using type assertion to convert @thiagoarrais Why not by keeping everybody's interest in mind, return a pointer to an empty struct of |
@code-lucidal58 thanks for the detailed response. Can I ask you to provide example of trimming data after type assertion? I can't imagine what it could be. Here is my example of how it could be: example. The idea of returning wide interface (like @thiagoarrais proposed) is also ok to me. |
@cheshir Thank you for correcting my concept here. I still think that |
@code-lucidal58, the semantic of golang error will be broken. Some part of the community uses codegen to generate wrappers around the API clients for writing logs, metrics, traces etc. They highly depend on the error type. Let's look closer to the idea of returning error type: It would be great to hear thoughts of the other community members. |
I'm in favor of reverting to the previous behaviour to keep the semantics of golang error. I think the helper function is a good way to improve readability and avoid too many code duplications when dealing with errors. Cheers |
This current approach can lead to bugs elsewhere in the code, it leads to really nasty boilerplate everywhere (even worse than the occasional cast):
If you really want to make the Error easily accessible, embed it in an |
That is very close to what we did on our terraform provider too, @zippolyte (can't link to code because it is still closed source, though). And we ran for some time without the helper function because the In my PR (#8344) I've included sample code with that strategy to make it clear how to get the |
Bug Report Checklist
Description
When calling any generated
Execute()
function, a non-nil error is always returned. Instead of using the usual Go idiom:I need to check the actual error message in order to detect errors:
openapi-generator version
c322084 (current main branch)
Related issues/PRs
I believe this was introduced by #8137. Maybe it is intended behaviour. Care to weigh in here, @code-lucidal58?
Suggest a fix
Successful execution of
*Execute()
should not return a non-nil error.The text was updated successfully, but these errors were encountered: