httpError handles better formatted custom errors.
(rw http.ResponseWriter, defaultCode int, err error)
| 1743 | |
| 1744 | // httpError handles better formatted custom errors. |
| 1745 | func httpError(rw http.ResponseWriter, defaultCode int, err error) { |
| 1746 | status := defaultCode |
| 1747 | |
| 1748 | var statusErr statusHookError |
| 1749 | if errors.As(err, &statusErr) { |
| 1750 | status = statusErr.HTTPStatusCode |
| 1751 | } |
| 1752 | |
| 1753 | var oauthErr *oauth2.RetrieveError |
| 1754 | if errors.As(err, &oauthErr) { |
| 1755 | if oauthErr.Response.StatusCode != 0 { |
| 1756 | status = oauthErr.Response.StatusCode |
| 1757 | } |
| 1758 | |
| 1759 | rw.Header().Set("Content-Type", "application/x-www-form-urlencoded; charset=utf-8") |
| 1760 | form := url.Values{ |
| 1761 | "error": {oauthErr.ErrorCode}, |
| 1762 | "error_description": {oauthErr.ErrorDescription}, |
| 1763 | "error_uri": {oauthErr.ErrorURI}, |
| 1764 | } |
| 1765 | rw.WriteHeader(status) |
| 1766 | _, _ = rw.Write([]byte(form.Encode())) |
| 1767 | return |
| 1768 | } |
| 1769 | |
| 1770 | http.Error(rw, err.Error(), status) |
| 1771 | } |
| 1772 | |
| 1773 | type fakeRoundTripper struct { |
| 1774 | roundTrip func(req *http.Request) (*http.Response, error) |
no test coverage detected