-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
module command: Add Error Code for Kyma Warning State (#1692)
* module command: Add Error Code for Kyma Warning State * Fix Linting Issues
- Loading branch information
1 parent
03423bf
commit f92606a
Showing
10 changed files
with
115 additions
and
11 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package module | ||
|
||
import "errors" | ||
|
||
var ErrKymaInWarningState = errors.New("kyma CR in warning state 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package cli | ||
|
||
import ( | ||
"github.com/avast/retry-go" | ||
"github.com/kyma-project/cli/internal/cli/alpha/module" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
var ErrorCodeMap = map[error]int{ | ||
module.ErrKymaInWarningState: 2, | ||
} | ||
|
||
func GetExitCode(err error) int { | ||
switch err := err.(type) { | ||
default: | ||
return handleSingleError(err) | ||
case retry.Error: | ||
return handleListOfErrors(err) | ||
} | ||
} | ||
|
||
func handleSingleError(err error) int { | ||
if errorCode, found := mapNestedErrorToCode(err); found { | ||
return errorCode | ||
} | ||
return 1 | ||
} | ||
|
||
func handleListOfErrors(errorList retry.Error) int { | ||
for _, err := range errorList { | ||
if errorCode, found := mapNestedErrorToCode(err); found { | ||
return errorCode | ||
} | ||
} | ||
return 1 | ||
} | ||
|
||
func mapNestedErrorToCode(err error) (int, bool) { | ||
for { | ||
if errorCode, ok := ErrorCodeMap[err]; ok { | ||
return errorCode, true | ||
} | ||
err = errors.Unwrap(err) | ||
if err == nil { | ||
return -1, false | ||
} | ||
} | ||
} |
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 cli | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"github.com/avast/retry-go" | ||
"github.com/kyma-project/cli/internal/cli/alpha/module" | ||
"testing" | ||
) | ||
|
||
func TestGetExitCode(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
err error | ||
want int | ||
}{ | ||
{ | ||
name: "correct exit code for single error", | ||
err: module.ErrKymaInWarningState, | ||
want: ErrorCodeMap[module.ErrKymaInWarningState], | ||
}, | ||
{ | ||
name: "correct exit code for wrapped error", | ||
err: fmt.Errorf("wrapped error 2: %w", | ||
fmt.Errorf("wrapped error 1: %w", | ||
module.ErrKymaInWarningState)), | ||
want: ErrorCodeMap[module.ErrKymaInWarningState], | ||
}, | ||
{ | ||
name: "correct exit code for error in a list", | ||
err: retry.Error([]error{ | ||
errors.New("random non-mapped error"), | ||
module.ErrKymaInWarningState, | ||
}), | ||
want: ErrorCodeMap[module.ErrKymaInWarningState], | ||
}, | ||
{ | ||
name: "default exit code for non-mapped error", | ||
err: errors.New("random non-mapped error"), | ||
want: 1, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := GetExitCode(tt.err); got != tt.want { | ||
t.Errorf("GetExitCode() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |