diff --git a/examples/petstore-expanded/gin/api/petstore-server.gen.go b/examples/petstore-expanded/gin/api/petstore-server.gen.go index b05b8437a..8dabdd9e7 100644 --- a/examples/petstore-expanded/gin/api/petstore-server.gen.go +++ b/examples/petstore-expanded/gin/api/petstore-server.gen.go @@ -38,6 +38,7 @@ type ServerInterface interface { type ServerInterfaceWrapper struct { Handler ServerInterface HandlerMiddlewares []MiddlewareFunc + ErrorHandler func(*gin.Context, error, int) } type MiddlewareFunc func(c *gin.Context) @@ -54,7 +55,7 @@ func (siw *ServerInterfaceWrapper) FindPets(c *gin.Context) { err = runtime.BindQueryParameter("form", true, false, "tags", c.Request.URL.Query(), ¶ms.Tags) if err != nil { - c.JSON(http.StatusBadRequest, gin.H{"msg": fmt.Sprintf("Invalid format for parameter tags: %s", err)}) + siw.ErrorHandler(c, fmt.Errorf("Invalid format for parameter tags: %s", err), http.StatusBadRequest) return } @@ -62,7 +63,7 @@ func (siw *ServerInterfaceWrapper) FindPets(c *gin.Context) { err = runtime.BindQueryParameter("form", true, false, "limit", c.Request.URL.Query(), ¶ms.Limit) if err != nil { - c.JSON(http.StatusBadRequest, gin.H{"msg": fmt.Sprintf("Invalid format for parameter limit: %s", err)}) + siw.ErrorHandler(c, fmt.Errorf("Invalid format for parameter limit: %s", err), http.StatusBadRequest) return } @@ -93,7 +94,7 @@ func (siw *ServerInterfaceWrapper) DeletePet(c *gin.Context) { err = runtime.BindStyledParameter("simple", false, "id", c.Param("id"), &id) if err != nil { - c.JSON(http.StatusBadRequest, gin.H{"msg": fmt.Sprintf("Invalid format for parameter id: %s", err)}) + siw.ErrorHandler(c, fmt.Errorf("Invalid format for parameter id: %s", err), http.StatusBadRequest) return } @@ -114,7 +115,7 @@ func (siw *ServerInterfaceWrapper) FindPetByID(c *gin.Context) { err = runtime.BindStyledParameter("simple", false, "id", c.Param("id"), &id) if err != nil { - c.JSON(http.StatusBadRequest, gin.H{"msg": fmt.Sprintf("Invalid format for parameter id: %s", err)}) + siw.ErrorHandler(c, fmt.Errorf("Invalid format for parameter id: %s", err), http.StatusBadRequest) return } @@ -127,8 +128,9 @@ func (siw *ServerInterfaceWrapper) FindPetByID(c *gin.Context) { // GinServerOptions provides options for the Gin server. type GinServerOptions struct { - BaseURL string - Middlewares []MiddlewareFunc + BaseURL string + Middlewares []MiddlewareFunc + ErrorHandler func(*gin.Context, error, int) } // RegisterHandlers creates http.Handler with routing matching OpenAPI spec. @@ -138,9 +140,19 @@ func RegisterHandlers(router *gin.Engine, si ServerInterface) *gin.Engine { // RegisterHandlersWithOptions creates http.Handler with additional options func RegisterHandlersWithOptions(router *gin.Engine, si ServerInterface, options GinServerOptions) *gin.Engine { + + errorHandler := options.ErrorHandler + + if errorHandler == nil { + errorHandler = func(c *gin.Context, err error, statusCode int) { + c.JSON(statusCode, gin.H{"msg": err.Error()}) + } + } + wrapper := ServerInterfaceWrapper{ Handler: si, HandlerMiddlewares: options.Middlewares, + ErrorHandler: errorHandler, } router.GET(options.BaseURL+"/pets", wrapper.FindPets) diff --git a/internal/test/server/server_moq.gen.go b/internal/test/server/server_moq.gen.go index aa22e65ca..80bc73a9e 100644 --- a/internal/test/server/server_moq.gen.go +++ b/internal/test/server/server_moq.gen.go @@ -14,46 +14,46 @@ var _ ServerInterface = &ServerInterfaceMock{} // ServerInterfaceMock is a mock implementation of ServerInterface. // -// func TestSomethingThatUsesServerInterface(t *testing.T) { +// func TestSomethingThatUsesServerInterface(t *testing.T) { // -// // make and configure a mocked ServerInterface -// mockedServerInterface := &ServerInterfaceMock{ -// CreateResourceFunc: func(w http.ResponseWriter, r *http.Request, argument string) { -// panic("mock out the CreateResource method") -// }, -// CreateResource2Func: func(w http.ResponseWriter, r *http.Request, inlineArgument int, params CreateResource2Params) { -// panic("mock out the CreateResource2 method") -// }, -// GetEveryTypeOptionalFunc: func(w http.ResponseWriter, r *http.Request) { -// panic("mock out the GetEveryTypeOptional method") -// }, -// GetReservedKeywordFunc: func(w http.ResponseWriter, r *http.Request) { -// panic("mock out the GetReservedKeyword method") -// }, -// GetResponseWithReferenceFunc: func(w http.ResponseWriter, r *http.Request) { -// panic("mock out the GetResponseWithReference method") -// }, -// GetSimpleFunc: func(w http.ResponseWriter, r *http.Request) { -// panic("mock out the GetSimple method") -// }, -// GetWithArgsFunc: func(w http.ResponseWriter, r *http.Request, params GetWithArgsParams) { -// panic("mock out the GetWithArgs method") -// }, -// GetWithContentTypeFunc: func(w http.ResponseWriter, r *http.Request, contentType GetWithContentTypeParamsContentType) { -// panic("mock out the GetWithContentType method") -// }, -// GetWithReferencesFunc: func(w http.ResponseWriter, r *http.Request, globalArgument int64, argument string) { -// panic("mock out the GetWithReferences method") -// }, -// UpdateResource3Func: func(w http.ResponseWriter, r *http.Request, pFallthrough int) { -// panic("mock out the UpdateResource3 method") -// }, -// } +// // make and configure a mocked ServerInterface +// mockedServerInterface := &ServerInterfaceMock{ +// CreateResourceFunc: func(w http.ResponseWriter, r *http.Request, argument string) { +// panic("mock out the CreateResource method") +// }, +// CreateResource2Func: func(w http.ResponseWriter, r *http.Request, inlineArgument int, params CreateResource2Params) { +// panic("mock out the CreateResource2 method") +// }, +// GetEveryTypeOptionalFunc: func(w http.ResponseWriter, r *http.Request) { +// panic("mock out the GetEveryTypeOptional method") +// }, +// GetReservedKeywordFunc: func(w http.ResponseWriter, r *http.Request) { +// panic("mock out the GetReservedKeyword method") +// }, +// GetResponseWithReferenceFunc: func(w http.ResponseWriter, r *http.Request) { +// panic("mock out the GetResponseWithReference method") +// }, +// GetSimpleFunc: func(w http.ResponseWriter, r *http.Request) { +// panic("mock out the GetSimple method") +// }, +// GetWithArgsFunc: func(w http.ResponseWriter, r *http.Request, params GetWithArgsParams) { +// panic("mock out the GetWithArgs method") +// }, +// GetWithContentTypeFunc: func(w http.ResponseWriter, r *http.Request, contentType GetWithContentTypeParamsContentType) { +// panic("mock out the GetWithContentType method") +// }, +// GetWithReferencesFunc: func(w http.ResponseWriter, r *http.Request, globalArgument int64, argument string) { +// panic("mock out the GetWithReferences method") +// }, +// UpdateResource3Func: func(w http.ResponseWriter, r *http.Request, pFallthrough int) { +// panic("mock out the UpdateResource3 method") +// }, +// } // -// // use mockedServerInterface in code that requires ServerInterface -// // and then make assertions. +// // use mockedServerInterface in code that requires ServerInterface +// // and then make assertions. // -// } +// } type ServerInterfaceMock struct { // CreateResourceFunc mocks the CreateResource method. CreateResourceFunc func(w http.ResponseWriter, r *http.Request, argument string) @@ -208,7 +208,8 @@ func (mock *ServerInterfaceMock) CreateResource(w http.ResponseWriter, r *http.R // CreateResourceCalls gets all the calls that were made to CreateResource. // Check the length with: -// len(mockedServerInterface.CreateResourceCalls()) +// +// len(mockedServerInterface.CreateResourceCalls()) func (mock *ServerInterfaceMock) CreateResourceCalls() []struct { W http.ResponseWriter R *http.Request @@ -249,7 +250,8 @@ func (mock *ServerInterfaceMock) CreateResource2(w http.ResponseWriter, r *http. // CreateResource2Calls gets all the calls that were made to CreateResource2. // Check the length with: -// len(mockedServerInterface.CreateResource2Calls()) +// +// len(mockedServerInterface.CreateResource2Calls()) func (mock *ServerInterfaceMock) CreateResource2Calls() []struct { W http.ResponseWriter R *http.Request @@ -288,7 +290,8 @@ func (mock *ServerInterfaceMock) GetEveryTypeOptional(w http.ResponseWriter, r * // GetEveryTypeOptionalCalls gets all the calls that were made to GetEveryTypeOptional. // Check the length with: -// len(mockedServerInterface.GetEveryTypeOptionalCalls()) +// +// len(mockedServerInterface.GetEveryTypeOptionalCalls()) func (mock *ServerInterfaceMock) GetEveryTypeOptionalCalls() []struct { W http.ResponseWriter R *http.Request @@ -323,7 +326,8 @@ func (mock *ServerInterfaceMock) GetReservedKeyword(w http.ResponseWriter, r *ht // GetReservedKeywordCalls gets all the calls that were made to GetReservedKeyword. // Check the length with: -// len(mockedServerInterface.GetReservedKeywordCalls()) +// +// len(mockedServerInterface.GetReservedKeywordCalls()) func (mock *ServerInterfaceMock) GetReservedKeywordCalls() []struct { W http.ResponseWriter R *http.Request @@ -358,7 +362,8 @@ func (mock *ServerInterfaceMock) GetResponseWithReference(w http.ResponseWriter, // GetResponseWithReferenceCalls gets all the calls that were made to GetResponseWithReference. // Check the length with: -// len(mockedServerInterface.GetResponseWithReferenceCalls()) +// +// len(mockedServerInterface.GetResponseWithReferenceCalls()) func (mock *ServerInterfaceMock) GetResponseWithReferenceCalls() []struct { W http.ResponseWriter R *http.Request @@ -393,7 +398,8 @@ func (mock *ServerInterfaceMock) GetSimple(w http.ResponseWriter, r *http.Reques // GetSimpleCalls gets all the calls that were made to GetSimple. // Check the length with: -// len(mockedServerInterface.GetSimpleCalls()) +// +// len(mockedServerInterface.GetSimpleCalls()) func (mock *ServerInterfaceMock) GetSimpleCalls() []struct { W http.ResponseWriter R *http.Request @@ -430,7 +436,8 @@ func (mock *ServerInterfaceMock) GetWithArgs(w http.ResponseWriter, r *http.Requ // GetWithArgsCalls gets all the calls that were made to GetWithArgs. // Check the length with: -// len(mockedServerInterface.GetWithArgsCalls()) +// +// len(mockedServerInterface.GetWithArgsCalls()) func (mock *ServerInterfaceMock) GetWithArgsCalls() []struct { W http.ResponseWriter R *http.Request @@ -469,7 +476,8 @@ func (mock *ServerInterfaceMock) GetWithContentType(w http.ResponseWriter, r *ht // GetWithContentTypeCalls gets all the calls that were made to GetWithContentType. // Check the length with: -// len(mockedServerInterface.GetWithContentTypeCalls()) +// +// len(mockedServerInterface.GetWithContentTypeCalls()) func (mock *ServerInterfaceMock) GetWithContentTypeCalls() []struct { W http.ResponseWriter R *http.Request @@ -510,7 +518,8 @@ func (mock *ServerInterfaceMock) GetWithReferences(w http.ResponseWriter, r *htt // GetWithReferencesCalls gets all the calls that were made to GetWithReferences. // Check the length with: -// len(mockedServerInterface.GetWithReferencesCalls()) +// +// len(mockedServerInterface.GetWithReferencesCalls()) func (mock *ServerInterfaceMock) GetWithReferencesCalls() []struct { W http.ResponseWriter R *http.Request @@ -551,7 +560,8 @@ func (mock *ServerInterfaceMock) UpdateResource3(w http.ResponseWriter, r *http. // UpdateResource3Calls gets all the calls that were made to UpdateResource3. // Check the length with: -// len(mockedServerInterface.UpdateResource3Calls()) +// +// len(mockedServerInterface.UpdateResource3Calls()) func (mock *ServerInterfaceMock) UpdateResource3Calls() []struct { W http.ResponseWriter R *http.Request diff --git a/internal/test/strict-server/gin/server.gen.go b/internal/test/strict-server/gin/server.gen.go index 5192a0dc5..837cb70ab 100644 --- a/internal/test/strict-server/gin/server.gen.go +++ b/internal/test/strict-server/gin/server.gen.go @@ -57,6 +57,7 @@ type ServerInterface interface { type ServerInterfaceWrapper struct { Handler ServerInterface HandlerMiddlewares []MiddlewareFunc + ErrorHandler func(*gin.Context, error, int) } type MiddlewareFunc func(c *gin.Context) @@ -156,20 +157,20 @@ func (siw *ServerInterfaceWrapper) HeadersExample(c *gin.Context) { var Header1 string n := len(valueList) if n != 1 { - c.JSON(http.StatusBadRequest, gin.H{"msg": fmt.Sprintf("Expected one value for header1, got %d", n)}) + siw.ErrorHandler(c, fmt.Errorf("Expected one value for header1, got %d", n), http.StatusBadRequest) return } err = runtime.BindStyledParameterWithLocation("simple", false, "header1", runtime.ParamLocationHeader, valueList[0], &Header1) if err != nil { - c.JSON(http.StatusBadRequest, gin.H{"msg": fmt.Sprintf("Invalid format for parameter header1: %s", err)}) + siw.ErrorHandler(c, fmt.Errorf("Invalid format for parameter header1: %s", err), http.StatusBadRequest) return } params.Header1 = Header1 } else { - c.JSON(http.StatusBadRequest, gin.H{"msg": fmt.Sprintf("Header parameter header1 is required, but not found: %s", err)}) + siw.ErrorHandler(c, fmt.Errorf("Header parameter header1 is required, but not found: %s", err), http.StatusBadRequest) return } @@ -178,13 +179,13 @@ func (siw *ServerInterfaceWrapper) HeadersExample(c *gin.Context) { var Header2 int n := len(valueList) if n != 1 { - c.JSON(http.StatusBadRequest, gin.H{"msg": fmt.Sprintf("Expected one value for header2, got %d", n)}) + siw.ErrorHandler(c, fmt.Errorf("Expected one value for header2, got %d", n), http.StatusBadRequest) return } err = runtime.BindStyledParameterWithLocation("simple", false, "header2", runtime.ParamLocationHeader, valueList[0], &Header2) if err != nil { - c.JSON(http.StatusBadRequest, gin.H{"msg": fmt.Sprintf("Invalid format for parameter header2: %s", err)}) + siw.ErrorHandler(c, fmt.Errorf("Invalid format for parameter header2: %s", err), http.StatusBadRequest) return } @@ -201,8 +202,9 @@ func (siw *ServerInterfaceWrapper) HeadersExample(c *gin.Context) { // GinServerOptions provides options for the Gin server. type GinServerOptions struct { - BaseURL string - Middlewares []MiddlewareFunc + BaseURL string + Middlewares []MiddlewareFunc + ErrorHandler func(*gin.Context, error, int) } // RegisterHandlers creates http.Handler with routing matching OpenAPI spec. @@ -212,9 +214,19 @@ func RegisterHandlers(router *gin.Engine, si ServerInterface) *gin.Engine { // RegisterHandlersWithOptions creates http.Handler with additional options func RegisterHandlersWithOptions(router *gin.Engine, si ServerInterface, options GinServerOptions) *gin.Engine { + + errorHandler := options.ErrorHandler + + if errorHandler == nil { + errorHandler = func(c *gin.Context, err error, statusCode int) { + c.JSON(statusCode, gin.H{"msg": err.Error()}) + } + } + wrapper := ServerInterfaceWrapper{ Handler: si, HandlerMiddlewares: options.Middlewares, + ErrorHandler: errorHandler, } router.POST(options.BaseURL+"/json", wrapper.JSONExample) diff --git a/pkg/codegen/templates/gin/gin-register.tmpl b/pkg/codegen/templates/gin/gin-register.tmpl index 5362dd7ab..188cbb83e 100644 --- a/pkg/codegen/templates/gin/gin-register.tmpl +++ b/pkg/codegen/templates/gin/gin-register.tmpl @@ -2,6 +2,7 @@ type GinServerOptions struct { BaseURL string Middlewares []MiddlewareFunc + ErrorHandler func(*gin.Context, error, int) } // RegisterHandlers creates http.Handler with routing matching OpenAPI spec. @@ -11,13 +12,23 @@ func RegisterHandlers(router *gin.Engine, si ServerInterface) *gin.Engine { // RegisterHandlersWithOptions creates http.Handler with additional options func RegisterHandlersWithOptions(router *gin.Engine, si ServerInterface, options GinServerOptions) *gin.Engine { -{{if .}}wrapper := ServerInterfaceWrapper{ -Handler: si, -HandlerMiddlewares: options.Middlewares, -} -{{end}} -{{range .}} -router.{{.Method }}(options.BaseURL+"{{.Path | swaggerUriToGinUri }}", wrapper.{{.OperationId}}) -{{end}} -return router + {{if .}} + errorHandler := options.ErrorHandler + + if errorHandler == nil { + errorHandler = func(c *gin.Context, err error, statusCode int) { + c.JSON(statusCode, gin.H{"msg": err.Error()}) + } + } + + wrapper := ServerInterfaceWrapper{ + Handler: si, + HandlerMiddlewares: options.Middlewares, + ErrorHandler: errorHandler, + } + {{end}} + {{range .}} + router.{{.Method }}(options.BaseURL+"{{.Path | swaggerUriToGinUri }}", wrapper.{{.OperationId}}) + {{end}} + return router } diff --git a/pkg/codegen/templates/gin/gin-wrappers.tmpl b/pkg/codegen/templates/gin/gin-wrappers.tmpl index 59d03f504..fae5c04c2 100644 --- a/pkg/codegen/templates/gin/gin-wrappers.tmpl +++ b/pkg/codegen/templates/gin/gin-wrappers.tmpl @@ -2,6 +2,7 @@ type ServerInterfaceWrapper struct { Handler ServerInterface HandlerMiddlewares []MiddlewareFunc + ErrorHandler func(*gin.Context, error, int) } type MiddlewareFunc func(c *gin.Context) @@ -24,14 +25,14 @@ func (siw *ServerInterfaceWrapper) {{$opid}}(c *gin.Context) { {{if .IsJson}} err = json.Unmarshal([]byte(c.Query("{{.ParamName}}")), &{{$varName}}) if err != nil { - c.JSON(http.StatusBadRequest, gin.H{"msg": "Error unmarshaling parameter '{{.ParamName}}' as JSON"}) + siw.ErrorHandler(c, fmt.Errorf("Error unmarshaling parameter '{{.ParamName}}' as JSON"), http.StatusBadRequest) return } {{end}} {{if .IsStyled}} err = runtime.BindStyledParameter("{{.Style}}",{{.Explode}}, "{{.ParamName}}", c.Param("{{.ParamName}}"), &{{$varName}}) if err != nil { - c.JSON(http.StatusBadRequest, gin.H{"msg": fmt.Sprintf("Invalid format for parameter {{.ParamName}}: %s", err)}) + siw.ErrorHandler(c, fmt.Errorf("Invalid format for parameter {{.ParamName}}: %s", err), http.StatusBadRequest) return } {{end}} @@ -61,21 +62,22 @@ func (siw *ServerInterfaceWrapper) {{$opid}}(c *gin.Context) { var value {{.TypeDef}} err = json.Unmarshal([]byte(paramValue), &value) if err != nil { - c.JSON(http.StatusBadRequest, gin.H{"msg": "Error unmarshaling parameter '{{.ParamName}}' as JSON"}) - return + siw.ErrorHandler(c, fmt.Errorf("Error unmarshalling parameter '{{.ParamName}}' as JSON: %s", err), http.StatusBadRequest) { } params.{{.GoName}} = {{if not .Required}}&{{end}}value {{end}} }{{if .Required}} else { - c.JSON(http.StatusBadRequest, gin.H{"msg": "Query argument {{.ParamName}} is required, but not found"}) - return + if !siw.ErrorHandler(c, fmt.Errorf("Query argument {{.ParamName}} is required, but not found: %s", err), http.StatusBadRequest) { + return + } }{{end}} {{end}} + {{if .IsStyled}} err = runtime.BindQueryParameter("{{.Style}}", {{.Explode}}, {{.Required}}, "{{.ParamName}}", c.Request.URL.Query(), ¶ms.{{.GoName}}) if err != nil { - c.JSON(http.StatusBadRequest, gin.H{"msg": fmt.Sprintf("Invalid format for parameter {{.ParamName}}: %s", err)}) + siw.ErrorHandler(c, fmt.Errorf("Invalid format for parameter {{.ParamName}}: %s", err), http.StatusBadRequest) return } {{end}} @@ -89,7 +91,7 @@ func (siw *ServerInterfaceWrapper) {{$opid}}(c *gin.Context) { var {{.GoName}} {{.TypeDef}} n := len(valueList) if n != 1 { - c.JSON(http.StatusBadRequest, gin.H{"msg": fmt.Sprintf("Expected one value for {{.ParamName}}, got %d", n)}) + siw.ErrorHandler(c, fmt.Errorf("Expected one value for {{.ParamName}}, got %d", n), http.StatusBadRequest) return } @@ -100,7 +102,7 @@ func (siw *ServerInterfaceWrapper) {{$opid}}(c *gin.Context) { {{if .IsJson}} err = json.Unmarshal([]byte(valueList[0]), &{{.GoName}}) if err != nil { - c.JSON(http.StatusBadRequest, gin.H{"msg": "Error unmarshaling parameter '{{.ParamName}}' as JSON"}) + siw.ErrorHandler(c, fmt.Errorf("Error unmarshaling parameter '{{.ParamName}}' as JSON"), http.StatusBadRequest) return } {{end}} @@ -108,7 +110,7 @@ func (siw *ServerInterfaceWrapper) {{$opid}}(c *gin.Context) { {{if .IsStyled}} err = runtime.BindStyledParameterWithLocation("{{.Style}}",{{.Explode}}, "{{.ParamName}}", runtime.ParamLocationHeader, valueList[0], &{{.GoName}}) if err != nil { - c.JSON(http.StatusBadRequest, gin.H{"msg": fmt.Sprintf("Invalid format for parameter {{.ParamName}}: %s", err)}) + siw.ErrorHandler(c, fmt.Errorf("Invalid format for parameter {{.ParamName}}: %s", err), http.StatusBadRequest) return } {{end}} @@ -116,7 +118,7 @@ func (siw *ServerInterfaceWrapper) {{$opid}}(c *gin.Context) { params.{{.GoName}} = {{if not .Required}}&{{end}}{{.GoName}} } {{if .Required}}else { - c.JSON(http.StatusBadRequest, gin.H{"msg": fmt.Sprintf("Header parameter {{.ParamName}} is required, but not found: %s", err)}) + siw.ErrorHandler(c, fmt.Errorf("Header parameter {{.ParamName}} is required, but not found: %s", err), http.StatusBadRequest) return }{{end}} @@ -137,14 +139,14 @@ func (siw *ServerInterfaceWrapper) {{$opid}}(c *gin.Context) { var decoded string decoded, err := url.QueryUnescape(cookie.Value) if err != nil { - c.JSON(http.StatusBadRequest, gin.H{"msg": "Error unescaping cookie parameter '{{.ParamName}}'"}) - return + siw.ErrorHandler(c, fmt.Errorf("Error unescaping cookie parameter '{{.ParamName}}'"), http.StatusBadRequest) + return } err = json.Unmarshal([]byte(decoded), &value) if err != nil { - c.JSON(http.StatusBadRequest, gin.H{"msg": "Error unmarshaling parameter '{{.ParamName}}' as JSON"}) - return + siw.ErrorHandler(c, fmt.Errorf("Error unmarshaling parameter '{{.ParamName}}' as JSON"), http.StatusBadRequest) + return } params.{{.GoName}} = {{if not .Required}}&{{end}}value @@ -154,8 +156,8 @@ func (siw *ServerInterfaceWrapper) {{$opid}}(c *gin.Context) { var value {{.TypeDef}} err = runtime.BindStyledParameter("simple",{{.Explode}}, "{{.ParamName}}", cookie.Value, &value) if err != nil { - c.JSON(http.StatusBadRequest, gin.H{"msg": "Invalid format for parameter {{.ParamName}}: %s"}) - return + siw.ErrorHandler(c, fmt.Errorf("Invalid format for parameter {{.ParamName}}: %s", err), http.StatusBadRequest) + return } params.{{.GoName}} = {{if not .Required}}&{{end}}value {{end}} @@ -163,7 +165,7 @@ func (siw *ServerInterfaceWrapper) {{$opid}}(c *gin.Context) { } {{- if .Required}} else { - c.JSON(http.StatusBadRequest, gin.H{"msg": "Query argument {{.ParamName}} is required, but not found"}) + siw.ErrorHandler(c, fmt.Errorf("Query argument {{.ParamName}} is required, but not found"), http.StatusBadRequest) return } {{- end}}