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
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()
}

View File

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

View File

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

View File

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