diff --git a/web/error.go b/web/error.go new file mode 100644 index 0000000..b7c77bc --- /dev/null +++ b/web/error.go @@ -0,0 +1,17 @@ +package web + +func NewWebError(code int, message string) *WebError { + return &WebError{ + Code: code, + Message: message, + } +} + +type WebError struct { + Code int + Message string +} + +func (e *WebError) Error() string { + return e.Message +} diff --git a/web/fasthttp/server-handler.go b/web/fasthttp/server-handler.go index 6f3f4d2..7b17025 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, status int, reason error) + OnError(serverCtx server.ServerCtx, ctx *fasthttp.RequestCtx, err *web.WebError) RegisterServlet(path string, servlet Servlet) Servlet(serverCtx server.ServerCtx, ctx *fasthttp.RequestCtx) Servlet @@ -58,8 +58,8 @@ func (sh *ServerHandlers) Destroy(serverCtx server.ServerCtx) { sh.ServerHandlers.Destroy(serverCtx) } -func (sh *ServerHandlers) OnError(serverCtx server.ServerCtx, ctx *fasthttp.RequestCtx, status int, reason error) { - ctx.Error(reason.Error(), status) +func (sh *ServerHandlers) OnError(serverCtx server.ServerCtx, ctx *fasthttp.RequestCtx, err *web.WebError) { + ctx.Error(err.Message, err.Code) } func (sh *ServerHandlers) RegisterServlet(contextPath string, servlet Servlet) { diff --git a/web/fasthttp/server.go b/web/fasthttp/server.go index d85f4fe..75c207e 100644 --- a/web/fasthttp/server.go +++ b/web/fasthttp/server.go @@ -9,6 +9,7 @@ import ( "git.loafle.net/commons/logging-go" "git.loafle.net/commons/server-go" + "git.loafle.net/commons/server-go/web" "github.com/valyala/fasthttp" ) @@ -138,14 +139,16 @@ func (s *Server) httpHandler(ctx *fasthttp.RequestCtx) { ) if servlet = s.ServerHandler.Servlet(s.ctx, ctx); nil == servlet { - s.onError(ctx, fasthttp.StatusNotFound, fmt.Errorf("Not Found")) + s.onError(ctx, web.NewWebError(fasthttp.StatusNotFound, "Not Found")) return } servletCtx := servlet.ServletCtx(s.ctx) - servlet.Handle(servletCtx, ctx) + if err := servlet.Handle(servletCtx, ctx); nil != err { + s.onError(ctx, err) + } } -func (s *Server) onError(ctx *fasthttp.RequestCtx, status int, reason error) { - s.ServerHandler.OnError(s.ctx, ctx, status, reason) +func (s *Server) onError(ctx *fasthttp.RequestCtx, err *web.WebError) { + s.ServerHandler.OnError(s.ctx, ctx, err) } diff --git a/web/fasthttp/servlet.go b/web/fasthttp/servlet.go index 3a9271f..33eb703 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) + Handle(servletCtx server.ServletCtx, ctx *fasthttp.RequestCtx) *web.WebError } type Servlets struct { @@ -28,6 +28,6 @@ func (s *Servlets) Destroy(serverCtx server.ServerCtx) { // } -func (s *Servlets) Handle(servletCtx server.ServletCtx, ctx *fasthttp.RequestCtx) { - +func (s *Servlets) Handle(servletCtx server.ServletCtx, ctx *fasthttp.RequestCtx) *web.WebError { + return nil }