router := gin.New() router.Use(middleware.ContextError()) router.GET("/some-route", func(c *gin.Context) { panic("oops something went wrong!") })
router := gin.New() router.Use(middleware.ContextError()) router.GET("/some-route", func(c *gin.Context) { err := someFunctionThatCouldReturnAnError() if err != nil { c.Error(err) return } // continue with normal request handling })In this example, we are using the Context Error middleware in conjunction with the Gin framework's error handling functionality. We define a new route that calls a function that could potentially return an error. If an error is returned, we set the error on the context and return it. This will allow the Context Error middleware to intercept the error and return an appropriate error response to the client. Package Library: The Context Error middleware is part of the "github.com/gogits/gogs/modules/middleware" package in the Gogs (Go Git Service) project.