From 7260aa0cd70195a4679ca3c94204d9e618f947f2 Mon Sep 17 00:00:00 2001 From: jwijenbergh Date: Wed, 19 Oct 2022 16:51:48 +0200 Subject: 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 --- internal/http/http.go | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) (limited to 'internal/http') 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 -- cgit v1.2.3