Skip to content

Commit

Permalink
update http context handler
Browse files Browse the repository at this point in the history
  • Loading branch information
xgfone committed Apr 5, 2024
1 parent 78caa34 commit 35b527c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 61 deletions.
60 changes: 8 additions & 52 deletions http/reqresp/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,68 +20,23 @@ import (
"github.com/xgfone/go-apiserver/result"
)

func handleContextResult(c *Context) {
if c.ResponseWriter.WroteHeader() {
return
}

switch {
case result.Respond != nil:
result.Respond(c, result.Err(c.Err))

case c.Err == nil:
c.WriteHeader(200)

case !respondError(c, c.Err):
c.Text(500, c.Err.Error())
}
}

func respondError(c *Context, err error) (ok bool) {
switch e := err.(type) {
case http.Handler:
e.ServeHTTP(c.ResponseWriter, c.Request)
ok = true

case interface{ Unwrap() error }:
if err = e.Unwrap(); err != nil {
ok = respondError(c, err)
}

case interface{ Unwrap() []error }:
for _, err := range e.Unwrap() {
if ok = respondError(c, err); ok {
break
}
}
}
return
}

// Handler is a handler based on Context to handle the http request.
type Handler func(c *Context)

// ServeHTTP implements the interface http.Handler.
func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
c := GetContext(r.Context())
if c == nil {
c = AcquireContext()
defer ReleaseContext(c)

c.Request = r.WithContext(SetContext(r.Context(), c))
c.ResponseWriter = AcquireResponseWriter(w)
defer ReleaseResponseWriter(c.ResponseWriter)
}

h(c)
handleContextResult(c)
runhandler(w, r, h)
}

// HandlerWithError is a handler to handle the http request with the error.
type HandlerWithError func(c *Context) error

// ServeHTTP implements the interface http.Handler.
func (h HandlerWithError) ServeHTTP(w http.ResponseWriter, r *http.Request) {
runhandler(w, r, func(c *Context) { c.AppendError(h(c)) })
}

func runhandler(w http.ResponseWriter, r *http.Request, f Handler) {
c := GetContext(r.Context())
if c == nil {
c = AcquireContext()
Expand All @@ -92,6 +47,7 @@ func (h HandlerWithError) ServeHTTP(w http.ResponseWriter, r *http.Request) {
defer ReleaseResponseWriter(c.ResponseWriter)
}

c.AppendError(h(c))
handleContextResult(c)
if f(c); !c.ResponseWriter.WroteHeader() {
result.Err(c.Err).Respond(c)
}
}
9 changes: 0 additions & 9 deletions http/reqresp/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,6 @@ func TestHandlerWithError(t *testing.T) {
t.Errorf("expect status code %d, but got %d", 400, rec.Code)
}

rec = httptest.NewRecorder()
resetContextResponse(rec)
reqresp.HandlerWithError(func(c *reqresp.Context) error {
return errors.Join(statuscode.ErrUnsupportedMediaType, statuscode.ErrBadRequest)
}).ServeHTTP(c.ResponseWriter, c.Request)
if rec.Code != 415 {
t.Errorf("expect status code %d, but got %d", 415, rec.Code)
}

_respond := result.Respond
defer func() { result.Respond = _respond }()
result.Respond = func(responder any, _ result.Response) {
Expand Down

0 comments on commit 35b527c

Please sign in to comment.