replace AbortWithError calls by custom function that sets the content type correctly

This commit is contained in:
Markus Muellner
2022-10-26 17:13:37 +02:00
committed by Benj Fassbind
parent 81582bffd2
commit ecc41f0c0f
11 changed files with 116 additions and 111 deletions
+11 -6
View File
@@ -158,7 +158,7 @@ func maybeRunTaskInBackground(c *gin.Context, name string, resources []string, p
log.Println("Executing task asynchronously") log.Println("Executing task asynchronously")
task, conflictErr := runTaskInBackground(name, resources, proc) task, conflictErr := runTaskInBackground(name, resources, proc)
if conflictErr != nil { if conflictErr != nil {
c.AbortWithError(409, conflictErr) AbortWithJSONError(c, 409, conflictErr)
return return
} }
c.JSON(202, task) c.JSON(202, task)
@@ -168,7 +168,7 @@ func maybeRunTaskInBackground(c *gin.Context, name string, resources []string, p
detail := task.Detail{} detail := task.Detail{}
retValue, err := proc(out, &detail) retValue, err := proc(out, &detail)
if err != nil { if err != nil {
c.AbortWithError(retValue.Code, err) AbortWithJSONError(c, retValue.Code, err)
return return
} }
if retValue != nil { if retValue != nil {
@@ -186,7 +186,7 @@ func showPackages(c *gin.Context, reflist *deb.PackageRefList, collectionFactory
list, err := deb.NewPackageListFromRefList(reflist, collectionFactory.PackageCollection(), nil) list, err := deb.NewPackageListFromRefList(reflist, collectionFactory.PackageCollection(), nil)
if err != nil { if err != nil {
c.AbortWithError(404, err) AbortWithJSONError(c, 404, err)
return return
} }
@@ -194,7 +194,7 @@ func showPackages(c *gin.Context, reflist *deb.PackageRefList, collectionFactory
if queryS != "" { if queryS != "" {
q, err := query.Parse(c.Request.URL.Query().Get("q")) q, err := query.Parse(c.Request.URL.Query().Get("q"))
if err != nil { if err != nil {
c.AbortWithError(400, err) AbortWithJSONError(c, 400, err)
return return
} }
@@ -211,7 +211,7 @@ func showPackages(c *gin.Context, reflist *deb.PackageRefList, collectionFactory
sort.Strings(architecturesList) sort.Strings(architecturesList)
if len(architecturesList) == 0 { if len(architecturesList) == 0 {
c.AbortWithError(400, fmt.Errorf("unable to determine list of architectures, please specify explicitly")) AbortWithJSONError(c, 400, fmt.Errorf("unable to determine list of architectures, please specify explicitly"))
return return
} }
} }
@@ -221,7 +221,7 @@ func showPackages(c *gin.Context, reflist *deb.PackageRefList, collectionFactory
list, err = list.Filter([]deb.PackageQuery{q}, withDeps, list, err = list.Filter([]deb.PackageQuery{q}, withDeps,
nil, context.DependencyOptions(), architecturesList) nil, context.DependencyOptions(), architecturesList)
if err != nil { if err != nil {
c.AbortWithError(500, fmt.Errorf("unable to search: %s", err)) AbortWithJSONError(c, 500, fmt.Errorf("unable to search: %s", err))
return return
} }
} }
@@ -237,3 +237,8 @@ func showPackages(c *gin.Context, reflist *deb.PackageRefList, collectionFactory
c.JSON(200, list.Strings()) c.JSON(200, list.Strings())
} }
} }
func AbortWithJSONError(c *gin.Context, code int, err error) *gin.Error {
c.Writer.Header().Set("Content-Type", "application/json; charset=utf-8")
return c.AbortWithError(code, err)
}
+12 -12
View File
@@ -24,7 +24,7 @@ func verifyPath(path string) bool {
func verifyDir(c *gin.Context) bool { func verifyDir(c *gin.Context) bool {
if !verifyPath(c.Params.ByName("dir")) { if !verifyPath(c.Params.ByName("dir")) {
c.AbortWithError(400, fmt.Errorf("wrong dir")) AbortWithJSONError(c, 400, fmt.Errorf("wrong dir"))
return false return false
} }
@@ -53,7 +53,7 @@ func apiFilesListDirs(c *gin.Context) {
}) })
if err != nil && !os.IsNotExist(err) { if err != nil && !os.IsNotExist(err) {
c.AbortWithError(400, err) AbortWithJSONError(c, 400, err)
return return
} }
@@ -70,13 +70,13 @@ func apiFilesUpload(c *gin.Context) {
err := os.MkdirAll(path, 0777) err := os.MkdirAll(path, 0777)
if err != nil { if err != nil {
c.AbortWithError(500, err) AbortWithJSONError(c, 500, err)
return return
} }
err = c.Request.ParseMultipartForm(10 * 1024 * 1024) err = c.Request.ParseMultipartForm(10 * 1024 * 1024)
if err != nil { if err != nil {
c.AbortWithError(400, err) AbortWithJSONError(c, 400, err)
return return
} }
@@ -86,7 +86,7 @@ func apiFilesUpload(c *gin.Context) {
for _, file := range files { for _, file := range files {
src, err := file.Open() src, err := file.Open()
if err != nil { if err != nil {
c.AbortWithError(500, err) AbortWithJSONError(c, 500, err)
return return
} }
defer src.Close() defer src.Close()
@@ -94,14 +94,14 @@ func apiFilesUpload(c *gin.Context) {
destPath := filepath.Join(path, filepath.Base(file.Filename)) destPath := filepath.Join(path, filepath.Base(file.Filename))
dst, err := os.Create(destPath) dst, err := os.Create(destPath)
if err != nil { if err != nil {
c.AbortWithError(500, err) AbortWithJSONError(c, 500, err)
return return
} }
defer dst.Close() defer dst.Close()
_, err = io.Copy(dst, src) _, err = io.Copy(dst, src)
if err != nil { if err != nil {
c.AbortWithError(500, err) AbortWithJSONError(c, 500, err)
return return
} }
@@ -138,9 +138,9 @@ func apiFilesListFiles(c *gin.Context) {
if err != nil { if err != nil {
if os.IsNotExist(err) { if os.IsNotExist(err) {
c.AbortWithError(404, err) AbortWithJSONError(c, 404, err)
} else { } else {
c.AbortWithError(500, err) AbortWithJSONError(c, 500, err)
} }
return return
} }
@@ -156,7 +156,7 @@ func apiFilesDeleteDir(c *gin.Context) {
err := os.RemoveAll(filepath.Join(context.UploadPath(), c.Params.ByName("dir"))) err := os.RemoveAll(filepath.Join(context.UploadPath(), c.Params.ByName("dir")))
if err != nil { if err != nil {
c.AbortWithError(500, err) AbortWithJSONError(c, 500, err)
return return
} }
@@ -170,14 +170,14 @@ func apiFilesDeleteFile(c *gin.Context) {
} }
if !verifyPath(c.Params.ByName("name")) { if !verifyPath(c.Params.ByName("name")) {
c.AbortWithError(400, fmt.Errorf("wrong file")) AbortWithJSONError(c, 400, fmt.Errorf("wrong file"))
return return
} }
err := os.Remove(filepath.Join(context.UploadPath(), c.Params.ByName("dir"), c.Params.ByName("name"))) err := os.Remove(filepath.Join(context.UploadPath(), c.Params.ByName("dir"), c.Params.ByName("name")))
if err != nil { if err != nil {
if err1, ok := err.(*os.PathError); !ok || !os.IsNotExist(err1.Err) { if err1, ok := err.(*os.PathError); !ok || !os.IsNotExist(err1.Err) {
c.AbortWithError(500, err) AbortWithJSONError(c, 500, err)
return return
} }
} }
+5 -5
View File
@@ -38,7 +38,7 @@ func apiGPGAddKey(c *gin.Context) {
var tempdir string var tempdir string
tempdir, err = os.MkdirTemp(os.TempDir(), "aptly") tempdir, err = os.MkdirTemp(os.TempDir(), "aptly")
if err != nil { if err != nil {
c.AbortWithError(400, err) AbortWithJSONError(c, 400, err)
return return
} }
defer os.RemoveAll(tempdir) defer os.RemoveAll(tempdir)
@@ -46,11 +46,11 @@ func apiGPGAddKey(c *gin.Context) {
keypath := filepath.Join(tempdir, "key") keypath := filepath.Join(tempdir, "key")
keyfile, e := os.Create(keypath) keyfile, e := os.Create(keypath)
if e != nil { if e != nil {
c.AbortWithError(400, e) AbortWithJSONError(c, 400, e)
return return
} }
if _, e = keyfile.WriteString(b.GpgKeyArmor); e != nil { if _, e = keyfile.WriteString(b.GpgKeyArmor); e != nil {
c.AbortWithError(400, e) AbortWithJSONError(c, 400, e)
} }
args = append(args, "--import", keypath) args = append(args, "--import", keypath)
@@ -64,7 +64,7 @@ func apiGPGAddKey(c *gin.Context) {
finder := pgp.GPG1Finder() finder := pgp.GPG1Finder()
gpg, _, err := finder.FindGPG() gpg, _, err := finder.FindGPG()
if err != nil { if err != nil {
c.AbortWithError(400, err) AbortWithJSONError(c, 400, err)
return return
} }
@@ -75,7 +75,7 @@ func apiGPGAddKey(c *gin.Context) {
fmt.Printf("running %s %s\n", gpg, strings.Join(args, " ")) fmt.Printf("running %s %s\n", gpg, strings.Join(args, " "))
cmd.Stdout = os.Stdout cmd.Stdout = os.Stdout
if err = cmd.Run(); err != nil { if err = cmd.Run(); err != nil {
c.AbortWithError(400, err) AbortWithJSONError(c, 400, err)
return return
} }
+4 -4
View File
@@ -43,25 +43,25 @@ func apiGraph(c *gin.Context) {
stdin, err := command.StdinPipe() stdin, err := command.StdinPipe()
if err != nil { if err != nil {
c.AbortWithError(500, err) AbortWithJSONError(c, 500, err)
return return
} }
_, err = io.Copy(stdin, buf) _, err = io.Copy(stdin, buf)
if err != nil { if err != nil {
c.AbortWithError(500, err) AbortWithJSONError(c, 500, err)
return return
} }
err = stdin.Close() err = stdin.Close()
if err != nil { if err != nil {
c.AbortWithError(500, err) AbortWithJSONError(c, 500, err)
return return
} }
output, err = command.Output() output, err = command.Output()
if err != nil { if err != nil {
c.AbortWithError(500, fmt.Errorf("unable to execute dot: %s (is graphviz package installed?)", err)) AbortWithJSONError(c, 500, fmt.Errorf("unable to execute dot: %s (is graphviz package installed?)", err))
return return
} }
+20 -20
View File
@@ -81,7 +81,7 @@ func apiMirrorsCreate(c *gin.Context) {
if strings.HasPrefix(b.ArchiveURL, "ppa:") { if strings.HasPrefix(b.ArchiveURL, "ppa:") {
b.ArchiveURL, b.Distribution, b.Components, err = deb.ParsePPA(b.ArchiveURL, context.Config()) b.ArchiveURL, b.Distribution, b.Components, err = deb.ParsePPA(b.ArchiveURL, context.Config())
if err != nil { if err != nil {
c.AbortWithError(400, err) AbortWithJSONError(c, 400, err)
return return
} }
} }
@@ -89,7 +89,7 @@ func apiMirrorsCreate(c *gin.Context) {
if b.Filter != "" { if b.Filter != "" {
_, err = query.Parse(b.Filter) _, err = query.Parse(b.Filter)
if err != nil { if err != nil {
c.AbortWithError(400, fmt.Errorf("unable to create mirror: %s", err)) AbortWithJSONError(c, 400, fmt.Errorf("unable to create mirror: %s", err))
return return
} }
} }
@@ -98,7 +98,7 @@ func apiMirrorsCreate(c *gin.Context) {
b.DownloadSources, b.DownloadUdebs, b.DownloadInstaller) b.DownloadSources, b.DownloadUdebs, b.DownloadInstaller)
if err != nil { if err != nil {
c.AbortWithError(400, fmt.Errorf("unable to create mirror: %s", err)) AbortWithJSONError(c, 400, fmt.Errorf("unable to create mirror: %s", err))
return return
} }
@@ -110,20 +110,20 @@ func apiMirrorsCreate(c *gin.Context) {
verifier, err := getVerifier(b.IgnoreSignatures, b.Keyrings) verifier, err := getVerifier(b.IgnoreSignatures, b.Keyrings)
if err != nil { if err != nil {
c.AbortWithError(400, fmt.Errorf("unable to initialize GPG verifier: %s", err)) AbortWithJSONError(c, 400, fmt.Errorf("unable to initialize GPG verifier: %s", err))
return return
} }
downloader := context.NewDownloader(nil) downloader := context.NewDownloader(nil)
err = repo.Fetch(downloader, verifier) err = repo.Fetch(downloader, verifier)
if err != nil { if err != nil {
c.AbortWithError(400, fmt.Errorf("unable to fetch mirror: %s", err)) AbortWithJSONError(c, 400, fmt.Errorf("unable to fetch mirror: %s", err))
return return
} }
err = collection.Add(repo) err = collection.Add(repo)
if err != nil { if err != nil {
c.AbortWithError(500, fmt.Errorf("unable to add mirror: %s", err)) AbortWithJSONError(c, 500, fmt.Errorf("unable to add mirror: %s", err))
return return
} }
@@ -141,7 +141,7 @@ func apiMirrorsDrop(c *gin.Context) {
repo, err := mirrorCollection.ByName(name) repo, err := mirrorCollection.ByName(name)
if err != nil { if err != nil {
c.AbortWithError(404, fmt.Errorf("unable to drop: %s", err)) AbortWithJSONError(c, 404, fmt.Errorf("unable to drop: %s", err))
return return
} }
@@ -177,13 +177,13 @@ func apiMirrorsShow(c *gin.Context) {
name := c.Params.ByName("name") name := c.Params.ByName("name")
repo, err := collection.ByName(name) repo, err := collection.ByName(name)
if err != nil { if err != nil {
c.AbortWithError(404, fmt.Errorf("unable to show: %s", err)) AbortWithJSONError(c, 404, fmt.Errorf("unable to show: %s", err))
return return
} }
err = collection.LoadComplete(repo) err = collection.LoadComplete(repo)
if err != nil { if err != nil {
c.AbortWithError(500, fmt.Errorf("unable to show: %s", err)) AbortWithJSONError(c, 500, fmt.Errorf("unable to show: %s", err))
} }
c.JSON(200, repo) c.JSON(200, repo)
@@ -197,17 +197,17 @@ func apiMirrorsPackages(c *gin.Context) {
name := c.Params.ByName("name") name := c.Params.ByName("name")
repo, err := collection.ByName(name) repo, err := collection.ByName(name)
if err != nil { if err != nil {
c.AbortWithError(404, fmt.Errorf("unable to show: %s", err)) AbortWithJSONError(c, 404, fmt.Errorf("unable to show: %s", err))
return return
} }
err = collection.LoadComplete(repo) err = collection.LoadComplete(repo)
if err != nil { if err != nil {
c.AbortWithError(500, fmt.Errorf("unable to show: %s", err)) AbortWithJSONError(c, 500, fmt.Errorf("unable to show: %s", err))
} }
if repo.LastDownloadDate.IsZero() { if repo.LastDownloadDate.IsZero() {
c.AbortWithError(404, fmt.Errorf("unable to show package list, mirror hasn't been downloaded yet")) AbortWithJSONError(c, 404, fmt.Errorf("unable to show package list, mirror hasn't been downloaded yet"))
return return
} }
@@ -216,7 +216,7 @@ func apiMirrorsPackages(c *gin.Context) {
list, err := deb.NewPackageListFromRefList(reflist, collectionFactory.PackageCollection(), nil) list, err := deb.NewPackageListFromRefList(reflist, collectionFactory.PackageCollection(), nil)
if err != nil { if err != nil {
c.AbortWithError(404, err) AbortWithJSONError(c, 404, err)
return return
} }
@@ -224,7 +224,7 @@ func apiMirrorsPackages(c *gin.Context) {
if queryS != "" { if queryS != "" {
q, err := query.Parse(c.Request.URL.Query().Get("q")) q, err := query.Parse(c.Request.URL.Query().Get("q"))
if err != nil { if err != nil {
c.AbortWithError(400, err) AbortWithJSONError(c, 400, err)
return return
} }
@@ -241,7 +241,7 @@ func apiMirrorsPackages(c *gin.Context) {
sort.Strings(architecturesList) sort.Strings(architecturesList)
if len(architecturesList) == 0 { if len(architecturesList) == 0 {
c.AbortWithError(400, fmt.Errorf("unable to determine list of architectures, please specify explicitly")) AbortWithJSONError(c, 400, fmt.Errorf("unable to determine list of architectures, please specify explicitly"))
return return
} }
} }
@@ -251,7 +251,7 @@ func apiMirrorsPackages(c *gin.Context) {
list, err = list.Filter([]deb.PackageQuery{q}, withDeps, list, err = list.Filter([]deb.PackageQuery{q}, withDeps,
nil, context.DependencyOptions(), architecturesList) nil, context.DependencyOptions(), architecturesList)
if err != nil { if err != nil {
c.AbortWithError(500, fmt.Errorf("unable to search: %s", err)) AbortWithJSONError(c, 500, fmt.Errorf("unable to search: %s", err))
} }
} }
@@ -296,7 +296,7 @@ func apiMirrorsUpdate(c *gin.Context) {
remote, err = collection.ByName(c.Params.ByName("name")) remote, err = collection.ByName(c.Params.ByName("name"))
if err != nil { if err != nil {
c.AbortWithError(404, err) AbortWithJSONError(c, 404, err)
return return
} }
@@ -318,14 +318,14 @@ func apiMirrorsUpdate(c *gin.Context) {
if b.Name != remote.Name { if b.Name != remote.Name {
_, err = collection.ByName(b.Name) _, err = collection.ByName(b.Name)
if err == nil { if err == nil {
c.AbortWithError(409, fmt.Errorf("unable to rename: mirror %s already exists", b.Name)) AbortWithJSONError(c, 409, fmt.Errorf("unable to rename: mirror %s already exists", b.Name))
return return
} }
} }
if b.DownloadUdebs != remote.DownloadUdebs { if b.DownloadUdebs != remote.DownloadUdebs {
if remote.IsFlat() && b.DownloadUdebs { if remote.IsFlat() && b.DownloadUdebs {
c.AbortWithError(400, fmt.Errorf("unable to update: flat mirrors don't support udebs")) AbortWithJSONError(c, 400, fmt.Errorf("unable to update: flat mirrors don't support udebs"))
return return
} }
} }
@@ -345,7 +345,7 @@ func apiMirrorsUpdate(c *gin.Context) {
verifier, err := getVerifier(b.IgnoreSignatures, b.Keyrings) verifier, err := getVerifier(b.IgnoreSignatures, b.Keyrings)
if err != nil { if err != nil {
c.AbortWithError(400, fmt.Errorf("unable to initialize GPG verifier: %s", err)) AbortWithJSONError(c, 400, fmt.Errorf("unable to initialize GPG verifier: %s", err))
return return
} }
+1 -1
View File
@@ -9,7 +9,7 @@ func apiPackagesShow(c *gin.Context) {
collectionFactory := context.NewCollectionFactory() collectionFactory := context.NewCollectionFactory()
p, err := collectionFactory.PackageCollection().ByKey([]byte(c.Params.ByName("key"))) p, err := collectionFactory.PackageCollection().ByKey([]byte(c.Params.ByName("key")))
if err != nil { if err != nil {
c.AbortWithError(404, err) AbortWithJSONError(c, 404, err)
return return
} }
+18 -18
View File
@@ -71,7 +71,7 @@ func apiPublishList(c *gin.Context) {
}) })
if err != nil { if err != nil {
c.AbortWithError(500, err) AbortWithJSONError(c, 500, err)
return return
} }
@@ -108,12 +108,12 @@ func apiPublishRepoOrSnapshot(c *gin.Context) {
signer, err := getSigner(&b.Signing) signer, err := getSigner(&b.Signing)
if err != nil { if err != nil {
c.AbortWithError(500, fmt.Errorf("unable to initialize GPG signer: %s", err)) AbortWithJSONError(c, 500, fmt.Errorf("unable to initialize GPG signer: %s", err))
return return
} }
if len(b.Sources) == 0 { if len(b.Sources) == 0 {
c.AbortWithError(400, fmt.Errorf("unable to publish: soures are empty")) AbortWithJSONError(c, 400, fmt.Errorf("unable to publish: soures are empty"))
return return
} }
@@ -134,14 +134,14 @@ func apiPublishRepoOrSnapshot(c *gin.Context) {
snapshot, err = snapshotCollection.ByName(source.Name) snapshot, err = snapshotCollection.ByName(source.Name)
if err != nil { if err != nil {
c.AbortWithError(404, fmt.Errorf("unable to publish: %s", err)) AbortWithJSONError(c, 404, fmt.Errorf("unable to publish: %s", err))
return return
} }
resources = append(resources, string(snapshot.ResourceKey())) resources = append(resources, string(snapshot.ResourceKey()))
err = snapshotCollection.LoadComplete(snapshot) err = snapshotCollection.LoadComplete(snapshot)
if err != nil { if err != nil {
c.AbortWithError(500, fmt.Errorf("unable to publish: %s", err)) AbortWithJSONError(c, 500, fmt.Errorf("unable to publish: %s", err))
return return
} }
@@ -158,26 +158,26 @@ func apiPublishRepoOrSnapshot(c *gin.Context) {
localRepo, err = localCollection.ByName(source.Name) localRepo, err = localCollection.ByName(source.Name)
if err != nil { if err != nil {
c.AbortWithError(404, fmt.Errorf("unable to publish: %s", err)) AbortWithJSONError(c, 404, fmt.Errorf("unable to publish: %s", err))
return return
} }
resources = append(resources, string(localRepo.Key())) resources = append(resources, string(localRepo.Key()))
err = localCollection.LoadComplete(localRepo) err = localCollection.LoadComplete(localRepo)
if err != nil { if err != nil {
c.AbortWithError(500, fmt.Errorf("unable to publish: %s", err)) AbortWithJSONError(c, 500, fmt.Errorf("unable to publish: %s", err))
} }
sources = append(sources, localRepo) sources = append(sources, localRepo)
} }
} else { } else {
c.AbortWithError(400, fmt.Errorf("unknown SourceKind")) AbortWithJSONError(c, 400, fmt.Errorf("unknown SourceKind"))
return return
} }
published, err := deb.NewPublishedRepo(storage, prefix, b.Distribution, b.Architectures, components, sources, collectionFactory) published, err := deb.NewPublishedRepo(storage, prefix, b.Distribution, b.Architectures, components, sources, collectionFactory)
if err != nil { if err != nil {
c.AbortWithError(500, fmt.Errorf("unable to publish: %s", err)) AbortWithJSONError(c, 500, fmt.Errorf("unable to publish: %s", err))
return return
} }
@@ -264,7 +264,7 @@ func apiPublishUpdateSwitch(c *gin.Context) {
signer, err := getSigner(&b.Signing) signer, err := getSigner(&b.Signing)
if err != nil { if err != nil {
c.AbortWithError(500, fmt.Errorf("unable to initialize GPG signer: %s", err)) AbortWithJSONError(c, 500, fmt.Errorf("unable to initialize GPG signer: %s", err))
return return
} }
@@ -273,12 +273,12 @@ func apiPublishUpdateSwitch(c *gin.Context) {
published, err := collection.ByStoragePrefixDistribution(storage, prefix, distribution) published, err := collection.ByStoragePrefixDistribution(storage, prefix, distribution)
if err != nil { if err != nil {
c.AbortWithError(404, fmt.Errorf("unable to update: %s", err)) AbortWithJSONError(c, 404, fmt.Errorf("unable to update: %s", err))
return return
} }
err = collection.LoadComplete(published, collectionFactory) err = collection.LoadComplete(published, collectionFactory)
if err != nil { if err != nil {
c.AbortWithError(500, fmt.Errorf("unable to update: %s", err)) AbortWithJSONError(c, 500, fmt.Errorf("unable to update: %s", err))
return return
} }
@@ -288,7 +288,7 @@ func apiPublishUpdateSwitch(c *gin.Context) {
if published.SourceKind == deb.SourceLocalRepo { if published.SourceKind == deb.SourceLocalRepo {
if len(b.Snapshots) > 0 { if len(b.Snapshots) > 0 {
c.AbortWithError(400, fmt.Errorf("snapshots shouldn't be given when updating local repo")) AbortWithJSONError(c, 400, fmt.Errorf("snapshots shouldn't be given when updating local repo"))
return return
} }
updatedComponents = published.Components() updatedComponents = published.Components()
@@ -299,20 +299,20 @@ func apiPublishUpdateSwitch(c *gin.Context) {
publishedComponents := published.Components() publishedComponents := published.Components()
for _, snapshotInfo := range b.Snapshots { for _, snapshotInfo := range b.Snapshots {
if !utils.StrSliceHasItem(publishedComponents, snapshotInfo.Component) { if !utils.StrSliceHasItem(publishedComponents, snapshotInfo.Component) {
c.AbortWithError(404, fmt.Errorf("component %s is not in published repository", snapshotInfo.Component)) AbortWithJSONError(c, 404, fmt.Errorf("component %s is not in published repository", snapshotInfo.Component))
return return
} }
snapshotCollection := collectionFactory.SnapshotCollection() snapshotCollection := collectionFactory.SnapshotCollection()
snapshot, err2 := snapshotCollection.ByName(snapshotInfo.Name) snapshot, err2 := snapshotCollection.ByName(snapshotInfo.Name)
if err2 != nil { if err2 != nil {
c.AbortWithError(404, err2) AbortWithJSONError(c, 404, err2)
return return
} }
err2 = snapshotCollection.LoadComplete(snapshot) err2 = snapshotCollection.LoadComplete(snapshot)
if err2 != nil { if err2 != nil {
c.AbortWithError(500, err2) AbortWithJSONError(c, 500, err2)
return return
} }
@@ -321,7 +321,7 @@ func apiPublishUpdateSwitch(c *gin.Context) {
updatedSnapshots = append(updatedSnapshots, snapshot.Name) updatedSnapshots = append(updatedSnapshots, snapshot.Name)
} }
} else { } else {
c.AbortWithError(500, fmt.Errorf("unknown published repository type")) AbortWithJSONError(c, 500, fmt.Errorf("unknown published repository type"))
return return
} }
@@ -376,7 +376,7 @@ func apiPublishDrop(c *gin.Context) {
published, err := collection.ByStoragePrefixDistribution(storage, prefix, distribution) published, err := collection.ByStoragePrefixDistribution(storage, prefix, distribution)
if err != nil { if err != nil {
c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("unable to drop: %s", err)) AbortWithJSONError(c, http.StatusInternalServerError, fmt.Errorf("unable to drop: %s", err))
return return
} }
+16 -16
View File
@@ -52,7 +52,7 @@ func apiReposCreate(c *gin.Context) {
collection := collectionFactory.LocalRepoCollection() collection := collectionFactory.LocalRepoCollection()
err := collection.Add(repo) err := collection.Add(repo)
if err != nil { if err != nil {
c.AbortWithError(400, err) AbortWithJSONError(c, 400, err)
return return
} }
@@ -77,7 +77,7 @@ func apiReposEdit(c *gin.Context) {
repo, err := collection.ByName(c.Params.ByName("name")) repo, err := collection.ByName(c.Params.ByName("name"))
if err != nil { if err != nil {
c.AbortWithError(404, err) AbortWithJSONError(c, 404, err)
return return
} }
@@ -85,7 +85,7 @@ func apiReposEdit(c *gin.Context) {
_, err := collection.ByName(*b.Name) _, err := collection.ByName(*b.Name)
if err == nil { if err == nil {
// already exists // already exists
c.AbortWithError(404, err) AbortWithJSONError(c, 404, err)
return return
} }
repo.Name = *b.Name repo.Name = *b.Name
@@ -102,7 +102,7 @@ func apiReposEdit(c *gin.Context) {
err = collection.Update(repo) err = collection.Update(repo)
if err != nil { if err != nil {
c.AbortWithError(500, err) AbortWithJSONError(c, 500, err)
return return
} }
@@ -116,7 +116,7 @@ func apiReposShow(c *gin.Context) {
repo, err := collection.ByName(c.Params.ByName("name")) repo, err := collection.ByName(c.Params.ByName("name"))
if err != nil { if err != nil {
c.AbortWithError(404, err) AbortWithJSONError(c, 404, err)
return return
} }
@@ -135,7 +135,7 @@ func apiReposDrop(c *gin.Context) {
repo, err := collection.ByName(name) repo, err := collection.ByName(name)
if err != nil { if err != nil {
c.AbortWithError(404, err) AbortWithJSONError(c, 404, err)
return return
} }
@@ -165,13 +165,13 @@ func apiReposPackagesShow(c *gin.Context) {
repo, err := collection.ByName(c.Params.ByName("name")) repo, err := collection.ByName(c.Params.ByName("name"))
if err != nil { if err != nil {
c.AbortWithError(404, err) AbortWithJSONError(c, 404, err)
return return
} }
err = collection.LoadComplete(repo) err = collection.LoadComplete(repo)
if err != nil { if err != nil {
c.AbortWithError(500, err) AbortWithJSONError(c, 500, err)
return return
} }
@@ -193,13 +193,13 @@ func apiReposPackagesAddDelete(c *gin.Context, taskNamePrefix string, cb func(li
repo, err := collection.ByName(c.Params.ByName("name")) repo, err := collection.ByName(c.Params.ByName("name"))
if err != nil { if err != nil {
c.AbortWithError(404, err) AbortWithJSONError(c, 404, err)
return return
} }
err = collection.LoadComplete(repo) err = collection.LoadComplete(repo)
if err != nil { if err != nil {
c.AbortWithError(500, err) AbortWithJSONError(c, 500, err)
return return
} }
@@ -274,7 +274,7 @@ func apiReposPackageFromDir(c *gin.Context) {
dirParam := c.Params.ByName("dir") dirParam := c.Params.ByName("dir")
fileParam := c.Params.ByName("file") fileParam := c.Params.ByName("file")
if fileParam != "" && !verifyPath(fileParam) { if fileParam != "" && !verifyPath(fileParam) {
c.AbortWithError(400, fmt.Errorf("wrong file")) AbortWithJSONError(c, 400, fmt.Errorf("wrong file"))
return return
} }
@@ -284,13 +284,13 @@ func apiReposPackageFromDir(c *gin.Context) {
name := c.Params.ByName("name") name := c.Params.ByName("name")
repo, err := collection.ByName(name) repo, err := collection.ByName(name)
if err != nil { if err != nil {
c.AbortWithError(404, err) AbortWithJSONError(c, 404, err)
return return
} }
err = collection.LoadComplete(repo) err = collection.LoadComplete(repo)
if err != nil { if err != nil {
c.AbortWithError(500, err) AbortWithJSONError(c, 500, err)
return return
} }
@@ -407,7 +407,7 @@ func apiReposIncludePackageFromDir(c *gin.Context) {
dirParam := c.Params.ByName("dir") dirParam := c.Params.ByName("dir")
fileParam := c.Params.ByName("file") fileParam := c.Params.ByName("file")
if fileParam != "" && !verifyPath(fileParam) { if fileParam != "" && !verifyPath(fileParam) {
c.AbortWithError(400, fmt.Errorf("wrong file")) AbortWithJSONError(c, 400, fmt.Errorf("wrong file"))
return return
} }
@@ -421,7 +421,7 @@ func apiReposIncludePackageFromDir(c *gin.Context) {
repoTemplate, err := template.New("repo").Parse(repoTemplateString) repoTemplate, err := template.New("repo").Parse(repoTemplateString)
if err != nil { if err != nil {
c.AbortWithError(400, fmt.Errorf("error parsing repo template: %s", err)) AbortWithJSONError(c, 400, fmt.Errorf("error parsing repo template: %s", err))
return return
} }
@@ -432,7 +432,7 @@ func apiReposIncludePackageFromDir(c *gin.Context) {
// repo template string is simple text so only use resource key of specific repository // repo template string is simple text so only use resource key of specific repository
repo, err := collectionFactory.LocalRepoCollection().ByName(repoTemplateString) repo, err := collectionFactory.LocalRepoCollection().ByName(repoTemplateString)
if err != nil { if err != nil {
c.AbortWithError(404, err) AbortWithJSONError(c, 404, err)
return return
} }
+2 -2
View File
@@ -45,7 +45,7 @@ func Router(c *ctx.AptlyContext) http.Handler {
err = <-errCh err = <-errCh
if err != nil { if err != nil {
c.AbortWithError(500, err) AbortWithJSONError(c, 500, err)
return return
} }
@@ -53,7 +53,7 @@ func Router(c *ctx.AptlyContext) http.Handler {
dbRequests <- dbRequest{releasedb, errCh} dbRequests <- dbRequest{releasedb, errCh}
err = <-errCh err = <-errCh
if err != nil { if err != nil {
c.AbortWithError(500, err) AbortWithJSONError(c, 500, err)
} }
}() }()
+15 -15
View File
@@ -55,7 +55,7 @@ func apiSnapshotsCreateFromMirror(c *gin.Context) {
repo, err = collection.ByName(name) repo, err = collection.ByName(name)
if err != nil { if err != nil {
c.AbortWithError(404, err) AbortWithJSONError(c, 404, err)
return return
} }
@@ -123,13 +123,13 @@ func apiSnapshotsCreate(c *gin.Context) {
for i := range b.SourceSnapshots { for i := range b.SourceSnapshots {
sources[i], err = snapshotCollection.ByName(b.SourceSnapshots[i]) sources[i], err = snapshotCollection.ByName(b.SourceSnapshots[i])
if err != nil { if err != nil {
c.AbortWithError(404, err) AbortWithJSONError(c, 404, err)
return return
} }
err = snapshotCollection.LoadComplete(sources[i]) err = snapshotCollection.LoadComplete(sources[i])
if err != nil { if err != nil {
c.AbortWithError(500, err) AbortWithJSONError(c, 500, err)
return return
} }
@@ -188,7 +188,7 @@ func apiSnapshotsCreateFromRepository(c *gin.Context) {
repo, err = collection.ByName(name) repo, err = collection.ByName(name)
if err != nil { if err != nil {
c.AbortWithError(404, err) AbortWithJSONError(c, 404, err)
return return
} }
@@ -240,7 +240,7 @@ func apiSnapshotsUpdate(c *gin.Context) {
snapshot, err = collection.ByName(name) snapshot, err = collection.ByName(name)
if err != nil { if err != nil {
c.AbortWithError(404, err) AbortWithJSONError(c, 404, err)
return return
} }
@@ -275,13 +275,13 @@ func apiSnapshotsShow(c *gin.Context) {
snapshot, err := collection.ByName(c.Params.ByName("name")) snapshot, err := collection.ByName(c.Params.ByName("name"))
if err != nil { if err != nil {
c.AbortWithError(404, err) AbortWithJSONError(c, 404, err)
return return
} }
err = collection.LoadComplete(snapshot) err = collection.LoadComplete(snapshot)
if err != nil { if err != nil {
c.AbortWithError(500, err) AbortWithJSONError(c, 500, err)
return return
} }
@@ -299,7 +299,7 @@ func apiSnapshotsDrop(c *gin.Context) {
snapshot, err := snapshotCollection.ByName(name) snapshot, err := snapshotCollection.ByName(name)
if err != nil { if err != nil {
c.AbortWithError(404, err) AbortWithJSONError(c, 404, err)
return return
} }
@@ -336,32 +336,32 @@ func apiSnapshotsDiff(c *gin.Context) {
snapshotA, err := collection.ByName(c.Params.ByName("name")) snapshotA, err := collection.ByName(c.Params.ByName("name"))
if err != nil { if err != nil {
c.AbortWithError(404, err) AbortWithJSONError(c, 404, err)
return return
} }
snapshotB, err := collection.ByName(c.Params.ByName("withSnapshot")) snapshotB, err := collection.ByName(c.Params.ByName("withSnapshot"))
if err != nil { if err != nil {
c.AbortWithError(404, err) AbortWithJSONError(c, 404, err)
return return
} }
err = collection.LoadComplete(snapshotA) err = collection.LoadComplete(snapshotA)
if err != nil { if err != nil {
c.AbortWithError(500, err) AbortWithJSONError(c, 500, err)
return return
} }
err = collection.LoadComplete(snapshotB) err = collection.LoadComplete(snapshotB)
if err != nil { if err != nil {
c.AbortWithError(500, err) AbortWithJSONError(c, 500, err)
return return
} }
// Calculate diff // Calculate diff
diff, err := snapshotA.RefList().Diff(snapshotB.RefList(), collectionFactory.PackageCollection()) diff, err := snapshotA.RefList().Diff(snapshotB.RefList(), collectionFactory.PackageCollection())
if err != nil { if err != nil {
c.AbortWithError(500, err) AbortWithJSONError(c, 500, err)
return return
} }
@@ -385,13 +385,13 @@ func apiSnapshotsSearchPackages(c *gin.Context) {
snapshot, err := collection.ByName(c.Params.ByName("name")) snapshot, err := collection.ByName(c.Params.ByName("name"))
if err != nil { if err != nil {
c.AbortWithError(404, err) AbortWithJSONError(c, 404, err)
return return
} }
err = collection.LoadComplete(snapshot) err = collection.LoadComplete(snapshot)
if err != nil { if err != nil {
c.AbortWithError(500, err) AbortWithJSONError(c, 500, err)
return return
} }
+12 -12
View File
@@ -35,13 +35,13 @@ func apiTasksWaitForTaskByID(c *gin.Context) {
list := context.TaskList() list := context.TaskList()
id, err := strconv.ParseInt(c.Params.ByName("id"), 10, 0) id, err := strconv.ParseInt(c.Params.ByName("id"), 10, 0)
if err != nil { if err != nil {
c.AbortWithError(500, err) AbortWithJSONError(c, 500, err)
return return
} }
task, err := list.WaitForTaskByID(int(id)) task, err := list.WaitForTaskByID(int(id))
if err != nil { if err != nil {
c.AbortWithError(400, err) AbortWithJSONError(c, 400, err)
return return
} }
@@ -53,14 +53,14 @@ func apiTasksShow(c *gin.Context) {
list := context.TaskList() list := context.TaskList()
id, err := strconv.ParseInt(c.Params.ByName("id"), 10, 0) id, err := strconv.ParseInt(c.Params.ByName("id"), 10, 0)
if err != nil { if err != nil {
c.AbortWithError(500, err) AbortWithJSONError(c, 500, err)
return return
} }
var task task.Task var task task.Task
task, err = list.GetTaskByID(int(id)) task, err = list.GetTaskByID(int(id))
if err != nil { if err != nil {
c.AbortWithError(404, err) AbortWithJSONError(c, 404, err)
return return
} }
@@ -72,14 +72,14 @@ func apiTasksOutputShow(c *gin.Context) {
list := context.TaskList() list := context.TaskList()
id, err := strconv.ParseInt(c.Params.ByName("id"), 10, 0) id, err := strconv.ParseInt(c.Params.ByName("id"), 10, 0)
if err != nil { if err != nil {
c.AbortWithError(500, err) AbortWithJSONError(c, 500, err)
return return
} }
var output string var output string
output, err = list.GetTaskOutputByID(int(id)) output, err = list.GetTaskOutputByID(int(id))
if err != nil { if err != nil {
c.AbortWithError(404, err) AbortWithJSONError(c, 404, err)
return return
} }
@@ -91,14 +91,14 @@ func apiTasksDetailShow(c *gin.Context) {
list := context.TaskList() list := context.TaskList()
id, err := strconv.ParseInt(c.Params.ByName("id"), 10, 0) id, err := strconv.ParseInt(c.Params.ByName("id"), 10, 0)
if err != nil { if err != nil {
c.AbortWithError(500, err) AbortWithJSONError(c, 500, err)
return return
} }
var detail interface{} var detail interface{}
detail, err = list.GetTaskDetailByID(int(id)) detail, err = list.GetTaskDetailByID(int(id))
if err != nil { if err != nil {
c.AbortWithError(404, err) AbortWithJSONError(c, 404, err)
return return
} }
@@ -110,13 +110,13 @@ func apiTasksReturnValueShow(c *gin.Context) {
list := context.TaskList() list := context.TaskList()
id, err := strconv.ParseInt(c.Params.ByName("id"), 10, 0) id, err := strconv.ParseInt(c.Params.ByName("id"), 10, 0)
if err != nil { if err != nil {
c.AbortWithError(500, err) AbortWithJSONError(c, 500, err)
return return
} }
output, err := list.GetTaskReturnValueByID(int(id)) output, err := list.GetTaskReturnValueByID(int(id))
if err != nil { if err != nil {
c.AbortWithError(404, err) AbortWithJSONError(c, 404, err)
return return
} }
@@ -128,14 +128,14 @@ func apiTasksDelete(c *gin.Context) {
list := context.TaskList() list := context.TaskList()
id, err := strconv.ParseInt(c.Params.ByName("id"), 10, 0) id, err := strconv.ParseInt(c.Params.ByName("id"), 10, 0)
if err != nil { if err != nil {
c.AbortWithError(500, err) AbortWithJSONError(c, 500, err)
return return
} }
var delTask task.Task var delTask task.Task
delTask, err = list.DeleteTaskByID(int(id)) delTask, err = list.DeleteTaskByID(int(id))
if err != nil { if err != nil {
c.AbortWithError(400, err) AbortWithJSONError(c, 400, err)
return return
} }