-
Notifications
You must be signed in to change notification settings - Fork 364
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor antctl error message (#509)
Turn error message into a more friendly one, including http.StatusNotFound, http.StatusInternalServerError.
- Loading branch information
Showing
4 changed files
with
104 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
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,42 @@ | ||
// Copyright 2020 Antrea 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 antctl | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
"k8s.io/client-go/rest" | ||
) | ||
|
||
func generateMessage(opt *requestOption, result rest.Result) error { | ||
var code int | ||
_ = result.StatusCode(&code) | ||
if errMsg := generate(opt.commandDefinition, opt.args, code); errMsg != nil { | ||
return errMsg | ||
} | ||
return result.Error() | ||
} | ||
|
||
func generate(cd *commandDefinition, args map[string]string, code int) error { | ||
switch code { | ||
case http.StatusNotFound: | ||
return fmt.Errorf("NotFound: %s \"%s\" not found", cd.use, args["name"]) | ||
case http.StatusInternalServerError: | ||
return fmt.Errorf("InternalServerError: Encoding response failed for %s", cd.use) | ||
default: | ||
return fmt.Errorf("Unknown 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,59 @@ | ||
// Copyright 2020 Antrea 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 antctl | ||
|
||
import ( | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestGenerate(t *testing.T) { | ||
for _, tc := range []struct { | ||
cd *commandDefinition | ||
args map[string]string | ||
code int | ||
expected string | ||
}{ | ||
{ | ||
cd: &commandDefinition{ | ||
use: "foo", | ||
}, | ||
args: map[string]string{ | ||
"name": "bar", | ||
}, | ||
code: http.StatusNotFound, | ||
expected: "NotFound: foo \"bar\" not found", | ||
}, | ||
{ | ||
cd: &commandDefinition{ | ||
use: "foo", | ||
}, | ||
args: map[string]string{}, | ||
code: http.StatusInternalServerError, | ||
expected: "InternalServerError: Encoding response failed for foo", | ||
}, | ||
{ | ||
cd: &commandDefinition{}, | ||
args: map[string]string{}, | ||
code: http.StatusOK, | ||
expected: "Unknown error", | ||
}, | ||
} { | ||
generated := generate(tc.cd, tc.args, tc.code) | ||
assert.Equal(t, tc.expected, generated.Error()) | ||
} | ||
} |