-
-
Notifications
You must be signed in to change notification settings - Fork 7k
[go-server] Allow user to specify how errors are handled #9764
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
Conversation
|
I'd like to see more feedback on this PR. I'm not getting what the error handler is trying to solve or improve. |
Sure! Let's assume there are 2 types of users of the generator:
For type 1 usersIf their specifications require them to return errors with fields like
Second, let's say you have a debug mode in your application. If debug is turned on, the details of the error responses are more verbose, and if off, it merely returns "unexpected error".
Case 2 is where it shinesWhere I work, we do not modify any of the generated files. Instead, we use overlays that embed the servicer. If we have such a spec as laid out in the first point, we will need to reimplement the controller. In addition to the tediousness, this process is extremely error-prone especially if the specs are changing. The concerns could be cleanly separated with an injection of an
To sum it up, Aside from this, Let me know if you have any questions! |
Thank you @lwj5, Now I get a clearer picture of what you had in mind when you submitted this PR. P.S: In the meantime, I'd like to know others' opinions regarding this change. |
No worries, thanks for doing the review |
@lwj5 thanks for the detailed explanation. One minor suggestion is to add some comments in https://github.com/OpenAPITools/openapi-generator/pull/9764/files#diff-40523ac4e3f7daf99f002d095c93bb305acc3a313f05b8160654207a3661a942 explaining what those functions do so as to onboard new Go users more easily. |
I rebased it with the master, the last 3 commits are new.
Done 👍 |
I've been using this branch with a custom error handler for a bit, it is working as intended and I've managed to inject debug and stack trace information to err responses originating from the servicers. This is all done without the need for the controller or the servicer to know whether the debug flag is set. |
modules/openapi-generator/src/main/resources/go-server/controller-api.mustache
Outdated
Show resolved
Hide resolved
EncodeJSONResponse(err.Error(), func(i int) *int { return &i }(http.StatusBadRequest), map[string][]string{}, w) | ||
} else { | ||
// Handle all other errors | ||
EncodeJSONResponse(err.Error(), &result.Code, result.Headers, w) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The same can be done like the if part.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
similar to the above. I'm ok with changing it
func DefaultErrorHandler(w http.ResponseWriter, r *http.Request, err error, result *ImplResponse) { | ||
if _, ok := err.(*ParsingError); ok { | ||
// Handle parsing errors | ||
EncodeJSONResponse(err.Error(), func(i int) *int { return &i }(http.StatusBadRequest), map[string][]string{}, w) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is how all errors are being handled by the controller prior to the PR. I'm okay with changing it, but it would mean a change in the implementation for those currently using the generator.
if _, ok := err.(*ParsingError); ok { | ||
// Handle parsing errors | ||
EncodeJSONResponse(err.Error(), func(i int) *int { return &i }(http.StatusBadRequest), map[string][]string{}, w) | ||
} else { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's up to you then :)
This PR LGTM, I've tested it locally, and it works fine. And sorry for the delay :) |
Yes, changes completed. It's better to create a new PR for changing the default error handling (if anyone wants to). No worries, thank you very much for reviewing |
@lwj5 I think you need to resubmit the samples. |
Let me run that now |
@wing328 LGTM |
CircleCI failure not related to this change. |
error.go
with an error handlerTested on both go-server with
mux
andchi
routersPR checklist
This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
These must match the expectations made by your contribution.
You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example
./bin/generate-samples.sh bin/configs/java*
.For Windows users, please run the script in Git BASH.
master
,5.1.x
,6.0.x
Hi @antihax @grokify @kemokemo @jirikuncar @ph4r5h4d, please help to take a look at this PR