This commit is contained in:
crusader 2018-04-06 14:50:20 +09:00
parent 8915cba78b
commit 153a9768db
4 changed files with 38 additions and 21 deletions

View File

@ -1,17 +1,25 @@
package web package web
func NewWebError(code int, message string) *WebError { import (
return &WebError{ cuc "git.loafle.net/commons/util-go/context"
)
const (
ErrorKey = cuc.ContextKey("ErrorKey")
)
func NewError(code int, cause error) *Error {
return &Error{
Code: code, Code: code,
Message: message, Cause: cause,
} }
} }
type WebError struct { type Error struct {
Code int Code int
Message string Cause error
} }
func (e *WebError) Error() string { func (e *Error) Error() string {
return e.Message return e.Cause.Error()
} }

View File

@ -14,7 +14,7 @@ import (
type ServerHandler interface { type ServerHandler interface {
web.ServerHandler 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) RegisterServlet(path string, servlet Servlet)
Servlet(serverCtx server.ServerCtx, ctx *fasthttp.RequestCtx) Servlet Servlet(serverCtx server.ServerCtx, ctx *fasthttp.RequestCtx) Servlet
@ -25,7 +25,7 @@ type ServerHandler interface {
type ServerHandlers struct { type ServerHandlers struct {
web.ServerHandlers web.ServerHandlers
NotFoundServelt Servlet ErrorServelt Servlet
// path = context only. // path = context only.
// ex) /auth => /auth, /auth/member => /auth // ex) /auth => /auth, /auth/member => /auth
@ -58,8 +58,15 @@ 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, err *web.WebError) { func (sh *ServerHandlers) OnError(serverCtx server.ServerCtx, ctx *fasthttp.RequestCtx, err *web.Error) {
ctx.Error(err.Message, err.Code) 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) { 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 { 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 { if nil != err {
logging.Logger().Warnf("%v", err) logging.Logger().Warnf("Bad Request %v", err)
return sh.NotFoundServelt return nil
} }
var servlet Servlet var servlet Servlet
if servlet = sh.servlets[contextPath]; nil == 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 return servlet

View File

@ -139,7 +139,7 @@ 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, web.NewWebError(fasthttp.StatusNotFound, "Not Found")) s.onError(ctx, web.NewError(fasthttp.StatusNotFound, fmt.Errorf("Not Found")))
return return
} }
servletCtx := servlet.ServletCtx(s.ctx) 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) 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) *web.WebError Handle(servletCtx server.ServletCtx, ctx *fasthttp.RequestCtx) *web.Error
} }
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) *web.WebError { func (s *Servlets) Handle(servletCtx server.ServletCtx, ctx *fasthttp.RequestCtx) *web.Error {
return nil return nil
} }