gin gonic中间件,如何获取链中下一个处理程序的失败/成功状态

huangapple go评论97阅读模式
英文:

gin gonic middleware, how to get failure/success status for next handler in chain

问题

我正在编写一个中间件,使用gin gonic golang框架。我想在我的中间件中知道如果调用下一个处理程序失败,并根据此采取行动。

  1. func foo() gin.HandlerFunc {
  2. //做一些事情..
  3. c.Next()
  4. //根据c.Next()的结果做一些事情
  5. }

我该如何做到这一点?Next的文档没有提供太多信息:https://godoc.org/github.com/gin-gonic/gin#Context.Next

你能给出一些建议吗?我基本上想检查不存在的API端点,例如,如果有人输入一个我们没有任何API端点的URL,我能在这里检测到吗?

英文:

I am writing a middleware, using gin gonic golang framework. I want to know within my middleware, if the call to next handler failed and take action based on that.

  1. func foo() gin.HandlerFunc {
  2. //do something..
  3. c.Next()
  4. //do something based on result of c.Next()
  5. }

How can I do that? Documentation for next doesnt give much information https://godoc.org/github.com/gin-gonic/gin#Context.Next

Can you offer any suggestions. I basically want to check for non-existing api endpoints, for example, someone enters a URL, for which we don't have any api endpoint, then can I detect it here.

答案1

得分: 4

我将回答我的问题,因为我找到了一种处理路由未找到/方法未找到等问题的方法。思路是在NoRoute/NoMethod处理程序中使用context.Error()在gin上下文中设置一个错误。这将在c.Errors的错误切片中添加一个错误,然后可以在您的中间件函数中稍后使用。

  1. func MyMiddleware() gin.HandlerFunc {
  2. return func(c *gin.Context) {
  3. // 在这里做一些操作
  4. c.Next()
  5. // 根据c中的错误做一些操作
  6. if c.Errors.Error() != nil {
  7. // 错误情况,退出
  8. return
  9. }
  10. // 否则继续正常处理
  11. }
  12. }
  1. engine.NoRoute(func(c *gin.Context) {
  2. c.JSON(404,
  3. gin.H{"error": gin.H{
  4. "key": "not allowed",
  5. "description": "not allowed",
  6. }})
  7. c.Error(errors.New("Failed to find route")) // 在这里设置错误
  8. })

希望对你有帮助。

英文:

I am going to answer my question since I found a way to handle this kind of issues with route not found / method not found etc. The idea is to set an error in the gin context using context.Error() from the NoRoute/NoMethod handlers. This adds an error to the slice of errors in the c.Errors, which can then later on be used inside your middleware function.

  1. func MyMiddleware() gin.HandlerFunc {
  2. return func(c *gin.Context) {
  3. //do something here
  4. c.Next()
  5. //do something based on error in c.
  6. if c.Errors.Error() != nil {
  7. //error case, get out
  8. return
  9. }
  10. //else continue regular processing.
  11. }
  12. }
  13. engine.NoRoute(func(c *gin.Context) {
  14. c.JSON(404,
  15. gin.H{"error": gin.H{
  16. "key": "not allowed",
  17. "description": "not allowed",
  18. }})
  19. c.Error(errors.New("Failed to find route")) //Set the error here.
  20. })

Hope it helps.

答案2

得分: 1

Engine 上有一个名为 NoRoute 的方法,你可以传递路由处理程序来处理 404 等情况。

英文:

There is a method on Engine called NoRoute that you can pass route handlers to handle 404's and such.

huangapple
  • 本文由 发表于 2015年12月12日 04:19:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/34231904.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定