ResolveResponseStatus returns the Response and HTTP status code that should be (or has been) sent for rw, given an optional error. This function is useful for middleware and handlers that need to figure out the HTTP status code to return based on the error that occurred or what was set in the respo
(rw http.ResponseWriter, err error)
| 64 | // - StatusCode(err) if non-zero |
| 65 | // - otherwise 500 Internal Server Error. |
| 66 | func ResolveResponseStatus(rw http.ResponseWriter, err error) (resp *Response, status int) { |
| 67 | resp, _ = UnwrapResponse(rw) |
| 68 | |
| 69 | // once committed (sent to the client), the wire status is fixed; err cannot change it. |
| 70 | if resp != nil && resp.Committed { |
| 71 | if resp.Status == 0 { |
| 72 | // unlikely path, but fall back to net/http implicit default if handler never calls WriteHeader |
| 73 | return resp, http.StatusOK |
| 74 | } |
| 75 | return resp, resp.Status |
| 76 | } |
| 77 | |
| 78 | // net/http implicit default if handler never calls WriteHeader. |
| 79 | status = http.StatusOK |
| 80 | |
| 81 | // suggested status written from middleware/handlers, if present. |
| 82 | if resp != nil && resp.Status != 0 { |
| 83 | status = resp.Status |
| 84 | } |
| 85 | |
| 86 | // error overrides suggested status (matches typical Echo error-handler semantics). |
| 87 | if err != nil { |
| 88 | if s := StatusCode(err); s != 0 { |
| 89 | status = s |
| 90 | } else { |
| 91 | status = http.StatusInternalServerError |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | return resp, status |
| 96 | } |
| 97 | |
| 98 | // NewHTTPError creates a new instance of HTTPError |
| 99 | func NewHTTPError(code int, message string) *HTTPError { |
searching dependent graphs…