diff options
| author | jwijenbergh <jeroenwijenbergh@protonmail.com> | 2022-10-19 16:51:48 +0200 |
|---|---|---|
| committer | jwijenbergh <jeroenwijenbergh@protonmail.com> | 2022-10-19 17:05:59 +0200 |
| commit | 7260aa0cd70195a4679ca3c94204d9e618f947f2 (patch) | |
| tree | 9321f5f3d21b06d1ab6dd50420879bc5ea41f044 /internal/http | |
| parent | f1a265190d8fd862bfff680fd0937a7f99759955 (diff) | |
Refactor: Make errors use the parent's error level
- All wrapped errors have to be created with types.NewWrappedError to
inherit the error level from the parent
- Or types.NewWrappedErrorLevel can be used which means a custom error
level is given. For example this is done with cancelling OAuth
- Client public errors are forwarded with handleError that also logs
it with the error's level
Diffstat (limited to 'internal/http')
| -rw-r--r-- | internal/http/http.go | 24 |
1 files changed, 12 insertions, 12 deletions
diff --git a/internal/http/http.go b/internal/http/http.go index 02d83a6..6ff853b 100644 --- a/internal/http/http.go +++ b/internal/http/http.go @@ -25,14 +25,14 @@ type HTTPOptionalParams struct { func HTTPConstructURL(baseURL string, parameters URLParameters) (string, error) { url, parseErr := url.Parse(baseURL) if parseErr != nil { - return "", &types.WrappedErrorMessage{ - Message: fmt.Sprintf( + return "", types.NewWrappedError( + fmt.Sprintf( "failed to construct url: %s including parameters: %v", url, parameters, ), - Err: parseErr, - } + parseErr, + ) } q := url.Query() @@ -66,10 +66,10 @@ func httpOptionalURL(url string, opts *HTTPOptionalParams) (string, error) { url, urlErr := HTTPConstructURL(url, opts.URLParameters) if urlErr != nil { - return url, &types.WrappedErrorMessage{ - Message: fmt.Sprintf("failed to create HTTP request with url: %s", url), - Err: urlErr, - } + return url, types.NewWrappedError( + fmt.Sprintf("failed to create HTTP request with url: %s", url), + urlErr, + ) } return url, nil } @@ -121,7 +121,7 @@ func HTTPMethodWithOpts( // Create request object with the body reader generated from the optional arguments req, reqErr := http.NewRequest(method, url, httpOptionalBodyReader(opts)) if reqErr != nil { - return nil, nil, &types.WrappedErrorMessage{Message: errorMessage, Err: reqErr} + return nil, nil, types.NewWrappedError(errorMessage, reqErr) } // See https://stackoverflow.com/questions/17714494/golang-http-request-results-in-eof-errors-when-making-multiple-requests-successi @@ -133,7 +133,7 @@ func HTTPMethodWithOpts( // Do request resp, respErr := client.Do(req) if respErr != nil { - return nil, nil, &types.WrappedErrorMessage{Message: errorMessage, Err: respErr} + return nil, nil, types.NewWrappedError(errorMessage, respErr) } // Request successful, make sure body is closed at the end @@ -142,13 +142,13 @@ func HTTPMethodWithOpts( // Return a string body, readErr := ioutil.ReadAll(resp.Body) if readErr != nil { - return resp.Header, nil, &types.WrappedErrorMessage{Message: errorMessage, Err: readErr} + return resp.Header, nil, types.NewWrappedError(errorMessage, readErr) } if resp.StatusCode < 200 || resp.StatusCode > 299 { // We make this a custom error because we want to extract the status code later statusErr := &HTTPStatusError{URL: url, Body: string(body), Status: resp.StatusCode} - return resp.Header, body, &types.WrappedErrorMessage{Message: errorMessage, Err: statusErr} + return resp.Header, body, types.NewWrappedError(errorMessage, statusErr) } // Return the body in bytes and signal the status error if there was one |
