Skip to content

Commit abe2755

Browse files
authored
Fix container registry error handling (#36021)
1. the `if` check in `handleCreateManifestResult` didn't handler err correctly 2. add more error details for debugging
1 parent 688430e commit abe2755

File tree

2 files changed

+28
-29
lines changed

2 files changed

+28
-29
lines changed

routers/api/packages/container/container.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -290,8 +290,8 @@ func PostBlobsUploads(ctx *context.Context) {
290290
Creator: ctx.Doer,
291291
},
292292
); err != nil {
293-
switch err {
294-
case packages_service.ErrQuotaTotalCount, packages_service.ErrQuotaTypeSize, packages_service.ErrQuotaTotalSize:
293+
switch {
294+
case errors.Is(err, packages_service.ErrQuotaTotalCount), errors.Is(err, packages_service.ErrQuotaTypeSize), errors.Is(err, packages_service.ErrQuotaTotalSize):
295295
apiError(ctx, http.StatusForbidden, err)
296296
default:
297297
apiError(ctx, http.StatusInternalServerError, err)
@@ -439,8 +439,8 @@ func PutBlobsUpload(ctx *context.Context) {
439439
Creator: ctx.Doer,
440440
},
441441
); err != nil {
442-
switch err {
443-
case packages_service.ErrQuotaTotalCount, packages_service.ErrQuotaTypeSize, packages_service.ErrQuotaTotalSize:
442+
switch {
443+
case errors.Is(err, packages_service.ErrQuotaTotalCount), errors.Is(err, packages_service.ErrQuotaTypeSize), errors.Is(err, packages_service.ErrQuotaTotalSize):
444444
apiError(ctx, http.StatusForbidden, err)
445445
default:
446446
apiError(ctx, http.StatusInternalServerError, err)
@@ -592,13 +592,10 @@ func PutManifest(ctx *context.Context) {
592592
apiErrorDefined(ctx, namedError)
593593
} else if errors.Is(err, container_model.ErrContainerBlobNotExist) {
594594
apiErrorDefined(ctx, errBlobUnknown)
595+
} else if errors.Is(err, packages_service.ErrQuotaTotalCount) || errors.Is(err, packages_service.ErrQuotaTypeSize) || errors.Is(err, packages_service.ErrQuotaTotalSize) {
596+
apiError(ctx, http.StatusForbidden, err)
595597
} else {
596-
switch err {
597-
case packages_service.ErrQuotaTotalCount, packages_service.ErrQuotaTypeSize, packages_service.ErrQuotaTotalSize:
598-
apiError(ctx, http.StatusForbidden, err)
599-
default:
600-
apiError(ctx, http.StatusInternalServerError, err)
601-
}
598+
apiError(ctx, http.StatusInternalServerError, err)
602599
}
603600
return
604601
}

routers/api/packages/container/manifest.go

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,11 @@ type processManifestTxRet struct {
8282
}
8383

8484
func handleCreateManifestResult(ctx context.Context, err error, mci *manifestCreationInfo, contentStore *packages_module.ContentStore, txRet *processManifestTxRet) (string, error) {
85-
if err != nil && txRet.created && txRet.pb != nil {
86-
if err := contentStore.Delete(packages_module.BlobHash256Key(txRet.pb.HashSHA256)); err != nil {
87-
log.Error("Error deleting package blob from content store: %v", err)
85+
if err != nil {
86+
if txRet.created && txRet.pb != nil {
87+
if err := contentStore.Delete(packages_module.BlobHash256Key(txRet.pb.HashSHA256)); err != nil {
88+
log.Error("Error deleting package blob from content store: %v", err)
89+
}
8890
}
8991
return "", err
9092
}
@@ -198,14 +200,14 @@ func processOciImageIndex(ctx context.Context, mci *manifestCreationInfo, buf *p
198200
if errors.Is(err, container_model.ErrContainerBlobNotExist) {
199201
return errManifestBlobUnknown
200202
}
201-
return err
203+
return fmt.Errorf("GetContainerBlob: %w", err)
202204
}
203205

204206
size, err := packages_model.CalculateFileSize(ctx, &packages_model.PackageFileSearchOptions{
205207
VersionID: pfd.File.VersionID,
206208
})
207209
if err != nil {
208-
return err
210+
return fmt.Errorf("CalculateFileSize: %w", err)
209211
}
210212

211213
metadata.Manifests = append(metadata.Manifests, &container_module.Manifest{
@@ -217,7 +219,7 @@ func processOciImageIndex(ctx context.Context, mci *manifestCreationInfo, buf *p
217219

218220
pv, err := createPackageAndVersion(ctx, mci, metadata)
219221
if err != nil {
220-
return err
222+
return fmt.Errorf("createPackageAndVersion: %w", err)
221223
}
222224

223225
txRet.pv = pv
@@ -240,23 +242,23 @@ func createPackageAndVersion(ctx context.Context, mci *manifestCreationInfo, met
240242
if p, err = packages_model.TryInsertPackage(ctx, p); err != nil {
241243
if !errors.Is(err, packages_model.ErrDuplicatePackage) {
242244
log.Error("Error inserting package: %v", err)
243-
return nil, err
245+
return nil, fmt.Errorf("TryInsertPackage: %w", err)
244246
}
245247
created = false
246248
}
247249

248250
if created {
249251
if _, err := packages_model.InsertProperty(ctx, packages_model.PropertyTypePackage, p.ID, container_module.PropertyRepository, strings.ToLower(mci.Owner.LowerName+"/"+mci.Image)); err != nil {
250252
log.Error("Error setting package property: %v", err)
251-
return nil, err
253+
return nil, fmt.Errorf("InsertProperty(PropertyRepository): %w", err)
252254
}
253255
}
254256

255257
metadata.IsTagged = mci.IsTagged
256258

257259
metadataJSON, err := json.Marshal(metadata)
258260
if err != nil {
259-
return nil, err
261+
return nil, fmt.Errorf("json.Marshal(metadata): %w", err)
260262
}
261263

262264
// "docker buildx imagetools create" multi-arch operations:
@@ -276,43 +278,43 @@ func createPackageAndVersion(ctx context.Context, mci *manifestCreationInfo, met
276278
pv, err := packages_model.GetOrInsertVersion(ctx, _pv)
277279
if err != nil {
278280
if !errors.Is(err, packages_model.ErrDuplicatePackageVersion) {
279-
log.Error("Error inserting package: %v", err)
280-
return nil, err
281+
log.Error("Error GetOrInsertVersion (first try) package: %v", err)
282+
return nil, fmt.Errorf("GetOrInsertVersion: first try: %w", err)
281283
}
282284
if err = packages_service.DeletePackageVersionAndReferences(ctx, pv); err != nil {
283-
return nil, err
285+
return nil, fmt.Errorf("DeletePackageVersionAndReferences: %w", err)
284286
}
285287
// keep download count on overwriting
286288
_pv.DownloadCount = pv.DownloadCount
287289
pv, err = packages_model.GetOrInsertVersion(ctx, _pv)
288290
if err != nil {
289291
if !errors.Is(err, packages_model.ErrDuplicatePackageVersion) {
290-
log.Error("Error inserting package: %v", err)
291-
return nil, err
292+
log.Error("Error GetOrInsertVersion (second try) package: %v", err)
293+
return nil, fmt.Errorf("GetOrInsertVersion: second try: %w", err)
292294
}
293295
}
294296
}
295297

296298
if err := packages_service.CheckCountQuotaExceeded(ctx, mci.Creator, mci.Owner); err != nil {
297-
return nil, err
299+
return nil, fmt.Errorf("CheckCountQuotaExceeded: %w", err)
298300
}
299301

300302
if mci.IsTagged {
301303
if err = packages_model.InsertOrUpdateProperty(ctx, packages_model.PropertyTypeVersion, pv.ID, container_module.PropertyManifestTagged, ""); err != nil {
302-
return nil, err
304+
return nil, fmt.Errorf("InsertOrUpdateProperty(ManifestTagged): %w", err)
303305
}
304306
} else {
305307
if err = packages_model.DeletePropertiesByName(ctx, packages_model.PropertyTypeVersion, pv.ID, container_module.PropertyManifestTagged); err != nil {
306-
return nil, err
308+
return nil, fmt.Errorf("DeletePropertiesByName(ManifestTagged): %w", err)
307309
}
308310
}
309311

310312
if err = packages_model.DeletePropertiesByName(ctx, packages_model.PropertyTypeVersion, pv.ID, container_module.PropertyManifestReference); err != nil {
311-
return nil, err
313+
return nil, fmt.Errorf("DeletePropertiesByName(ManifestReference): %w", err)
312314
}
313315
for _, manifest := range metadata.Manifests {
314316
if _, err = packages_model.InsertProperty(ctx, packages_model.PropertyTypeVersion, pv.ID, container_module.PropertyManifestReference, manifest.Digest); err != nil {
315-
return nil, err
317+
return nil, fmt.Errorf("InsertProperty(ManifestReference): %w", err)
316318
}
317319
}
318320

0 commit comments

Comments
 (0)