Echo: Can add NoRoute(h HandlerFunc, m ...MiddlewareFunc) to Group and Echo?

Created on 19 Feb 2017  Â·  4Comments  Â·  Source: labstack/echo

Description

Can add NoRoute(h HandlerFunc, m ...MiddlewareFunc) to Group and Echo?
There are NoRoute() in https://github.com/gin-gonic/gin.

NoRoute() usage:

package mai

func main() {
    engine := echo.New()
    engine.Use(middleware.Logger())
    engine.Use(middleware.Recover())
       engine.NoRoute(http.DefaultServeMux)


       q := engine.Group("/query")
       q.GET("/hello", func(ctx echo.Context) error {
           ctx.String(http.StatusOK "hi!")
       })
       q.NoRoute(func(ctx echo.Context) error {
           ctx.String(http.StatusNotFound, "not found with prefix is /query")
       })
}
enhancement question wontfix

Most helpful comment

NoRoute() have more advantage than ErrorHandler!

  1. NoRoute() can add middleware handler, ErrorHandler can't.
  2. NoRoute() more user-friendly。

NoRoute() example as follow:
````go
package mai

func main() {
engine := echo.New()
engine.Use(middleware.Logger())
engine.Use(middleware.Recover())
engine.NoRoute(http.DefaultServeMux)

   q := engine.Group("/query", authQuery) // authQueryis middleware handler,
   q.GET("/hello", func(ctx echo.Context) error {
       return ctx.String(http.StatusOK "hi!")
   })
   q.NoRoute(func(ctx echo.Context) error {
       return ctx.JSON(http.StatusOK, xxxx)
   }, auth) // auth is middleware


   cfg := engine.Group("/config", authConfig) // authConfig is middleware handler,
   cfg.GET("/hello", func(ctx echo.Context) error {
       return ctx.String(http.StatusOK "hi!")
   })
   cfg.NoRoute(func(ctx echo.Context) error {
       return  ctx.JSON(http.StatusOK, xxxx)
   })

}
````

ErrorHandler() example as follow:
````go
package mai

func main() {
engine := echo.New()
engine.Use(middleware.Logger())
engine.Use(middleware.Recover())

   q := engine.Group("/query", auth) // auth is middleware handler,
   q.GET("/hello", func(ctx echo.Context) error {
       return ctx.String(http.StatusOK "hi!")
   })
   cfg := engine.Group("/config", auth) // auth is middleware handler,
   cfg.GET("/hello", func(ctx echo.Context) error {
       return ctx.String(http.StatusOK "hi!")
   })


   engine.ErrorHandler = func(e error, ctx Context) error {
      if e == echo.ErrNotFound {
           if strings.Contains(ctx.Request().URL.Path, "/query") {

                //  hack! it isn't standard for run handler
                authQuery(ctx, func(ctx Context) {
                    return ctx.JSON(http.StatusOK, queryxxxx)
                } )
             return nil
           } else if strings.Contains(ctx.Request().URL.Path, "/config") {

                //  hack! it isn't standard for run handler
                authConfig(ctx, func(ctx Context) {
                    return ctx.JSON(http.StatusOK, configxxxx)
                } )
               return nil
           }
          http.DefaultServeMux.ServeHttp(ctx.Response(), ctx.Request())
          return nil
      }
  }

}
````

All 4 comments

What is the function of NoRoute? If you are looking for handling route not found requests look at https://echo.labstack.com/guide/error-handling. Echo#DefaultErrorHandler captures all the errors and it can also be customized.

NoRoute() have more advantage than ErrorHandler!

  1. NoRoute() can add middleware handler, ErrorHandler can't.
  2. NoRoute() more user-friendly。

NoRoute() example as follow:
````go
package mai

func main() {
engine := echo.New()
engine.Use(middleware.Logger())
engine.Use(middleware.Recover())
engine.NoRoute(http.DefaultServeMux)

   q := engine.Group("/query", authQuery) // authQueryis middleware handler,
   q.GET("/hello", func(ctx echo.Context) error {
       return ctx.String(http.StatusOK "hi!")
   })
   q.NoRoute(func(ctx echo.Context) error {
       return ctx.JSON(http.StatusOK, xxxx)
   }, auth) // auth is middleware


   cfg := engine.Group("/config", authConfig) // authConfig is middleware handler,
   cfg.GET("/hello", func(ctx echo.Context) error {
       return ctx.String(http.StatusOK "hi!")
   })
   cfg.NoRoute(func(ctx echo.Context) error {
       return  ctx.JSON(http.StatusOK, xxxx)
   })

}
````

ErrorHandler() example as follow:
````go
package mai

func main() {
engine := echo.New()
engine.Use(middleware.Logger())
engine.Use(middleware.Recover())

   q := engine.Group("/query", auth) // auth is middleware handler,
   q.GET("/hello", func(ctx echo.Context) error {
       return ctx.String(http.StatusOK "hi!")
   })
   cfg := engine.Group("/config", auth) // auth is middleware handler,
   cfg.GET("/hello", func(ctx echo.Context) error {
       return ctx.String(http.StatusOK "hi!")
   })


   engine.ErrorHandler = func(e error, ctx Context) error {
      if e == echo.ErrNotFound {
           if strings.Contains(ctx.Request().URL.Path, "/query") {

                //  hack! it isn't standard for run handler
                authQuery(ctx, func(ctx Context) {
                    return ctx.JSON(http.StatusOK, queryxxxx)
                } )
             return nil
           } else if strings.Contains(ctx.Request().URL.Path, "/config") {

                //  hack! it isn't standard for run handler
                authConfig(ctx, func(ctx Context) {
                    return ctx.JSON(http.StatusOK, configxxxx)
                } )
               return nil
           }
          http.DefaultServeMux.ServeHttp(ctx.Response(), ctx.Request())
          return nil
      }
  }

}
````

If you looking for example, you could see what gin does here http://godoc.org/github.com/gin-gonic/gin#Engine.NoRoute

It will return default 404 for un-registered routes 🙄

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

vishr picture vishr  Â·  3Comments

arun0009 picture arun0009  Â·  3Comments

mmindenhall picture mmindenhall  Â·  4Comments

wangxianzhuo picture wangxianzhuo  Â·  4Comments

absinsekt picture absinsekt  Â·  4Comments