diff options
| author | jwijenbergh <jeroenwijenbergh@protonmail.com> | 2022-05-02 14:34:35 +0200 |
|---|---|---|
| committer | jwijenbergh <jeroenwijenbergh@protonmail.com> | 2022-05-02 14:34:35 +0200 |
| commit | 466450f0c47bdc614e66326d90e5fc6fb56ae732 (patch) | |
| tree | a01518a58d50d2f8449d37dadecc40e35c9f1fe1 /internal/http.go | |
| parent | a2a8efdcaad3d9b1852b1367a7cd7e8c5860cecf (diff) | |
Refactor: Wrap most errors in a custom type
Diffstat (limited to 'internal/http.go')
| -rw-r--r-- | internal/http.go | 109 |
1 files changed, 60 insertions, 49 deletions
diff --git a/internal/http.go b/internal/http.go index 8ca8cb9..0b1eda4 100644 --- a/internal/http.go +++ b/internal/http.go @@ -9,52 +9,6 @@ import ( "strings" ) -type HTTPResourceError struct { - URL string - Err error -} - -func (e *HTTPResourceError) Error() string { - return fmt.Sprintf("failed obtaining HTTP resource %s with error %v", e.URL, e.Err) -} - -type HTTPStatusError struct { - URL string - Status int -} - -func (e *HTTPStatusError) Error() string { - return fmt.Sprintf("failed obtaining HTTP resource %s as it gave an unsuccesful status code %d", e.URL, e.Status) -} - -type HTTPReadError struct { - URL string - Err error -} - -func (e *HTTPReadError) Error() string { - return fmt.Sprintf("failed reading HTTP resource %s with error %v", e.URL, e.Err) -} - -type HTTPParseJsonError struct { - URL string - Body string - Err error -} - -func (e *HTTPParseJsonError) Error() string { - return fmt.Sprintf("failed parsing json %s for HTTP resource %s with error %v", e.Body, e.URL, e.Err) -} - -type HTTPRequestCreateError struct { - URL string - Err error -} - -func (e *HTTPRequestCreateError) Error() string { - return fmt.Sprintf("failed to create HTTP request with url %s and error %v", e.URL, e.Err) -} - type URLParameters map[string]string type HTTPOptionalParams struct { @@ -65,9 +19,9 @@ type HTTPOptionalParams struct { // Construct an URL including on parameters func HTTPConstructURL(baseURL string, parameters URLParameters) (string, error) { - url, err := url.Parse(baseURL) - if err != nil { - return "", err + url, parseErr := url.Parse(baseURL) + if parseErr != nil { + return "", &HTTPConstructURLError{URL: baseURL, Parameters: parameters, Err: parseErr} } q := url.Query() @@ -130,6 +84,7 @@ func HTTPMethodWithOpts(method string, url string, opts *HTTPOptionalParams) (ht // it already has the right error so so we don't wrap it further url, urlErr := httpOptionalURL(url, opts) if urlErr != nil { + // No further type wrapping is needed here return nil, nil, urlErr } @@ -170,3 +125,59 @@ func HTTPMethodWithOpts(method string, url string, opts *HTTPOptionalParams) (ht // Return the body in bytes and signal the status error if there was one return resp.Header, body, nil } + +type HTTPResourceError struct { + URL string + Err error +} + +func (e *HTTPResourceError) Error() string { + return fmt.Sprintf("failed obtaining HTTP resource: %s with error: %v", e.URL, e.Err) +} + +type HTTPStatusError struct { + URL string + Status int +} + +func (e *HTTPStatusError) Error() string { + return fmt.Sprintf("failed obtaining HTTP resource: %s as it gave an unsuccesful status code: %d", e.URL, e.Status) +} + +type HTTPReadError struct { + URL string + Err error +} + +func (e *HTTPReadError) Error() string { + return fmt.Sprintf("failed reading HTTP resource: %s with error: %v", e.URL, e.Err) +} + +type HTTPParseJsonError struct { + URL string + Body string + Err error +} + +func (e *HTTPParseJsonError) Error() string { + return fmt.Sprintf("failed parsing json %s for HTTP resource: %s with error: %v", e.Body, e.URL, e.Err) +} + +type HTTPRequestCreateError struct { + URL string + Err error +} + +func (e *HTTPRequestCreateError) Error() string { + return fmt.Sprintf("failed to create HTTP request with url: %s and error: %v", e.URL, e.Err) +} + +type HTTPConstructURLError struct { + URL string + Parameters URLParameters + Err error +} + +func (e *HTTPConstructURLError) Error() string { + return fmt.Sprintf("failed to construct url: %s including parameters: %v with error: %v", e.URL, e.Parameters, e.Err) +} |
