refactor(remote): extract the fetch error translation

The same policy — keep TaskfileNotSecureError, make everything else a
generic download failure — was spelled out at each of the three call
sites, where copies of a security rule tend to drift apart.
This commit is contained in:
Valentin Maerten
2026-08-23 13:18:05 +02:00
parent 25fc28927d
commit f1637f84b5
2 changed files with 12 additions and 12 deletions

View File

@@ -140,10 +140,7 @@ func (node *HTTPNode) ReadContext(ctx context.Context) ([]byte, error) {
if ctx.Err() != nil {
return nil, err
}
if notSecure, ok := errors.AsType[*errors.TaskfileNotSecureError](err); ok {
return nil, notSecure
}
return nil, errors.TaskfileFetchFailedError{URI: node.Location()}
return nil, taskfileFetchError(err, node.Location())
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {

View File

@@ -51,10 +51,7 @@ func RemoteExists(ctx context.Context, u url.URL, client *http.Client) (*url.URL
if ctx.Err() != nil {
return nil, fmt.Errorf("checking remote file: %w", ctx.Err())
}
if notSecure, ok := errors.AsType[*errors.TaskfileNotSecureError](err); ok {
return nil, notSecure
}
return nil, errors.TaskfileFetchFailedError{URI: u.Redacted()}
return nil, taskfileFetchError(err, u.Redacted())
}
defer resp.Body.Close()
@@ -83,10 +80,7 @@ func RemoteExists(ctx context.Context, u url.URL, client *http.Client) (*url.URL
// Try the alternative URL
resp, err = client.Do(req)
if err != nil {
if notSecure, ok := errors.AsType[*errors.TaskfileNotSecureError](err); ok {
return nil, notSecure
}
return nil, errors.TaskfileFetchFailedError{URI: u.Redacted()}
return nil, taskfileFetchError(err, u.Redacted())
}
defer resp.Body.Close()
@@ -98,3 +92,12 @@ func RemoteExists(ctx context.Context, u url.URL, client *http.Client) (*url.URL
return nil, errors.TaskfileNotFoundError{URI: u.Redacted(), Walk: false}
}
// taskfileFetchError preserves redirect-policy errors wrapped by http.Client.
// Other transport errors remain generic download failures.
func taskfileFetchError(err error, uri string) error {
if notSecure, ok := errors.AsType[*errors.TaskfileNotSecureError](err); ok {
return notSecure
}
return errors.TaskfileFetchFailedError{URI: uri}
}