Skip to content

Commit 972be3e

Browse files
committed
refactor(fetcher): minor refactoring
- Replace a strings.HasPrefix + strings.Cut with a call to strings.CutPrefix. - Instead of enumerating all possible redirection code, check if the code is 300-something. - Extract an error constructor outside of its current scope instead of duplicating it 5 times - Use constant strings for LocalizedError's return values where possible.
1 parent 14f3195 commit 972be3e

File tree

1 file changed

+19
-18
lines changed

1 file changed

+19
-18
lines changed

internal/reader/fetcher/response_handler.go

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,9 @@ func (r *ResponseHandler) Expires() time.Duration {
6868
func (r *ResponseHandler) CacheControlMaxAge() time.Duration {
6969
cacheControlHeaderValue := r.httpResponse.Header.Get("Cache-Control")
7070
if cacheControlHeaderValue != "" {
71-
for _, directive := range strings.Split(cacheControlHeaderValue, ",") {
72-
directive = strings.TrimSpace(directive)
73-
if strings.HasPrefix(directive, "max-age=") {
74-
maxAge, err := strconv.Atoi(strings.TrimPrefix(directive, "max-age="))
75-
if err == nil {
71+
for directive := range strings.SplitSeq(cacheControlHeaderValue, ",") {
72+
if after, ok := strings.CutPrefix(strings.TrimSpace(directive), "max-age="); ok {
73+
if maxAge, err := strconv.Atoi(after); err == nil {
7674
return time.Duration(maxAge) * time.Second
7775
}
7876
}
@@ -144,9 +142,9 @@ func (r *ResponseHandler) getReader(maxBodySize int64) io.ReadCloser {
144142
reader := r.httpResponse.Body
145143
switch contentEncoding {
146144
case "br":
147-
reader = NewBrotliReadCloser(r.httpResponse.Body)
145+
reader = NewBrotliReadCloser(reader)
148146
case "gzip":
149-
reader = NewGzipReadCloser(r.httpResponse.Body)
147+
reader = NewGzipReadCloser(reader)
150148
}
151149
return http.MaxBytesReader(nil, reader, maxBodySize)
152150
}
@@ -176,17 +174,18 @@ func (r *ResponseHandler) ReadBody(maxBodySize int64) ([]byte, *locale.Localized
176174

177175
func (r *ResponseHandler) LocalizedError() *locale.LocalizedErrorWrapper {
178176
if r.clientErr != nil {
177+
err := fmt.Errorf("fetcher: %w", r.clientErr)
179178
switch {
180179
case isSSLError(r.clientErr):
181-
return locale.NewLocalizedErrorWrapper(fmt.Errorf("fetcher: %w", r.clientErr), "error.tls_error", r.clientErr)
180+
return locale.NewLocalizedErrorWrapper(err, "error.tls_error", r.clientErr)
182181
case isNetworkError(r.clientErr):
183-
return locale.NewLocalizedErrorWrapper(fmt.Errorf("fetcher: %w", r.clientErr), "error.network_operation", r.clientErr)
182+
return locale.NewLocalizedErrorWrapper(err, "error.network_operation", r.clientErr)
184183
case os.IsTimeout(r.clientErr):
185-
return locale.NewLocalizedErrorWrapper(fmt.Errorf("fetcher: %w", r.clientErr), "error.network_timeout", r.clientErr)
184+
return locale.NewLocalizedErrorWrapper(err, "error.network_timeout", r.clientErr)
186185
case errors.Is(r.clientErr, io.EOF):
187-
return locale.NewLocalizedErrorWrapper(fmt.Errorf("fetcher: %w", r.clientErr), "error.http_empty_response")
186+
return locale.NewLocalizedErrorWrapper(err, "error.http_empty_response")
188187
default:
189-
return locale.NewLocalizedErrorWrapper(fmt.Errorf("fetcher: %w", r.clientErr), "error.http_client_error", r.clientErr)
188+
return locale.NewLocalizedErrorWrapper(err, "error.http_client_error", r.clientErr)
190189
}
191190
}
192191

@@ -197,16 +196,18 @@ func (r *ResponseHandler) LocalizedError() *locale.LocalizedErrorWrapper {
197196
return locale.NewLocalizedErrorWrapper(errors.New("fetcher: access forbidden (403 status code)"), "error.http_forbidden")
198197
case http.StatusTooManyRequests:
199198
return locale.NewLocalizedErrorWrapper(errors.New("fetcher: too many requests (429 status code)"), "error.http_too_many_requests")
200-
case http.StatusNotFound, http.StatusGone:
201-
return locale.NewLocalizedErrorWrapper(fmt.Errorf("fetcher: resource not found (%d status code)", r.httpResponse.StatusCode), "error.http_resource_not_found")
199+
case http.StatusNotFound:
200+
return locale.NewLocalizedErrorWrapper(errors.New("fetcher: resource not found (404 status code)"), "error.http_resource_not_found")
201+
case http.StatusGone:
202+
return locale.NewLocalizedErrorWrapper(errors.New("fetcher: resource not found (410 status code)"), "error.http_resource_not_found")
202203
case http.StatusInternalServerError:
203-
return locale.NewLocalizedErrorWrapper(fmt.Errorf("fetcher: remote server error (%d status code)", r.httpResponse.StatusCode), "error.http_internal_server_error")
204+
return locale.NewLocalizedErrorWrapper(errors.New("fetcher: remote server error (500 status code)"), "error.http_internal_server_error")
204205
case http.StatusBadGateway:
205-
return locale.NewLocalizedErrorWrapper(fmt.Errorf("fetcher: bad gateway (%d status code)", r.httpResponse.StatusCode), "error.http_bad_gateway")
206+
return locale.NewLocalizedErrorWrapper(errors.New("fetcher: bad gateway (502 status code)"), "error.http_bad_gateway")
206207
case http.StatusServiceUnavailable:
207-
return locale.NewLocalizedErrorWrapper(fmt.Errorf("fetcher: service unavailable (%d status code)", r.httpResponse.StatusCode), "error.http_service_unavailable")
208+
return locale.NewLocalizedErrorWrapper(errors.New("fetcher: service unavailable (503 status code)"), "error.http_service_unavailable")
208209
case http.StatusGatewayTimeout:
209-
return locale.NewLocalizedErrorWrapper(fmt.Errorf("fetcher: gateway timeout (%d status code)", r.httpResponse.StatusCode), "error.http_gateway_timeout")
210+
return locale.NewLocalizedErrorWrapper(errors.New("fetcher: gateway timeout (504 status code)"), "error.http_gateway_timeout")
210211
}
211212

212213
if r.httpResponse.StatusCode >= 400 {

0 commit comments

Comments
 (0)