Skip to content

Commit

Permalink
forward status code if available
Browse files Browse the repository at this point in the history
Signed-off-by: Adphi <philippe.adrien.nousse@gmail.com>
  • Loading branch information
Adphi committed Oct 3, 2023
1 parent c89b776 commit 5c2a6dd
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 23 deletions.
26 changes: 4 additions & 22 deletions pkg/packages/rpm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@ package rpm

import (
"context"
"errors"
"fmt"
"io"
"net/http"
"os"
"strings"

"github.com/gorilla/mux"
Expand Down Expand Up @@ -106,11 +104,7 @@ func (p *provider) upload(w http.ResponseWriter, r *http.Request) {
return
}
if err := repo.Write(ctx, pkg); err != nil {
if errors.Is(err, os.ErrExist) {
http.Error(w, err.Error(), http.StatusConflict)
return
}
http.Error(w, err.Error(), http.StatusInternalServerError)
repository.Error(w, err)
return
}
w.WriteHeader(http.StatusCreated)
Expand All @@ -125,11 +119,7 @@ func (p *provider) download(w http.ResponseWriter, r *http.Request) {
return
}
if err := repo.ServeFile(w, r, file); err != nil {
if errors.Is(err, os.ErrNotExist) {
http.Error(w, err.Error(), http.StatusNotFound)
return
}
http.Error(w, err.Error(), http.StatusInternalServerError)
repository.Error(w, err)
return
}
}
Expand All @@ -143,11 +133,7 @@ func (p *provider) delete(w http.ResponseWriter, r *http.Request) {
return
}
if err := repo.Delete(ctx, file); err != nil {
if errors.Is(err, os.ErrNotExist) {
http.Error(w, err.Error(), http.StatusNotFound)
return
}
http.Error(w, err.Error(), http.StatusInternalServerError)
repository.Error(w, err)
return
}
}
Expand All @@ -161,11 +147,7 @@ func (p *provider) repository(w http.ResponseWriter, r *http.Request) {
return
}
if err := repo.ServeFile(w, r, file); err != nil {
if errors.Is(err, os.ErrNotExist) {
http.Error(w, err.Error(), http.StatusNotFound)
return
}
http.Error(w, err.Error(), http.StatusInternalServerError)
repository.Error(w, err)
return
}
}
25 changes: 25 additions & 0 deletions pkg/repository/oci_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
"oras.land/oras-go/v2/errdef"
"oras.land/oras-go/v2/registry/remote"
"oras.land/oras-go/v2/registry/remote/auth"
"oras.land/oras-go/v2/registry/remote/errcode"

cache2 "go.linka.cloud/artifact-registry/pkg/cache"
"go.linka.cloud/artifact-registry/pkg/crypt/aes"
Expand Down Expand Up @@ -516,3 +517,27 @@ func (s *storage) MediaTypeRegistryLayerMetadata(name string) string {
func (s *storage) MediaTypeArtifactLayer() string {
return "application/vnd.lk.registry.layer.v1." + s.prov.Name()
}

func IsErrorCode(err error, code string) bool {
var ec errcode.Error
return errors.As(err, &ec) && ec.Code == code
}

func ErrCode(err error) int {
var ec *errcode.ErrorResponse
if errors.As(err, &ec) {
return ec.StatusCode
}
return http.StatusInternalServerError
}

func Error(w http.ResponseWriter, err error) {
switch {
case errors.Is(err, os.ErrExist):
http.Error(w, err.Error(), http.StatusConflict)
case errors.Is(err, os.ErrNotExist):
http.Error(w, err.Error(), http.StatusNotFound)
default:
http.Error(w, err.Error(), ErrCode(err))
}
}
2 changes: 1 addition & 1 deletion pkg/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func StorageMiddleware(ar Provider, backend string, key []byte) StorageMiddlewar
}
repo, err := NewStorage(ctx, backend, name, ar, key)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
Error(w, err)
return
}
defer repo.Close()
Expand Down

0 comments on commit 5c2a6dd

Please sign in to comment.