Skip to content

Commit 19c48ee

Browse files
authored
Merge pull request moby#51423 from thaJeztah/cleanup_token_errs
daemon/containerd: cleanup registry error-handling
2 parents de45c2a + de11467 commit 19c48ee

File tree

1 file changed

+19
-18
lines changed

1 file changed

+19
-18
lines changed

daemon/containerd/registry_errors.go

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,37 +14,38 @@ import (
1414
)
1515

1616
func translateRegistryError(ctx context.Context, err error) error {
17+
if err == nil {
18+
// Nothing to do
19+
return nil
20+
}
21+
1722
// Check for registry specific error
1823
var derrs docker.Errors
1924
if !errors.As(err, &derrs) {
2025
var remoteErr remoteerrors.ErrUnexpectedStatus
26+
var derr docker.Error
2127
if errors.As(err, &remoteErr) {
2228
if jerr := json.Unmarshal(remoteErr.Body, &derrs); jerr != nil {
2329
log.G(ctx).WithError(derrs).Debug("unable to unmarshal registry error")
2430
return fmt.Errorf("%w: %w", cerrdefs.ErrUnknown, err)
2531
}
26-
if len(derrs) == 0 {
27-
if remoteErr.StatusCode == http.StatusUnauthorized || remoteErr.StatusCode == http.StatusForbidden {
28-
// Some registries or token servers may use an old deprecated error format
29-
// which only has a "details" field and not the OCI defined "errors" array.
30-
var tokenErr struct {
31-
Details string `json:"details"`
32-
}
33-
if jerr := json.Unmarshal(remoteErr.Body, &tokenErr); jerr == nil && tokenErr.Details != "" {
34-
if remoteErr.StatusCode == http.StatusUnauthorized {
35-
return cerrdefs.ErrUnauthenticated.WithMessage(fmt.Sprintf("%s - %s", docker.ErrorCodeUnauthorized.Message(), tokenErr.Details))
36-
}
37-
return cerrdefs.ErrPermissionDenied.WithMessage(fmt.Sprintf("%s - %s", docker.ErrorCodeDenied.Message(), tokenErr.Details))
32+
if len(derrs) == 0 && (remoteErr.StatusCode == http.StatusUnauthorized || remoteErr.StatusCode == http.StatusForbidden) {
33+
// Some registries or token servers may use an old deprecated error format
34+
// which only has a "details" field and not the OCI defined "errors" array.
35+
var tokenErr struct {
36+
Details string `json:"details"`
37+
}
38+
if jerr := json.Unmarshal(remoteErr.Body, &tokenErr); jerr == nil && tokenErr.Details != "" {
39+
if remoteErr.StatusCode == http.StatusUnauthorized {
40+
return cerrdefs.ErrUnauthenticated.WithMessage(fmt.Sprintf("%s - %s", docker.ErrorCodeUnauthorized.Message(), tokenErr.Details))
3841
}
42+
return cerrdefs.ErrPermissionDenied.WithMessage(fmt.Sprintf("%s - %s", docker.ErrorCodeDenied.Message(), tokenErr.Details))
3943
}
4044
}
45+
} else if errors.As(err, &derr) {
46+
derrs = append(derrs, derr)
4147
} else {
42-
var derr docker.Error
43-
if errors.As(err, &derr) {
44-
derrs = append(derrs, derr)
45-
} else {
46-
return err
47-
}
48+
return err
4849
}
4950
}
5051
var errs []error

0 commit comments

Comments
 (0)