Echo: 404 Router

Created on 8 Oct 2016  路  4Comments  路  Source: labstack/echo

Description

How about make a new 404 router like Beego?

Most helpful comment

You Can also pass error URL Name. like
echo.NotFoundHandler = func(c echo.Context) error { user_input = c.Request().URL // http.URL msg = "not found page :" , user_input // render your 404 page return c.String(http.StatusNotFound, msg ) }

All 4 comments

@leoycx Please explain in detail!

e.GET("Error404", func(c echo.Context) error { return ... })

you never need to set up a router of not found error, it's a status of requested page

package main

import (
    "net/http"

    "github.com/labstack/echo"
    "github.com/labstack/echo/engine/standard"
)

func main() {

    echo.NotFoundHandler = func(c echo.Context) error {
        // render your 404 page
        return c.String(http.StatusNotFound, "not found page")
    }

    e := echo.New()

    e.GET("/something/:id", func(c echo.Context) error {
        if c.Param("id") == "5" {
            return echo.NotFoundHandler(c)
        }

        return c.String(http.StatusOK, c.Param("id"))
    })

    e.Run(standard.New(":8000"))
}

also see default error handler https://github.com/labstack/echo/blob/master/echo.go#L276, you can customize it setting own error handler using e.SetHTTPErrorHandler

You Can also pass error URL Name. like
echo.NotFoundHandler = func(c echo.Context) error { user_input = c.Request().URL // http.URL msg = "not found page :" , user_input // render your 404 page return c.String(http.StatusNotFound, msg ) }

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kyokomi picture kyokomi  路  3Comments

dre1080 picture dre1080  路  4Comments

absinsekt picture absinsekt  路  4Comments

arun0009 picture arun0009  路  3Comments

montanaflynn picture montanaflynn  路  3Comments