Created by: gmtstephane
Hi, in the go server, we can only return 500 and 200 error code whether we have an error or not. The implementation return a variable of type interface{} which contain the body message.
I suggest to change this interface to a struct wich contain the Body message and a return code as well.
// A Response defines the Message and reponse Code for an api response.
type Response struct {
Body interface{}
Code int
}
So the logic is the following.
if err != nil {
EncodeJSONResponse(err.Error(), &result.Code, w)
return
}
EncodeJSONResponse(result.Body, &result.Code, w)
- If we have an error parsing the request (body or parameters), we still have a 500 return error.
- If not, we enter our implementation function.
- If the implementation return an error, we encode the response with the error message as discussed in #7165 , and with the error code provided (500 by default).
- If no error is encoutered, we Encode the response with the custom code (if nil code = 200 by default).
I think being able to return custom code (202,204 etc..) is mandatory for a project aiming standardisation.
For example im trying to implement standard APIs defined with OpenAPI and need to be able to make the difference between 202 and 204 return type. https://forge.etsi.org/swagger/ui/?url=https://forge.etsi.org/gitlab/mec/gs015-bandwith-mgmt-api/raw/master/BwManagementApi.yaml#/
@antihax (2017/11) @grokify (2018/07) @kemokemo (2018/09) @bkabrda (2019/07) @wing328 Thanks
PR checklist
-
Read the contribution guidelines. -
Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community. -
If contributing template-only or documentation-only changes which will change sample output, build the project beforehand. -
Run the shell script ./bin/generate-samples.sh
to update all Petstore samples related to your fix. 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. -
File the PR against the correct branch: master
-
Copy the technical committee to review the pull request if your PR is targeting a particular programming language.