Skip to content

Commit

Permalink
gin server custom error handler option (#587)
Browse files Browse the repository at this point in the history
* gin server custom error handler option

* removed bool return

* ran make gen

* Fix various issues from rebasing against master

This pull request has been in the queue for a long time, and
diverged a lot. Fix various issues related to that.

Co-authored-by: marcinromaszewicz <marcinromaszewicz@deepmap.ai>
  • Loading branch information
chaseisabelle and marcinromaszewicz committed Oct 27, 2022
1 parent 40503ed commit d74c88f
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 87 deletions.
24 changes: 18 additions & 6 deletions examples/petstore-expanded/gin/api/petstore-server.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

104 changes: 57 additions & 47 deletions internal/test/server/server_moq.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 19 additions & 7 deletions internal/test/strict-server/gin/server.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 20 additions & 9 deletions pkg/codegen/templates/gin/gin-register.tmpl
Expand Up @@ -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.
Expand All @@ -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
}

0 comments on commit d74c88f

Please sign in to comment.