forked from loafle/openapi-generator-original
[go-server] add field name in parsing error messages (#18533)
Currently when a parsing rule of a field is not respected (eg: min/max value, required, ...), the api only returns an error message without providing the field name to help the user to fix the request. This commit add the field name to the error message to help the user of the API.
This commit is contained in:
@@ -161,7 +161,7 @@ func (c *PetAPIController) DeletePet(w http.ResponseWriter, r *http.Request) {
|
||||
WithRequire[int64](parseInt64),
|
||||
)
|
||||
if err != nil {
|
||||
c.errorHandler(w, r, &ParsingError{Err: err}, nil)
|
||||
c.errorHandler(w, r, &ParsingError{Param: "petId", Err: err}, nil)
|
||||
return
|
||||
}
|
||||
apiKeyParam := r.Header.Get("api_key")
|
||||
@@ -185,7 +185,7 @@ func (c *PetAPIController) FilterPetsByCategory(w http.ResponseWriter, r *http.R
|
||||
}
|
||||
genderParam, err := NewGenderFromValue(params["gender"])
|
||||
if err != nil {
|
||||
c.errorHandler(w, r, &ParsingError{Err: err}, nil)
|
||||
c.errorHandler(w, r, &ParsingError{Param: "gender", Err: err}, nil)
|
||||
return
|
||||
}
|
||||
var speciesParam Species
|
||||
@@ -204,7 +204,7 @@ func (c *PetAPIController) FilterPetsByCategory(w http.ResponseWriter, r *http.R
|
||||
for _, param := range paramSplits {
|
||||
paramEnum, err := NewSpeciesFromValue(param)
|
||||
if err != nil {
|
||||
c.errorHandler(w, r, &ParsingError{Err: err}, nil)
|
||||
c.errorHandler(w, r, &ParsingError{Param: "notSpecies", Err: err}, nil)
|
||||
return
|
||||
}
|
||||
notSpeciesParam = append(notSpeciesParam, paramEnum)
|
||||
@@ -270,7 +270,7 @@ func (c *PetAPIController) FindPetsByTags(w http.ResponseWriter, r *http.Request
|
||||
if query.Has("bornAfter"){
|
||||
param, err := parseTime(query.Get("bornAfter"))
|
||||
if err != nil {
|
||||
c.errorHandler(w, r, &ParsingError{Err: err}, nil)
|
||||
c.errorHandler(w, r, &ParsingError{Param: "bornAfter", Err: err}, nil)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -283,7 +283,7 @@ func (c *PetAPIController) FindPetsByTags(w http.ResponseWriter, r *http.Request
|
||||
if query.Has("bornBefore"){
|
||||
param, err := parseTime(query.Get("bornBefore"))
|
||||
if err != nil {
|
||||
c.errorHandler(w, r, &ParsingError{Err: err}, nil)
|
||||
c.errorHandler(w, r, &ParsingError{Param: "bornBefore", Err: err}, nil)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -315,7 +315,7 @@ func (c *PetAPIController) GetPetById(w http.ResponseWriter, r *http.Request) {
|
||||
WithRequire[int64](parseInt64),
|
||||
)
|
||||
if err != nil {
|
||||
c.errorHandler(w, r, &ParsingError{Err: err}, nil)
|
||||
c.errorHandler(w, r, &ParsingError{Param: "petId", Err: err}, nil)
|
||||
return
|
||||
}
|
||||
result, err := c.service.GetPetById(r.Context(), petIdParam)
|
||||
@@ -336,7 +336,7 @@ func (c *PetAPIController) GetPetImageById(w http.ResponseWriter, r *http.Reques
|
||||
WithRequire[int64](parseInt64),
|
||||
)
|
||||
if err != nil {
|
||||
c.errorHandler(w, r, &ParsingError{Err: err}, nil)
|
||||
c.errorHandler(w, r, &ParsingError{Param: "petId", Err: err}, nil)
|
||||
return
|
||||
}
|
||||
result, err := c.service.GetPetImageById(r.Context(), petIdParam)
|
||||
@@ -354,7 +354,7 @@ func (c *PetAPIController) GetPetsByTime(w http.ResponseWriter, r *http.Request)
|
||||
params := mux.Vars(r)
|
||||
createdTimeParam, err := parseTime(params["createdTime"])
|
||||
if err != nil {
|
||||
c.errorHandler(w, r, &ParsingError{Err: err}, nil)
|
||||
c.errorHandler(w, r, &ParsingError{Param: "createdTime", Err: err}, nil)
|
||||
return
|
||||
}
|
||||
result, err := c.service.GetPetsByTime(r.Context(), createdTimeParam)
|
||||
@@ -381,7 +381,7 @@ func (c *PetAPIController) GetPetsUsingBooleanQueryParameters(w http.ResponseWri
|
||||
WithParse[bool](parseBool),
|
||||
)
|
||||
if err != nil {
|
||||
c.errorHandler(w, r, &ParsingError{Err: err}, nil)
|
||||
c.errorHandler(w, r, &ParsingError{Param: "expr", Err: err}, nil)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -397,7 +397,7 @@ func (c *PetAPIController) GetPetsUsingBooleanQueryParameters(w http.ResponseWri
|
||||
WithParse[bool](parseBool),
|
||||
)
|
||||
if err != nil {
|
||||
c.errorHandler(w, r, &ParsingError{Err: err}, nil)
|
||||
c.errorHandler(w, r, &ParsingError{Param: "grouping", Err: err}, nil)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -411,7 +411,7 @@ func (c *PetAPIController) GetPetsUsingBooleanQueryParameters(w http.ResponseWri
|
||||
WithParse[bool](parseBool),
|
||||
)
|
||||
if err != nil {
|
||||
c.errorHandler(w, r, &ParsingError{Err: err}, nil)
|
||||
c.errorHandler(w, r, &ParsingError{Param: "inactive", Err: err}, nil)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -444,7 +444,7 @@ func (c *PetAPIController) SearchPet(w http.ResponseWriter, r *http.Request) {
|
||||
WithParse[int64](parseInt64),
|
||||
)
|
||||
if err != nil {
|
||||
c.errorHandler(w, r, &ParsingError{Err: err}, nil)
|
||||
c.errorHandler(w, r, &ParsingError{Param: "age", Err: err}, nil)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -458,7 +458,7 @@ func (c *PetAPIController) SearchPet(w http.ResponseWriter, r *http.Request) {
|
||||
WithParse[float32](parseFloat32),
|
||||
)
|
||||
if err != nil {
|
||||
c.errorHandler(w, r, &ParsingError{Err: err}, nil)
|
||||
c.errorHandler(w, r, &ParsingError{Param: "price", Err: err}, nil)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -469,7 +469,7 @@ func (c *PetAPIController) SearchPet(w http.ResponseWriter, r *http.Request) {
|
||||
if query.Has("bornAfter"){
|
||||
param, err := parseTime(query.Get("bornAfter"))
|
||||
if err != nil {
|
||||
c.errorHandler(w, r, &ParsingError{Err: err}, nil)
|
||||
c.errorHandler(w, r, &ParsingError{Param: "bornAfter", Err: err}, nil)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -483,7 +483,7 @@ func (c *PetAPIController) SearchPet(w http.ResponseWriter, r *http.Request) {
|
||||
WithParse[bool](parseBool),
|
||||
)
|
||||
if err != nil {
|
||||
c.errorHandler(w, r, &ParsingError{Err: err}, nil)
|
||||
c.errorHandler(w, r, &ParsingError{Param: "old", Err: err}, nil)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -539,7 +539,7 @@ func (c *PetAPIController) UpdatePetWithForm(w http.ResponseWriter, r *http.Requ
|
||||
WithRequire[int64](parseInt64),
|
||||
)
|
||||
if err != nil {
|
||||
c.errorHandler(w, r, &ParsingError{Err: err}, nil)
|
||||
c.errorHandler(w, r, &ParsingError{Param: "petId", Err: err}, nil)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -570,7 +570,7 @@ func (c *PetAPIController) UploadFile(w http.ResponseWriter, r *http.Request) {
|
||||
WithRequire[int64](parseInt64),
|
||||
)
|
||||
if err != nil {
|
||||
c.errorHandler(w, r, &ParsingError{Err: err}, nil)
|
||||
c.errorHandler(w, r, &ParsingError{Param: "petId", Err: err}, nil)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -583,7 +583,7 @@ func (c *PetAPIController) UploadFile(w http.ResponseWriter, r *http.Request) {
|
||||
{
|
||||
param, err := ReadFormFileToTempFile(r, "file")
|
||||
if err != nil {
|
||||
c.errorHandler(w, r, &ParsingError{Err: err}, nil)
|
||||
c.errorHandler(w, r, &ParsingError{Param: "file", Err: err}, nil)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -613,7 +613,7 @@ func (c *PetAPIController) UploadFileArrayOfFiles(w http.ResponseWriter, r *http
|
||||
WithRequire[int64](parseInt64),
|
||||
)
|
||||
if err != nil {
|
||||
c.errorHandler(w, r, &ParsingError{Err: err}, nil)
|
||||
c.errorHandler(w, r, &ParsingError{Param: "petId", Err: err}, nil)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -623,7 +623,7 @@ func (c *PetAPIController) UploadFileArrayOfFiles(w http.ResponseWriter, r *http
|
||||
{
|
||||
param, err := ReadFormFilesToTempFiles(r, "files")
|
||||
if err != nil {
|
||||
c.errorHandler(w, r, &ParsingError{Err: err}, nil)
|
||||
c.errorHandler(w, r, &ParsingError{Param: "files", Err: err}, nil)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -114,7 +114,7 @@ func (c *StoreAPIController) GetOrderById(w http.ResponseWriter, r *http.Request
|
||||
WithMaximum[int64](5),
|
||||
)
|
||||
if err != nil {
|
||||
c.errorHandler(w, r, &ParsingError{Err: err}, nil)
|
||||
c.errorHandler(w, r, &ParsingError{Param: "orderId", Err: err}, nil)
|
||||
return
|
||||
}
|
||||
result, err := c.service.GetOrderById(r.Context(), orderIdParam)
|
||||
|
||||
@@ -191,7 +191,7 @@ func (c *UserAPIController) DeleteUser(w http.ResponseWriter, r *http.Request) {
|
||||
WithParse[bool](parseBool),
|
||||
)
|
||||
if err != nil {
|
||||
c.errorHandler(w, r, &ParsingError{Err: err}, nil)
|
||||
c.errorHandler(w, r, &ParsingError{Param: "boolean_test", Err: err}, nil)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -258,7 +258,7 @@ func (c *UserAPIController) LoginUser(w http.ResponseWriter, r *http.Request) {
|
||||
WithParse[bool](parseBool),
|
||||
)
|
||||
if err != nil {
|
||||
c.errorHandler(w, r, &ParsingError{Err: err}, nil)
|
||||
c.errorHandler(w, r, &ParsingError{Param: "boolean_test", Err: err}, nil)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -23,7 +23,8 @@ var (
|
||||
|
||||
// ParsingError indicates that an error has occurred when parsing request parameters
|
||||
type ParsingError struct {
|
||||
Err error
|
||||
Param string
|
||||
Err error
|
||||
}
|
||||
|
||||
func (e *ParsingError) Unwrap() error {
|
||||
@@ -31,7 +32,11 @@ func (e *ParsingError) Unwrap() error {
|
||||
}
|
||||
|
||||
func (e *ParsingError) Error() string {
|
||||
return e.Err.Error()
|
||||
if e.Param == "" {
|
||||
return e.Err.Error()
|
||||
} else {
|
||||
return e.Param + ": " + e.Err.Error()
|
||||
}
|
||||
}
|
||||
|
||||
// RequiredError indicates that an error has occurred when parsing request parameters
|
||||
|
||||
Reference in New Issue
Block a user