Skip to content

Commit

Permalink
[Middleware] Add StatusCodeFromHTTPServeResult() method to ResponseWr…
Browse files Browse the repository at this point in the history
…iter middleware. (#12)
  • Loading branch information
jkuma committed May 26, 2023
1 parent bc0db73 commit 23334e1
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions middleware/response_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (m ResponseWriter) Wrap(h handler.Handler) handler.Handler {
return handler.Func(func(w http.ResponseWriter, r *http.Request) (interface{}, error) {
resp, err := h.Serve(w, r)

m.writeStatusCode(w, resp, err)
w.WriteHeader(m.StatusCodeFromHTTPServeResult(resp, err))

var errW error
if err != nil {
Expand All @@ -94,26 +94,25 @@ func (m ResponseWriter) Wrap(h handler.Handler) handler.Handler {
})
}

func (m ResponseWriter) writeStatusCode(w http.ResponseWriter, resp interface{}, err error) {
// StatusCodeFromHTTPServeResult returns the http status code from http.Serve result.
func (m ResponseWriter) StatusCodeFromHTTPServeResult(resp interface{}, err error) int {
if err != nil {
if errWithStatus, ok := err.(WithStatusCode); ok {
w.WriteHeader(errWithStatus.StatusCode())
return
return errWithStatus.StatusCode()
}

w.WriteHeader(http.StatusInternalServerError)
return
return http.StatusInternalServerError
}

if resp == nil {
w.WriteHeader(http.StatusNoContent)
return
return http.StatusNoContent
}

if respWithStatus, ok := err.(WithStatusCode); ok {
w.WriteHeader(respWithStatus.StatusCode())
return
return respWithStatus.StatusCode()
}

return http.StatusOK
}

func (m ResponseWriter) writeResponse(w http.ResponseWriter, r *http.Request, resp interface{}) error {
Expand Down

0 comments on commit 23334e1

Please sign in to comment.