-
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.
improved create service error message (#312)
* improved create service error message * error pkg * revisions error handling * adding license * project rebuild * unit test improvements * build updates
- Loading branch information
1 parent
ffe80b9
commit c4e8d5a
Showing
9 changed files
with
315 additions
and
14 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,28 @@ | ||
// Copyright © 2019 The Knative Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
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,33 @@ | ||
// Copyright © 2019 The Knative Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package errors | ||
|
||
import ( | ||
"testing" | ||
|
||
"gotest.tools/assert" | ||
) | ||
|
||
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,51 @@ | ||
// Copyright © 2019 The Knative Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package errors | ||
|
||
import ( | ||
"strings" | ||
|
||
api_errors "k8s.io/apimachinery/pkg/api/errors" | ||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
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 | ||
} | ||
|
||
//Retrieves a custom error struct based on the original error APIStatus struct | ||
//Returns the original error struct in case it can't identify the kind of APIStatus error | ||
func GetError(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,94 @@ | ||
// Copyright © 2019 The Knative Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package errors | ||
|
||
import ( | ||
"testing" | ||
|
||
"gotest.tools/assert" | ||
api_errors "k8s.io/apimachinery/pkg/api/errors" | ||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
) | ||
|
||
func TestBuild(t *testing.T) { | ||
cases := []struct { | ||
Name string | ||
Schema schema.GroupResource | ||
StatusError func(schema.GroupResource) *api_errors.StatusError | ||
ExpectedMsg string | ||
Validate func(t *testing.T, err error, msg string) | ||
}{ | ||
{ | ||
Name: "Should get a missing serving api error", | ||
Schema: schema.GroupResource{ | ||
Group: "serving.knative.dev", | ||
Resource: "service", | ||
}, | ||
StatusError: func(resource schema.GroupResource) *api_errors.StatusError { | ||
statusError := api_errors.NewNotFound(resource, "serv") | ||
statusError.Status().Details.Causes = []v1.StatusCause{ | ||
{ | ||
Type: "UnexpectedServerResponse", | ||
Message: "404 page not found", | ||
}, | ||
} | ||
return statusError | ||
}, | ||
ExpectedMsg: "no Knative serving API found on the backend. Please verify the installation.", | ||
Validate: func(t *testing.T, err error, msg string) { | ||
assert.Error(t, err, msg) | ||
}, | ||
}, | ||
{ | ||
Name: "Should get the default not found error", | ||
Schema: schema.GroupResource{ | ||
Group: "serving.knative.dev", | ||
Resource: "service", | ||
}, | ||
StatusError: func(resource schema.GroupResource) *api_errors.StatusError { | ||
return api_errors.NewNotFound(resource, "serv") | ||
}, | ||
ExpectedMsg: "service.serving.knative.dev \"serv\" not found", | ||
Validate: func(t *testing.T, err error, msg string) { | ||
assert.Error(t, err, msg) | ||
}, | ||
}, | ||
{ | ||
Name: "Should return the original error", | ||
Schema: schema.GroupResource{ | ||
Group: "serving.knative.dev", | ||
Resource: "service", | ||
}, | ||
StatusError: func(resource schema.GroupResource) *api_errors.StatusError { | ||
return api_errors.NewAlreadyExists(resource, "serv") | ||
}, | ||
ExpectedMsg: "service.serving.knative.dev \"serv\" already exists", | ||
Validate: func(t *testing.T, err error, msg string) { | ||
assert.Error(t, err, msg) | ||
}, | ||
}, | ||
} | ||
|
||
for _, tc := range cases { | ||
tc := tc | ||
t.Run(tc.Name, func(t *testing.T) { | ||
t.Parallel() | ||
statusError := tc.StatusError(tc.Schema) | ||
err := GetError(statusError) | ||
tc.Validate(t, err, tc.ExpectedMsg) | ||
}) | ||
} | ||
} |
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,25 @@ | ||
// Copyright © 2019 The Knative Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
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,37 @@ | ||
// Copyright © 2019 The Knative Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package errors | ||
|
||
import ( | ||
"testing" | ||
|
||
"gotest.tools/assert" | ||
) | ||
|
||
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,22 @@ | ||
// Copyright © 2019 The Knative Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
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
Oops, something went wrong.