This commit is contained in:
crusader 2018-04-06 13:06:05 +09:00
parent aa66a49925
commit 8915cba78b
4 changed files with 30 additions and 10 deletions

17
web/error.go Normal file
View File

@ -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
}

View File

@ -14,7 +14,7 @@ import (
type ServerHandler interface { type ServerHandler interface {
web.ServerHandler 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) RegisterServlet(path string, servlet Servlet)
Servlet(serverCtx server.ServerCtx, ctx *fasthttp.RequestCtx) Servlet Servlet(serverCtx server.ServerCtx, ctx *fasthttp.RequestCtx) Servlet
@ -58,8 +58,8 @@ func (sh *ServerHandlers) Destroy(serverCtx server.ServerCtx) {
sh.ServerHandlers.Destroy(serverCtx) sh.ServerHandlers.Destroy(serverCtx)
} }
func (sh *ServerHandlers) OnError(serverCtx server.ServerCtx, ctx *fasthttp.RequestCtx, status int, reason error) { func (sh *ServerHandlers) OnError(serverCtx server.ServerCtx, ctx *fasthttp.RequestCtx, err *web.WebError) {
ctx.Error(reason.Error(), status) ctx.Error(err.Message, err.Code)
} }
func (sh *ServerHandlers) RegisterServlet(contextPath string, servlet Servlet) { func (sh *ServerHandlers) RegisterServlet(contextPath string, servlet Servlet) {

View File

@ -9,6 +9,7 @@ import (
"git.loafle.net/commons/logging-go" "git.loafle.net/commons/logging-go"
"git.loafle.net/commons/server-go" "git.loafle.net/commons/server-go"
"git.loafle.net/commons/server-go/web"
"github.com/valyala/fasthttp" "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 { 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 return
} }
servletCtx := servlet.ServletCtx(s.ctx) 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) { func (s *Server) onError(ctx *fasthttp.RequestCtx, err *web.WebError) {
s.ServerHandler.OnError(s.ctx, ctx, status, reason) s.ServerHandler.OnError(s.ctx, ctx, err)
} }

View File

@ -9,7 +9,7 @@ import (
type Servlet interface { type Servlet interface {
web.Servlet web.Servlet
Handle(servletCtx server.ServletCtx, ctx *fasthttp.RequestCtx) Handle(servletCtx server.ServletCtx, ctx *fasthttp.RequestCtx) *web.WebError
} }
type Servlets struct { 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
} }