Skip to content

Commit 4ff449e

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 e8f5c24 commit 4ff449e

File tree

1 file changed

+20
-24
lines changed

1 file changed

+20
-24
lines changed

internal/reader/fetcher/response_handler.go

Lines changed: 20 additions & 24 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
}
@@ -118,12 +116,7 @@ func (r *ResponseHandler) IsModified(lastEtagValue, lastModifiedValue string) bo
118116
}
119117

120118
func (r *ResponseHandler) IsRedirect() bool {
121-
return r.httpResponse != nil &&
122-
(r.httpResponse.StatusCode == http.StatusMovedPermanently ||
123-
r.httpResponse.StatusCode == http.StatusFound ||
124-
r.httpResponse.StatusCode == http.StatusSeeOther ||
125-
r.httpResponse.StatusCode == http.StatusTemporaryRedirect ||
126-
r.httpResponse.StatusCode == http.StatusPermanentRedirect)
119+
return r.httpResponse != nil && (r.httpResponse.StatusCode&300 != 0)
127120
}
128121

129122
func (r *ResponseHandler) Close() {
@@ -144,9 +137,9 @@ func (r *ResponseHandler) getReader(maxBodySize int64) io.ReadCloser {
144137
reader := r.httpResponse.Body
145138
switch contentEncoding {
146139
case "br":
147-
reader = NewBrotliReadCloser(r.httpResponse.Body)
140+
reader = NewBrotliReadCloser(reader)
148141
case "gzip":
149-
reader = NewGzipReadCloser(r.httpResponse.Body)
142+
reader = NewGzipReadCloser(reader)
150143
}
151144
return http.MaxBytesReader(nil, reader, maxBodySize)
152145
}
@@ -176,17 +169,18 @@ func (r *ResponseHandler) ReadBody(maxBodySize int64) ([]byte, *locale.Localized
176169

177170
func (r *ResponseHandler) LocalizedError() *locale.LocalizedErrorWrapper {
178171
if r.clientErr != nil {
172+
err := fmt.Errorf("fetcher: %w", r.clientErr)
179173
switch {
180174
case isSSLError(r.clientErr):
181-
return locale.NewLocalizedErrorWrapper(fmt.Errorf("fetcher: %w", r.clientErr), "error.tls_error", r.clientErr)
175+
return locale.NewLocalizedErrorWrapper(err, "error.tls_error", r.clientErr)
182176
case isNetworkError(r.clientErr):
183-
return locale.NewLocalizedErrorWrapper(fmt.Errorf("fetcher: %w", r.clientErr), "error.network_operation", r.clientErr)
177+
return locale.NewLocalizedErrorWrapper(err, "error.network_operation", r.clientErr)
184178
case os.IsTimeout(r.clientErr):
185-
return locale.NewLocalizedErrorWrapper(fmt.Errorf("fetcher: %w", r.clientErr), "error.network_timeout", r.clientErr)
179+
return locale.NewLocalizedErrorWrapper(err, "error.network_timeout", r.clientErr)
186180
case errors.Is(r.clientErr, io.EOF):
187-
return locale.NewLocalizedErrorWrapper(fmt.Errorf("fetcher: %w", r.clientErr), "error.http_empty_response")
181+
return locale.NewLocalizedErrorWrapper(err, "error.http_empty_response")
188182
default:
189-
return locale.NewLocalizedErrorWrapper(fmt.Errorf("fetcher: %w", r.clientErr), "error.http_client_error", r.clientErr)
183+
return locale.NewLocalizedErrorWrapper(err, "error.http_client_error", r.clientErr)
190184
}
191185
}
192186

@@ -197,16 +191,18 @@ func (r *ResponseHandler) LocalizedError() *locale.LocalizedErrorWrapper {
197191
return locale.NewLocalizedErrorWrapper(errors.New("fetcher: access forbidden (403 status code)"), "error.http_forbidden")
198192
case http.StatusTooManyRequests:
199193
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")
194+
case http.StatusNotFound:
195+
return locale.NewLocalizedErrorWrapper(errors.New("fetcher: resource not found (404 status code)"), "error.http_resource_not_found")
196+
case http.StatusGone:
197+
return locale.NewLocalizedErrorWrapper(errors.New("fetcher: resource not found (410 status code)"), "error.http_resource_not_found")
202198
case http.StatusInternalServerError:
203-
return locale.NewLocalizedErrorWrapper(fmt.Errorf("fetcher: remote server error (%d status code)", r.httpResponse.StatusCode), "error.http_internal_server_error")
199+
return locale.NewLocalizedErrorWrapper(errors.New("fetcher: remote server error (500 status code)"), "error.http_internal_server_error")
204200
case http.StatusBadGateway:
205-
return locale.NewLocalizedErrorWrapper(fmt.Errorf("fetcher: bad gateway (%d status code)", r.httpResponse.StatusCode), "error.http_bad_gateway")
201+
return locale.NewLocalizedErrorWrapper(errors.New("fetcher: bad gateway (502 status code)"), "error.http_bad_gateway")
206202
case http.StatusServiceUnavailable:
207-
return locale.NewLocalizedErrorWrapper(fmt.Errorf("fetcher: service unavailable (%d status code)", r.httpResponse.StatusCode), "error.http_service_unavailable")
203+
return locale.NewLocalizedErrorWrapper(errors.New("fetcher: service unavailable (503 status code)"), "error.http_service_unavailable")
208204
case http.StatusGatewayTimeout:
209-
return locale.NewLocalizedErrorWrapper(fmt.Errorf("fetcher: gateway timeout (%d status code)", r.httpResponse.StatusCode), "error.http_gateway_timeout")
205+
return locale.NewLocalizedErrorWrapper(errors.New("fetcher: gateway timeout (504 status code)"), "error.http_gateway_timeout")
210206
}
211207

212208
if r.httpResponse.StatusCode >= 400 {

0 commit comments

Comments
 (0)