Skip to content

Commit 4d99509

Browse files
committed
Fix error swallowing behavior
1 parent d7db764 commit 4d99509

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

internal/cmd/backup.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,20 @@ const (
4242
// cobraRunEFunc is the signature of a cobra.Command.RunE function.
4343
type cobraRunEFunc = func(cmd *cobra.Command, args []string) (err error)
4444

45+
// TODO: apply these to other cmds as well
46+
var errorHandlers = []func(error) error{
47+
addPermissionDeniedErrInfo,
48+
addSizeErrInfo,
49+
}
50+
4551
// withErrorHandling is a wrapper that centralizes error handling, instead of having to scatter it around the command logic.
4652
func withErrorHandling(f cobraRunEFunc) cobraRunEFunc {
4753
return func(cmd *cobra.Command, args []string) (err error) {
48-
return addSizeErrInfo(f(cmd, args))
54+
cmdErr := f(cmd, args)
55+
for _, handler := range errorHandlers {
56+
cmdErr = handler(cmdErr)
57+
}
58+
return cmdErr
4959
}
5060
}
5161

@@ -398,6 +408,9 @@ func backupCreateCmdFunc(cmd *cobra.Command, args []string) (err error) {
398408
}
399409
return nil
400410
})
411+
if err != nil {
412+
return err
413+
}
401414

402415
backupCompleted = true
403416
return nil
@@ -902,3 +915,15 @@ func addSizeErrInfo(err error) error {
902915

903916
return fmt.Errorf("%w: set flag --max-message-size=%d to increase the maximum allowable size", err, 2*necessaryByteCount)
904917
}
918+
919+
func addPermissionDeniedErrInfo(err error) error {
920+
if err == nil {
921+
return nil
922+
}
923+
924+
code := status.Code(err)
925+
if code != codes.PermissionDenied {
926+
return err
927+
}
928+
return fmt.Errorf("%w: ensure that the token used for this call has all requisite permissions")
929+
}

internal/cmd/backup_test.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,9 +281,13 @@ func TestBackupCreateCmdFunc(t *testing.T) {
281281

282282
ctx := t.Context()
283283
srv := zedtesting.NewTestServer(ctx, t)
284+
runErrs := make(chan error, 1)
284285
go func() {
285-
require.NoError(t, srv.Run(ctx))
286+
err := srv.Run(ctx)
287+
runErrs <- err
286288
}()
289+
err := <-runErrs
290+
require.NoError(t, err)
287291
conn, err := srv.GRPCDialContext(ctx)
288292
require.NoError(t, err)
289293

@@ -518,9 +522,13 @@ func TestBackupRestoreCmdFunc(t *testing.T) {
518522

519523
ctx := t.Context()
520524
srv := zedtesting.NewTestServer(ctx, t)
525+
runErrs := make(chan error, 1)
521526
go func() {
522-
require.NoError(t, srv.Run(ctx))
527+
err := srv.Run(ctx)
528+
runErrs <- err
523529
}()
530+
err := <-runErrs
531+
require.NoError(t, err)
524532
conn, err := srv.GRPCDialContext(ctx)
525533
require.NoError(t, err)
526534

0 commit comments

Comments
 (0)