From f8857b995c467ffeed252335492678e1d985a076 Mon Sep 17 00:00:00 2001 From: oppiliappan Date: Tue, 9 Sep 2025 09:14:35 +0100 Subject: [PATCH] xrpc,knotserver: refactor RepoNotFound errors Change-Id: uvszkkvoxroswxktwrronwltlkzszzzk Signed-off-by: oppiliappan --- knotserver/xrpc/repo_branch.go | 5 +---- knotserver/xrpc/repo_branches.go | 5 +---- knotserver/xrpc/repo_compare.go | 5 +---- knotserver/xrpc/repo_tags.go | 5 +---- knotserver/xrpc/xrpc.go | 10 ++-------- xrpc/errors/errors.go | 5 +++++ 6 files changed, 11 insertions(+), 24 deletions(-) diff --git a/knotserver/xrpc/repo_branch.go b/knotserver/xrpc/repo_branch.go index 18828733..c85b40bb 100644 --- a/knotserver/xrpc/repo_branch.go +++ b/knotserver/xrpc/repo_branch.go @@ -32,10 +32,7 @@ func (x *Xrpc) RepoBranch(w http.ResponseWriter, r *http.Request) { gr, err := git.PlainOpen(repoPath) if err != nil { - writeError(w, xrpcerr.NewXrpcError( - xrpcerr.WithTag("RepoNotFound"), - xrpcerr.WithMessage("repository not found"), - ), http.StatusNotFound) + writeError(w, xrpcerr.RepoNotFoundError, http.StatusNoContent) return } diff --git a/knotserver/xrpc/repo_branches.go b/knotserver/xrpc/repo_branches.go index 1e3e748f..f8c82487 100644 --- a/knotserver/xrpc/repo_branches.go +++ b/knotserver/xrpc/repo_branches.go @@ -31,10 +31,7 @@ func (x *Xrpc) RepoBranches(w http.ResponseWriter, r *http.Request) { gr, err := git.PlainOpen(repoPath) if err != nil { - writeError(w, xrpcerr.NewXrpcError( - xrpcerr.WithTag("RepoNotFound"), - xrpcerr.WithMessage("repository not found"), - ), http.StatusNotFound) + writeError(w, xrpcerr.RepoNotFoundError, http.StatusNoContent) return } diff --git a/knotserver/xrpc/repo_compare.go b/knotserver/xrpc/repo_compare.go index e08d8aa4..6444b5f9 100644 --- a/knotserver/xrpc/repo_compare.go +++ b/knotserver/xrpc/repo_compare.go @@ -38,10 +38,7 @@ func (x *Xrpc) RepoCompare(w http.ResponseWriter, r *http.Request) { gr, err := git.PlainOpen(repoPath) if err != nil { - writeError(w, xrpcerr.NewXrpcError( - xrpcerr.WithTag("RepoNotFound"), - xrpcerr.WithMessage("repository not found"), - ), http.StatusNotFound) + writeError(w, xrpcerr.RepoNotFoundError, http.StatusNoContent) return } diff --git a/knotserver/xrpc/repo_tags.go b/knotserver/xrpc/repo_tags.go index 5eeec576..3da65499 100644 --- a/knotserver/xrpc/repo_tags.go +++ b/knotserver/xrpc/repo_tags.go @@ -33,10 +33,7 @@ func (x *Xrpc) RepoTags(w http.ResponseWriter, r *http.Request) { gr, err := git.PlainOpen(repoPath) if err != nil { x.Logger.Error("failed to open", "error", err) - writeError(w, xrpcerr.NewXrpcError( - xrpcerr.WithTag("RepoNotFound"), - xrpcerr.WithMessage("repository not found"), - ), http.StatusNoContent) + writeError(w, xrpcerr.RepoNotFoundError, http.StatusNoContent) return } diff --git a/knotserver/xrpc/xrpc.go b/knotserver/xrpc/xrpc.go index ea2451bc..a6e4192a 100644 --- a/knotserver/xrpc/xrpc.go +++ b/knotserver/xrpc/xrpc.go @@ -101,18 +101,12 @@ func (x *Xrpc) parseRepoParam(repo string) (string, error) { // Construct repository path using the same logic as didPath didRepoPath, err := securejoin.SecureJoin(did, repoName) if err != nil { - return "", xrpcerr.NewXrpcError( - xrpcerr.WithTag("RepoNotFound"), - xrpcerr.WithMessage("failed to access repository"), - ) + return "", xrpcerr.RepoNotFoundError } repoPath, err := securejoin.SecureJoin(x.Config.Repo.ScanPath, didRepoPath) if err != nil { - return "", xrpcerr.NewXrpcError( - xrpcerr.WithTag("RepoNotFound"), - xrpcerr.WithMessage("failed to access repository"), - ) + return "", xrpcerr.RepoNotFoundError } return repoPath, nil diff --git a/xrpc/errors/errors.go b/xrpc/errors/errors.go index 0aa1e8b9..fc12445c 100644 --- a/xrpc/errors/errors.go +++ b/xrpc/errors/errors.go @@ -56,6 +56,11 @@ var OwnerNotFoundError = NewXrpcError( WithMessage("owner not set for this service"), ) +var RepoNotFoundError = NewXrpcError( + WithTag("RepoNotFound"), + WithMessage("failed to access repository"), +) + var AuthError = func(err error) XrpcError { return NewXrpcError( WithTag("Auth"), -- 2.43.0