Skip to content

Commit e265423

Browse files
committed
Fix 404 handling
Since Context is the top level context of gocraft/web, setUpContext middleware is called before NotFoundHandler is invoked. So, helper methods of Context can be used from NotFoundHandler.
1 parent 123d874 commit e265423

File tree

1 file changed

+2
-11
lines changed

1 file changed

+2
-11
lines changed

context.go

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -108,17 +108,8 @@ func (c *Context) setUpContext(rw web.ResponseWriter, req *web.Request, next web
108108

109109
// NotFoundHandler handles 404.
110110
func (c *Context) NotFoundHandler(rw web.ResponseWriter, req *web.Request) {
111-
c.logger.WithFields(logrus.Fields{
112-
"method": req.Method,
113-
"uri": req.URL.RequestURI(),
114-
"status": http.StatusNotFound,
115-
}).Error("The request URL not found")
116-
117-
rw.Header().Set("Content-Type", "application/json")
118-
rw.WriteHeader(http.StatusNotFound)
119-
if _, err := rw.Write([]byte(`{"error":{"code":"J0001","message":"The request URL was not found."}}`)); err != nil {
120-
c.logger.WithField("err", err).Error("Cannot write a 404 response")
121-
}
111+
c.RenderError(NewError(requestURLNotFoundErrorCode, "The request URL was not found.",
112+
http.StatusNotFound, nil))
122113
}
123114

124115
// Log returns the logger having meta information.

0 commit comments

Comments
 (0)