-
Notifications
You must be signed in to change notification settings - Fork 263
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
161 additions
and
7 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,14 @@ | ||
package errors | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
func newInvalidCRD(apiGroup string) *KNError { | ||
parts := strings.Split(apiGroup, ".") | ||
name := parts[0] | ||
msg := fmt.Sprintf("no Knative %s API found on the backend. Please verify the installation.", name) | ||
|
||
return NewKNError(msg) | ||
} |
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,18 @@ | ||
package errors | ||
|
||
import ( | ||
"gotest.tools/assert" | ||
"testing" | ||
) | ||
|
||
func TestNewInvalidCRD(t *testing.T) { | ||
err := newInvalidCRD("serving.knative.dev") | ||
assert.Error(t, err, "no Knative serving API found on the backend. Please verify the installation.") | ||
|
||
err = newInvalidCRD("serving") | ||
assert.Error(t, err, "no Knative serving API found on the backend. Please verify the installation.") | ||
|
||
err = newInvalidCRD("") | ||
assert.Error(t, err, "no Knative API found on the backend. Please verify the installation.") | ||
|
||
} |
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 errors | ||
|
||
import ( | ||
api_errors "k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"strings" | ||
) | ||
|
||
func isCRDError(status api_errors.APIStatus) bool { | ||
for _, cause := range status.Status().Details.Causes { | ||
if strings.HasPrefix(cause.Message, "404") && cause.Type == v1.CauseTypeUnexpectedServerResponse { | ||
return true | ||
} | ||
} | ||
|
||
return false | ||
} | ||
|
||
func Build(err error) error { | ||
apiStatus, ok := err.(api_errors.APIStatus) | ||
if !ok { | ||
return err | ||
} | ||
|
||
var knerr *KNError | ||
|
||
if isCRDError(apiStatus) { | ||
knerr = newInvalidCRD(apiStatus.Status().Details.Group) | ||
knerr.Status = apiStatus | ||
return knerr | ||
} | ||
|
||
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,43 @@ | ||
package errors | ||
|
||
import ( | ||
"github.com/pkg/errors" | ||
"gotest.tools/assert" | ||
api_errors "k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
"testing" | ||
) | ||
|
||
func TestBuild(t *testing.T) { | ||
//default non api error | ||
defaultError := errors.New("my-custom-error") | ||
err := Build(defaultError) | ||
assert.Error(t, err, "my-custom-error") | ||
|
||
gv := schema.GroupResource{ | ||
Group: "serving.knative.dev", | ||
Resource: "service", | ||
} | ||
|
||
//api error containing expected error when knative crd is not available | ||
apiError := api_errors.NewNotFound(gv, "serv") | ||
apiError.Status().Details.Causes = []v1.StatusCause{ | ||
{ | ||
Type: "UnexpectedServerResponse", | ||
Message: "404 page not found", | ||
}, | ||
} | ||
err = Build(apiError) | ||
assert.Error(t, err, "no Knative serving API found on the backend. Please verify the installation.") | ||
|
||
//api error not registered in error factory | ||
apiError = api_errors.NewAlreadyExists(gv, "serv") | ||
err = Build(apiError) | ||
assert.Error(t, err, "service.serving.knative.dev \"serv\" already exists") | ||
|
||
//default not found api error | ||
apiError = api_errors.NewNotFound(gv, "serv") | ||
err = Build(apiError) | ||
assert.Error(t, err, "service.serving.knative.dev \"serv\" not found") | ||
} |
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,11 @@ | ||
package errors | ||
|
||
func NewKNError(msg string) *KNError { | ||
return &KNError{ | ||
msg: msg, | ||
} | ||
} | ||
|
||
func (kne *KNError) Error() string { | ||
return kne.msg | ||
} |
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,22 @@ | ||
package errors | ||
|
||
import ( | ||
"gotest.tools/assert" | ||
"testing" | ||
) | ||
|
||
func TestNewKNError(t *testing.T) { | ||
err := NewKNError("myerror") | ||
assert.Error(t, err, "myerror") | ||
|
||
err = NewKNError("") | ||
assert.Error(t, err, "") | ||
} | ||
|
||
func TestKNError_Error(t *testing.T) { | ||
err := NewKNError("myerror") | ||
assert.Equal(t, err.Error(), "myerror") | ||
|
||
err = NewKNError("") | ||
assert.Equal(t, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package errors | ||
|
||
import api_errors "k8s.io/apimachinery/pkg/api/errors" | ||
|
||
type KNError struct { | ||
Status api_errors.APIStatus | ||
msg string | ||
} |
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