Add not found handler
This commit is contained in:
parent
74d80765e5
commit
c20e999b70
|
|
@ -0,0 +1,18 @@
|
|||
package error
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// NotFound creates a gin middleware for handling page not found.
|
||||
func NotFound() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
c.JSON(http.StatusNotFound, &errorWrapper{
|
||||
Error: http.StatusText(http.StatusNotFound),
|
||||
ErrorCode: http.StatusNotFound,
|
||||
ErrorDescription: "page not found",
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
package error
|
||||
|
||||
import (
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func TestNotFound(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
rec := httptest.NewRecorder()
|
||||
ctx, _ := gin.CreateTestContext(rec)
|
||||
|
||||
NotFound()(ctx)
|
||||
|
||||
assertJSONResponse(t, rec, 404, `{"errorCode":404, "errorDescription":"page not found", "error":"Not Found"}`)
|
||||
}
|
||||
|
|
@ -21,6 +21,7 @@ func Create(db *database.GormDatabase) (*gin.Engine, func()) {
|
|||
|
||||
g := gin.New()
|
||||
g.Use(gin.Logger(), gin.Recovery(), error.Handler())
|
||||
g.NoRoute(error.NotFound())
|
||||
|
||||
g.GET("/")
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue