Skip to content

Commit be90f0d

Browse files
committed
making better error messaging
1 parent e5901fa commit be90f0d

7 files changed

Lines changed: 411 additions & 235 deletions

File tree

services/container/download.go

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,27 @@ func (s *Service) DownloadHandler(c *gin.Context) {
2323
}
2424
err := pkg.FillByName(name)
2525
if err != nil {
26-
c.JSON(500, gin.H{"error": "Error while trying to get package info"})
26+
c.JSON(500, gin.H{
27+
"errors": []gin.H{
28+
{
29+
"code": "DENIED",
30+
"message": "authentication required",
31+
"detail": "Error while trying to get package info",
32+
},
33+
},
34+
})
2735
return
2836
}
2937
if pkg.ID == uuid.Nil {
30-
c.JSON(404, gin.H{"error": "Package not found"})
38+
c.JSON(404, gin.H{
39+
"errors": []gin.H{
40+
{
41+
"code": "DENIED",
42+
"message": "authentication required",
43+
"detail": "Package not found",
44+
},
45+
},
46+
})
3147
return
3248
}
3349

@@ -36,17 +52,41 @@ func (s *Service) DownloadHandler(c *gin.Context) {
3652
}
3753
err = asset.FillByDigest(digest)
3854
if err != nil {
39-
c.JSON(500, gin.H{"error": "Unable to check the DB for package version"})
55+
c.JSON(500, gin.H{
56+
"errors": []gin.H{
57+
{
58+
"code": "DENIED",
59+
"message": "authentication required",
60+
"detail": "Unable to check the DB for package version",
61+
},
62+
},
63+
})
4064
return
4165
}
4266
if asset.Digest != digest {
43-
c.JSON(404, gin.H{"error": "Uploaded asset not found"})
67+
c.JSON(404, gin.H{
68+
"errors": []gin.H{
69+
{
70+
"code": "DENIED",
71+
"message": "authentication required",
72+
"detail": "Uploaded asset not found",
73+
},
74+
},
75+
})
4476
return
4577
}
4678

4779
fileData, err := s.Storage.GetFile(s.PackageFilename(asset.Digest))
4880
if err != nil || fileData == nil {
49-
c.JSON(404, gin.H{"error": "Not Found"})
81+
c.JSON(404, gin.H{
82+
"errors": []gin.H{
83+
{
84+
"code": "DENIED",
85+
"message": "authentication required",
86+
"detail": "Not Found",
87+
},
88+
},
89+
})
5090
return
5191
}
5292

services/container/metadata.go

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,27 @@ func (s *Service) pkgVersionMetadata(c *gin.Context) (pkg models.Package[Package
4747
}
4848
err := pkg.FillByName(name)
4949
if err != nil {
50-
c.JSON(500, gin.H{"error": "Error while trying to get package info"})
50+
c.JSON(500, gin.H{
51+
"errors": []gin.H{
52+
{
53+
"code": "DENIED",
54+
"message": "authentication required",
55+
"detail": "Error while trying to get package info",
56+
},
57+
},
58+
})
5159
return
5260
}
5361
if pkg.ID == uuid.Nil {
54-
c.JSON(404, gin.H{"error": "Package not found"})
62+
c.JSON(404, gin.H{
63+
"errors": []gin.H{
64+
{
65+
"code": "DENIED",
66+
"message": "authentication required",
67+
"detail": "Package not found",
68+
},
69+
},
70+
})
5571
return
5672
}
5773
if strings.Contains(tagOrDigest, "sha256:") {
@@ -62,11 +78,27 @@ func (s *Service) pkgVersionMetadata(c *gin.Context) (pkg models.Package[Package
6278
pkgVersion, err = pkg.Version(tagOrDigest)
6379
}
6480
if err != nil {
65-
c.JSON(500, gin.H{"error": "Error while trying to get package info"})
81+
c.JSON(500, gin.H{
82+
"errors": []gin.H{
83+
{
84+
"code": "DENIED",
85+
"message": "authentication required",
86+
"detail": "Error while trying to get package info",
87+
},
88+
},
89+
})
6690
return
6791
}
6892
if pkgVersion.ID == uuid.Nil || pkgVersion.PackageId != pkg.ID {
69-
c.JSON(404, gin.H{"error": "Package version not found"})
93+
c.JSON(404, gin.H{
94+
"errors": []gin.H{
95+
{
96+
"code": "DENIED",
97+
"message": "authentication required",
98+
"detail": "Package version not found",
99+
},
100+
},
101+
})
70102
return
71103
}
72104
return

services/container/service.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ func (s *Service) SetAuthHeaderAndAbort(c *gin.Context, message string) {
7474
"errors": []gin.H{
7575
{
7676
"code": "DENIED",
77-
"message": "authentication required",
78-
"detail": "Unable to parse registry host",
77+
"message": "Unable to parse registry host",
78+
"detail": "authentication required",
7979
},
8080
},
8181
})
@@ -87,7 +87,7 @@ func (s *Service) SetAuthHeaderAndAbort(c *gin.Context, message string) {
8787
"errors": []gin.H{
8888
{
8989
"code": "UNAUTHORIZED",
90-
"message": "authentication required",
90+
"message": message,
9191
"detail": message,
9292
},
9393
},

0 commit comments

Comments
 (0)