From 153a9768db2c0a9a2656e7cb0f5e9a21b0d03101 Mon Sep 17 00:00:00 2001 From: crusader Date: Fri, 6 Apr 2018 14:50:20 +0900 Subject: [PATCH] ing --- web/error.go | 26 +++++++++++++++++--------- web/fasthttp/server-handler.go | 25 +++++++++++++++++-------- web/fasthttp/server.go | 4 ++-- web/fasthttp/servlet.go | 4 ++-- 4 files changed, 38 insertions(+), 21 deletions(-) diff --git a/web/error.go b/web/error.go index b7c77bc..592c157 100644 --- a/web/error.go +++ b/web/error.go @@ -1,17 +1,25 @@ package web -func NewWebError(code int, message string) *WebError { - return &WebError{ - Code: code, - Message: message, +import ( + cuc "git.loafle.net/commons/util-go/context" +) + +const ( + ErrorKey = cuc.ContextKey("ErrorKey") +) + +func NewError(code int, cause error) *Error { + return &Error{ + Code: code, + Cause: cause, } } -type WebError struct { - Code int - Message string +type Error struct { + Code int + Cause error } -func (e *WebError) Error() string { - return e.Message +func (e *Error) Error() string { + return e.Cause.Error() } diff --git a/web/fasthttp/server-handler.go b/web/fasthttp/server-handler.go index 7b17025..37d8986 100644 --- a/web/fasthttp/server-handler.go +++ b/web/fasthttp/server-handler.go @@ -14,7 +14,7 @@ import ( type ServerHandler interface { web.ServerHandler - OnError(serverCtx server.ServerCtx, ctx *fasthttp.RequestCtx, err *web.WebError) + OnError(serverCtx server.ServerCtx, ctx *fasthttp.RequestCtx, err *web.Error) RegisterServlet(path string, servlet Servlet) Servlet(serverCtx server.ServerCtx, ctx *fasthttp.RequestCtx) Servlet @@ -25,7 +25,7 @@ type ServerHandler interface { type ServerHandlers struct { web.ServerHandlers - NotFoundServelt Servlet + ErrorServelt Servlet // path = context only. // ex) /auth => /auth, /auth/member => /auth @@ -58,8 +58,15 @@ func (sh *ServerHandlers) Destroy(serverCtx server.ServerCtx) { sh.ServerHandlers.Destroy(serverCtx) } -func (sh *ServerHandlers) OnError(serverCtx server.ServerCtx, ctx *fasthttp.RequestCtx, err *web.WebError) { - ctx.Error(err.Message, err.Code) +func (sh *ServerHandlers) OnError(serverCtx server.ServerCtx, ctx *fasthttp.RequestCtx, err *web.Error) { + if nil != sh.ErrorServelt { + servletCtx := sh.ErrorServelt.ServletCtx(serverCtx) + servletCtx.SetAttribute(web.ErrorKey, err) + sh.ErrorServelt.Handle(servletCtx, ctx) + return + } + + ctx.Error(err.Cause.Error(), err.Code) } func (sh *ServerHandlers) RegisterServlet(contextPath string, servlet Servlet) { @@ -70,15 +77,17 @@ func (sh *ServerHandlers) RegisterServlet(contextPath string, servlet Servlet) { } func (sh *ServerHandlers) Servlet(serverCtx server.ServerCtx, ctx *fasthttp.RequestCtx) Servlet { - contextPath, err := getContextPath(string(ctx.Path())) + path := string(ctx.Path()) + contextPath, err := getContextPath(path) if nil != err { - logging.Logger().Warnf("%v", err) - return sh.NotFoundServelt + logging.Logger().Warnf("Bad Request %v", err) + return nil } var servlet Servlet if servlet = sh.servlets[contextPath]; nil == servlet { - return sh.NotFoundServelt + logging.Logger().Warnf("Servlet is not exist for url[%s]", path) + return nil } return servlet diff --git a/web/fasthttp/server.go b/web/fasthttp/server.go index 75c207e..9d7ce59 100644 --- a/web/fasthttp/server.go +++ b/web/fasthttp/server.go @@ -139,7 +139,7 @@ func (s *Server) httpHandler(ctx *fasthttp.RequestCtx) { ) if servlet = s.ServerHandler.Servlet(s.ctx, ctx); nil == servlet { - s.onError(ctx, web.NewWebError(fasthttp.StatusNotFound, "Not Found")) + s.onError(ctx, web.NewError(fasthttp.StatusNotFound, fmt.Errorf("Not Found"))) return } servletCtx := servlet.ServletCtx(s.ctx) @@ -149,6 +149,6 @@ func (s *Server) httpHandler(ctx *fasthttp.RequestCtx) { } } -func (s *Server) onError(ctx *fasthttp.RequestCtx, err *web.WebError) { +func (s *Server) onError(ctx *fasthttp.RequestCtx, err *web.Error) { s.ServerHandler.OnError(s.ctx, ctx, err) } diff --git a/web/fasthttp/servlet.go b/web/fasthttp/servlet.go index 33eb703..720b9d3 100644 --- a/web/fasthttp/servlet.go +++ b/web/fasthttp/servlet.go @@ -9,7 +9,7 @@ import ( type Servlet interface { web.Servlet - Handle(servletCtx server.ServletCtx, ctx *fasthttp.RequestCtx) *web.WebError + Handle(servletCtx server.ServletCtx, ctx *fasthttp.RequestCtx) *web.Error } type Servlets struct { @@ -28,6 +28,6 @@ func (s *Servlets) Destroy(serverCtx server.ServerCtx) { // } -func (s *Servlets) Handle(servletCtx server.ServletCtx, ctx *fasthttp.RequestCtx) *web.WebError { +func (s *Servlets) Handle(servletCtx server.ServletCtx, ctx *fasthttp.RequestCtx) *web.Error { return nil }