forked from kyma-project/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement better error handling (kyma-project#2013)
* Implement better error handling * test * Simplify struct name * make test a bit mor readable * fix formatting issues * info * remove pleonasm
- Loading branch information
Showing
5 changed files
with
171 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package clierror | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
) | ||
|
||
type Error struct { | ||
Message string | ||
Details string | ||
Hints []string | ||
} | ||
|
||
// Wrap adds a new message and hints to the error | ||
func (e *Error) Wrap(message string, hints []string) *Error { | ||
newError := &Error{ | ||
Message: message, | ||
Details: e.Message, | ||
Hints: append(hints, e.Hints...), | ||
} | ||
|
||
if e.Details != "" { | ||
newError.Details = fmt.Sprintf("%s: %s", e.Message, e.Details) | ||
} | ||
|
||
return newError | ||
} | ||
|
||
func (e *Error) Print() { | ||
fmt.Printf("%s\n", e.Error()) | ||
} | ||
|
||
func (e *Error) PrintStderr() { | ||
fmt.Fprintf(os.Stderr, "%s\n", e.Error()) | ||
} | ||
|
||
// Error returns the error string, compatible with the error interface | ||
func (e Error) Error() string { | ||
output := fmt.Sprintf("Error:\n %s\n\n", e.Message) | ||
if e.Details != "" { | ||
output += fmt.Sprintf("Error Details:\n %s\n\n", e.Details) | ||
} | ||
if len(e.Hints) > 0 { | ||
output += "Hints:\n" | ||
for _, hint := range e.Hints { | ||
output += fmt.Sprintf(" - %s\n", hint) | ||
} | ||
} | ||
return output | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package clierror | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_CLIError_String(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
err Error | ||
want string | ||
}{ | ||
{ | ||
name: "empty", | ||
err: Error{}, | ||
want: "Error:\n \n\n", | ||
}, | ||
{ | ||
name: "error", | ||
err: Error{ | ||
Message: "error", | ||
}, | ||
want: "Error:\n error\n\n", | ||
}, | ||
{ | ||
name: "error and details", | ||
err: Error{ | ||
Message: "error", | ||
Details: "details", | ||
}, | ||
want: "Error:\n error\n\nError Details:\n details\n\n", | ||
}, | ||
{ | ||
name: "error, details and hints", | ||
err: Error{ | ||
Message: "error", | ||
Details: "details", | ||
Hints: []string{"hint1", "hint2"}, | ||
}, | ||
want: "Error:\n error\n\nError Details:\n details\n\nHints:\n - hint1\n - hint2\n", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
assert.Equal(t, tt.want, tt.err.Error()) | ||
}) | ||
} | ||
} | ||
|
||
// test Wrap function | ||
func Test_CLIError_Wrap(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
err Error | ||
message string | ||
hints []string | ||
want string | ||
}{ | ||
{ | ||
name: "Add to empty error", | ||
err: Error{}, | ||
message: "error", | ||
want: "Error:\n error\n\n", | ||
}, | ||
{ | ||
name: "Add with hints to empty error", | ||
err: Error{}, | ||
message: "error", | ||
hints: []string{"hint1", "hint2"}, | ||
want: "Error:\n error\n\nHints:\n - hint1\n - hint2\n", | ||
}, | ||
{ | ||
name: "add to error", | ||
err: Error{ | ||
Message: "error", | ||
}, | ||
message: "error", | ||
hints: []string{"hint1", "hint2"}, | ||
want: "Error:\n error\n\nError Details:\n error\n\nHints:\n - hint1\n - hint2\n", | ||
}, | ||
{ | ||
name: "add to error with details", | ||
err: Error{ | ||
Message: "previous", | ||
Details: "details", | ||
}, | ||
message: "error", | ||
hints: []string{"hint1", "hint2"}, | ||
want: "Error:\n error\n\nError Details:\n previous: details\n\nHints:\n - hint1\n - hint2\n", | ||
}, | ||
{ | ||
name: "add to error with details and hints", | ||
err: Error{ | ||
Message: "previous", | ||
Details: "details", | ||
Hints: []string{"hint1", "hint2"}, | ||
}, | ||
message: "error", | ||
hints: []string{"hint3", "hint4"}, | ||
want: "Error:\n error\n\nError Details:\n previous: details\n\nHints:\n - hint3\n - hint4\n - hint1\n - hint2\n", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
err := tt.err.Wrap(tt.message, tt.hints) | ||
assert.Equal(t, tt.want, err.Error()) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters