-
Notifications
You must be signed in to change notification settings - Fork 574
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add generic catalog error for generic golang error
- Loading branch information
1 parent
619bc08
commit a371db5
Showing
3 changed files
with
86 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,34 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"os/exec" | ||
|
||
"github.com/snyk/error-catalog-golang-public/cli" | ||
"github.com/snyk/error-catalog-golang-public/snyk_errors" | ||
|
||
cli_errors "github.com/snyk/cli/cliv2/internal/errors" | ||
) | ||
|
||
// decorate generic errors that do not contain Error-Catalog Errors | ||
func decorateError(err error) error { | ||
if err == nil { | ||
return err | ||
} | ||
|
||
if _, isExitError := err.(*exec.ExitError); isExitError { | ||
return err | ||
} | ||
|
||
if _, isErrorWithCode := err.(*cli_errors.ErrorWithExitCode); isErrorWithCode { | ||
return err | ||
} | ||
|
||
var errorCatalogError snyk_errors.Error | ||
if !errors.As(err, &errorCatalogError) { | ||
genericError := cli.NewGeneralCLIFailureError(err.Error()) | ||
genericError.StatusCode = 0 | ||
err = errors.Join(err, genericError) | ||
} | ||
return err | ||
} |
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,47 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"os" | ||
"os/exec" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/snyk/error-catalog-golang-public/cli" | ||
|
||
cli_errors "github.com/snyk/cli/cliv2/internal/errors" | ||
) | ||
|
||
func Test_decorateError(t *testing.T) { | ||
t.Run("is nil error", func(t *testing.T) { | ||
assert.Nil(t, decorateError(nil)) | ||
}) | ||
|
||
t.Run("is ErrorWithExitCode", func(t *testing.T) { | ||
err := &cli_errors.ErrorWithExitCode{ | ||
ExitCode: 2, | ||
} | ||
assert.Equal(t, err, decorateError(err)) | ||
}) | ||
|
||
t.Run("is ExitError", func(t *testing.T) { | ||
err := &exec.ExitError{ | ||
ProcessState: &os.ProcessState{}, | ||
} | ||
assert.Equal(t, err, decorateError(err)) | ||
}) | ||
|
||
t.Run("is already error catalog error", func(t *testing.T) { | ||
err := cli.NewConnectionTimeoutError("") | ||
assert.Equal(t, err, decorateError(err)) | ||
}) | ||
|
||
t.Run("is a generic error", func(t *testing.T) { | ||
err := errors.New("generic error") | ||
actualErrr := decorateError(err) | ||
expectedError := cli.NewGeneralCLIFailureError("") | ||
assert.ErrorIs(t, actualErrr, err) | ||
assert.ErrorAs(t, actualErrr, &expectedError) | ||
}) | ||
} |
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