Skip to content

Commit e5a0aee

Browse files
leonardocemnencia
authored andcommitted
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 <[email protected]>
1 parent 65a0d11 commit e5a0aee

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

internal/cnpgi/common/errors.go

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
package common
22

3-
// walNotFoundError is raised when a WAL file has not been found in the object store
4-
type walNotFoundError struct{}
5-
6-
func newWALNotFoundError() *walNotFoundError { return &walNotFoundError{} }
7-
8-
// ShouldPrintStackTrace tells whether the sidecar log stream should contain the stack trace
9-
func (e walNotFoundError) ShouldPrintStackTrace() bool {
10-
return false
11-
}
12-
13-
// Error implements the error interface
14-
func (e walNotFoundError) Error() string {
15-
return "WAL file not found"
3+
import (
4+
"google.golang.org/grpc/codes"
5+
"google.golang.org/grpc/status"
6+
)
7+
8+
// newWALNotFoundError returns a error that states that a
9+
// certain WAL file has not been found. This error is
10+
// compatible with GRPC status codes, resulting in a 404
11+
// being used as a response code.
12+
func newWALNotFoundError(walName string) error {
13+
return status.Errorf(codes.NotFound, "wal %q not found", walName)
1614
}

internal/cnpgi/common/wal.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import (
1818
"github.com/cloudnative-pg/machinery/pkg/fileutils"
1919
walUtils "github.com/cloudnative-pg/machinery/pkg/fileutils/wals"
2020
"github.com/cloudnative-pg/machinery/pkg/log"
21+
"google.golang.org/grpc/codes"
22+
"google.golang.org/grpc/status"
2123
apierrors "k8s.io/apimachinery/pkg/api/errors"
2224
"k8s.io/apimachinery/pkg/types"
2325
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -350,7 +352,7 @@ func (w WALServiceImplementation) restoreFromBarmanObjectStore(
350352
// The failure has already been logged in walRestorer.RestoreList method
351353
if walStatus[0].Err != nil {
352354
if errors.Is(walStatus[0].Err, barmanRestorer.ErrWALNotFound) {
353-
return newWALNotFoundError()
355+
return newWALNotFoundError(walStatus[0].WalName)
354356
}
355357

356358
return walStatus[0].Err
@@ -458,7 +460,7 @@ func gatherWALFilesToRestore(walName string, parallel int) (walList []string, er
458460
}
459461

460462
// ErrEndOfWALStreamReached is returned when end of WAL is detected in the cloud archive.
461-
var ErrEndOfWALStreamReached = errors.New("end of WAL reached")
463+
var ErrEndOfWALStreamReached = status.Errorf(codes.NotFound, "end of WAL reached")
462464

463465
// checkEndOfWALStreamFlag returns ErrEndOfWALStreamReached if the flag is set in the restorer.
464466
func checkEndOfWALStreamFlag(walRestorer *barmanRestorer.WALRestorer) error {

0 commit comments

Comments
 (0)