Skip to content

Commit d9ee2a0

Browse files
Fixed the exit status being returned in luksFormat function instead of actual error
1 parent 3cd1126 commit d9ee2a0

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed

utils/devices/luks/luks_linux.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func (d *LUKSDevice) luksFormat(ctx context.Context, luksPassphrase string) erro
108108
"device": device,
109109
"output": string(output),
110110
}).WithError(err).Error("Failed to format LUKS device.")
111-
return fmt.Errorf("could not format LUKS device; %w", err)
111+
return errors.FormatError(fmt.Errorf("could not format LUKS device; %v", string(output)))
112112
}
113113

114114
return nil

utils/errors/errors.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -989,3 +989,24 @@ func IsNotManagedError(err error) bool {
989989
var errPtr *notManagedError
990990
return errors.As(err, &errPtr)
991991
}
992+
993+
// ///////////////////////////////////////////////////////////////////////////
994+
// formatError
995+
// ///////////////////////////////////////////////////////////////////////////
996+
997+
type formatError struct {
998+
message string
999+
}
1000+
1001+
func (f *formatError) Error() string { return f.message }
1002+
1003+
func FormatError(err error) error {
1004+
return &formatError{
1005+
fmt.Sprintf("Formatting failed; %s", err.Error()),
1006+
}
1007+
}
1008+
1009+
func IsFormatError(err error) bool {
1010+
var f *formatError
1011+
return errors.As(err, &f)
1012+
}

utils/errors/errors_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,3 +459,43 @@ func TestReconcileFailedError(t *testing.T) {
459459
assert.True(t, IsReconcileFailedError(err))
460460
assert.Equal(t, "outer; inner; ", err.Error())
461461
}
462+
463+
func TestFormatError(t *testing.T) {
464+
err := FormatError(fmt.Errorf("formatting error"))
465+
assert.True(t, IsFormatError(err))
466+
assert.Equal(t, "Formatting failed; formatting error", err.Error())
467+
}
468+
469+
func TestIsFormatError(t *testing.T) {
470+
formatErr := &formatError{
471+
message: "format error",
472+
}
473+
474+
tests := []struct {
475+
Name string
476+
Err error
477+
wantErr assert.BoolAssertionFunc
478+
}{
479+
{
480+
Name: "NotFormatError",
481+
Err: fmt.Errorf("a generic error"),
482+
wantErr: assert.False,
483+
},
484+
{
485+
Name: "FormatError",
486+
Err: formatErr,
487+
wantErr: assert.True,
488+
},
489+
{
490+
Name: "WrappedFormatError",
491+
Err: fmt.Errorf("wrapping formatError; %w", formatErr),
492+
wantErr: assert.True,
493+
},
494+
}
495+
496+
for _, tt := range tests {
497+
t.Run(tt.Name, func(t *testing.T) {
498+
tt.wantErr(t, IsFormatError(tt.Err), "Unexpected error")
499+
})
500+
}
501+
}

0 commit comments

Comments
 (0)