Skip to content

Commit 6cafcee

Browse files
committed
feat: return proper gRPC error codes for expected conditions
Port of upstream PR cloudnative-pg#549. WAL not found returns NotFound, end of WAL stream returns OutOfRange, missing permissions returns FailedPrecondition.
1 parent 8398383 commit 6cafcee

File tree

2 files changed

+15
-16
lines changed

2 files changed

+15
-16
lines changed

internal/cnpgi/common/errors.go

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

3-
// walNotFoundError is raised when a WAL file has not been found in the object store
4-
type walNotFoundError struct{}
3+
import (
4+
"google.golang.org/grpc/codes"
5+
"google.golang.org/grpc/status"
6+
)
57

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
8+
// newWALNotFoundError creates a gRPC NotFound error for a missing WAL file
9+
func newWALNotFoundError() error {
10+
return status.Error(codes.NotFound, "WAL file not found")
1111
}
1212

13-
// Error implements the error interface
14-
func (e walNotFoundError) Error() string {
15-
return "WAL file not found"
16-
}
13+
// ErrEndOfWALStreamReached is returned when end of WAL is detected in the cloud archive.
14+
var ErrEndOfWALStreamReached = status.Error(codes.OutOfRange, "end of WAL reached")
15+
16+
// ErrMissingPermissions is returned when the plugin doesn't have the required permissions.
17+
var ErrMissingPermissions = status.Error(codes.FailedPrecondition,
18+
"backup credentials don't yet have access permissions. Will retry reconciliation loop")

internal/cnpgi/common/wal.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ func (w WALServiceImplementation) Archive(
109109
utils.SanitizedEnviron())
110110
if err != nil {
111111
if apierrors.IsForbidden(err) {
112-
return nil, errors.New("backup credentials don't yet have access permissions. Will retry reconciliation loop")
112+
return nil, ErrMissingPermissions
113113
}
114114
return nil, err
115115
}
@@ -394,7 +394,7 @@ func (w WALServiceImplementation) Status(
394394
utils.SanitizedEnviron())
395395
if err != nil {
396396
if apierrors.IsForbidden(err) {
397-
return nil, errors.New("backup credentials don't yet have access permissions. Will retry reconciliation loop")
397+
return nil, ErrMissingPermissions
398398
}
399399
return nil, err
400400
}
@@ -489,9 +489,6 @@ func gatherWALFilesToRestore(walName string, parallel int, controlledPromotion b
489489
return walList, err
490490
}
491491

492-
// ErrEndOfWALStreamReached is returned when end of WAL is detected in the cloud archive.
493-
var ErrEndOfWALStreamReached = errors.New("end of WAL reached")
494-
495492
// checkEndOfWALStreamFlag returns ErrEndOfWALStreamReached if the flag is set in the restorer.
496493
func checkEndOfWALStreamFlag(walRestorer *pgbackrestRestorer.WALRestorer) error {
497494
contain, err := walRestorer.IsEndOfWALStream()

0 commit comments

Comments
 (0)