From e5a0aeed347cb4a5f28fb8782e3fa87849e0842d Mon Sep 17 00:00:00 2001 From: Leonardo Cecchi Date: Wed, 24 Sep 2025 06:51:23 +0200 Subject: [PATCH] feat: return 404 for missing WAL files The plugin now returns a 404 status code when a requested WAL file does not exist in the object store. This prevents misleading log entries such as "Error while handling gRPC request" for expected missing-file scenarios. Signed-off-by: Leonardo Cecchi --- internal/cnpgi/common/errors.go | 22 ++++++++++------------ internal/cnpgi/common/wal.go | 6 ++++-- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/internal/cnpgi/common/errors.go b/internal/cnpgi/common/errors.go index 48b2cde..e475002 100644 --- a/internal/cnpgi/common/errors.go +++ b/internal/cnpgi/common/errors.go @@ -1,16 +1,14 @@ package common -// walNotFoundError is raised when a WAL file has not been found in the object store -type walNotFoundError struct{} +import ( + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" +) -func newWALNotFoundError() *walNotFoundError { return &walNotFoundError{} } - -// ShouldPrintStackTrace tells whether the sidecar log stream should contain the stack trace -func (e walNotFoundError) ShouldPrintStackTrace() bool { - return false -} - -// Error implements the error interface -func (e walNotFoundError) Error() string { - return "WAL file not found" +// newWALNotFoundError returns a error that states that a +// certain WAL file has not been found. This error is +// compatible with GRPC status codes, resulting in a 404 +// being used as a response code. +func newWALNotFoundError(walName string) error { + return status.Errorf(codes.NotFound, "wal %q not found", walName) } diff --git a/internal/cnpgi/common/wal.go b/internal/cnpgi/common/wal.go index 5f6c856..46fa501 100644 --- a/internal/cnpgi/common/wal.go +++ b/internal/cnpgi/common/wal.go @@ -18,6 +18,8 @@ import ( "github.com/cloudnative-pg/machinery/pkg/fileutils" walUtils "github.com/cloudnative-pg/machinery/pkg/fileutils/wals" "github.com/cloudnative-pg/machinery/pkg/log" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" apierrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/types" "sigs.k8s.io/controller-runtime/pkg/client" @@ -350,7 +352,7 @@ func (w WALServiceImplementation) restoreFromBarmanObjectStore( // The failure has already been logged in walRestorer.RestoreList method if walStatus[0].Err != nil { if errors.Is(walStatus[0].Err, barmanRestorer.ErrWALNotFound) { - return newWALNotFoundError() + return newWALNotFoundError(walStatus[0].WalName) } return walStatus[0].Err @@ -458,7 +460,7 @@ func gatherWALFilesToRestore(walName string, parallel int) (walList []string, er } // ErrEndOfWALStreamReached is returned when end of WAL is detected in the cloud archive. -var ErrEndOfWALStreamReached = errors.New("end of WAL reached") +var ErrEndOfWALStreamReached = status.Errorf(codes.NotFound, "end of WAL reached") // checkEndOfWALStreamFlag returns ErrEndOfWALStreamReached if the flag is set in the restorer. func checkEndOfWALStreamFlag(walRestorer *barmanRestorer.WALRestorer) error {