Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

usability: Handle Exceptions for no resources #103

Merged
merged 4 commits into from
Sep 6, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions pkg/blockdevice/blockdevice.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ func createTreeByNode(k *client.K8sClient, bdNames []string) error {
// Add an empty row so that the tree looks neat
rows = append(rows, metav1.TableRow{Cells: []interface{}{"", "", "", "", "", "", ""}})
}
if len(rows) == 0 {
return util.HandleEmptyTableError("Block Device", k.Ns, "")
}
// Show the output using cli-runtime
util.TablePrinter(util.BDTreeListColumnDefinations, rows, printers.PrintOptions{Wide: true})
return nil
Expand Down
3 changes: 1 addition & 2 deletions pkg/storage/lvmlocalpv.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ func GetVolumeGroups(c *client.K8sClient, vgs []string) ([]metav1.TableColumnDef
}
// 3. Actually print the table or return an error
if len(rows) == 0 {
// TODO: Improve this in issue #56
return nil, nil, fmt.Errorf("no lvm volumegroups found")
return nil, nil, util.HandleEmptyTableError("lvm Volumegroups", c.Ns, "")
}
return util.LVMvolgroupListColumnDefinitions, rows, nil
}
Expand Down
11 changes: 11 additions & 0 deletions pkg/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,31 @@ func Get(pools []string, openebsNS string, casType string) error {
if err != nil {
return err
}
if len(rows) == 0 {
return util.HandleEmptyTableError("Storage", openebsNS, casType)
}
util.TablePrinter(header, rows, printers.PrintOptions{Wide: true})
} else if casType != "" {
return fmt.Errorf("cas-type %s is not supported", casType)
} else {
storageResourcesFound := false
// 3. Call all functions & exit
for _, f := range CasList() {
header, row, err := f(k, pools)
if err == nil {
if len(row) > 0 {
storageResourcesFound = true
}
// 4. Find the correct heading & print the rows
util.TablePrinter(header, row, printers.PrintOptions{Wide: true})
// A visual separator for different cas-type pools/storage entities
fmt.Println()
}
}

if !storageResourcesFound {
return util.HandleEmptyTableError("Storage", openebsNS, casType)
}
}
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/storage/zfslocalpv.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func GetZFSPools(c *client.K8sClient, zfsnodes []string) ([]metav1.TableColumnDe
}
// 3. Actually print the table or return an error
if len(rows) == 0 {
return nil, nil, fmt.Errorf("no zfspools found")
return nil, nil, util.HandleEmptyTableError("zfs pools", c.Ns, "")
}
return util.ZFSPoolListColumnDefinitions, rows, nil
}
Expand Down
13 changes: 13 additions & 0 deletions pkg/util/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,16 @@ func CheckErr(err error, handleErr func(string)) {
}
handleErr(err.Error())
}

// Handle Empty handles error when resources or set of resources are not found
vharsh marked this conversation as resolved.
Show resolved Hide resolved
func HandleEmptyTableError(resource string, ns string, casType string) error {
if ns == "" && casType == "" {
return fmt.Errorf("no %s found in your cluster", resource)
} else if ns != "" && casType != "" {
return fmt.Errorf("no %s %s found in %s namespace", casType, resource, ns)
} else if casType != "" && !IsValidCasType(casType) {
return fmt.Errorf("cas-type %s not supported", casType)
} else {
return fmt.Errorf("no %s found in %s namespace", resource, ns)
}
}
66 changes: 66 additions & 0 deletions pkg/util/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,69 @@ func TestCheckErr(t *testing.T) {
})
}
}

func TestHandleEmptyTableError(t *testing.T) {
type args struct {
resource string
ns string
casType string
}
tests := []struct {
name string
args args
expected error
}{
{
"No Namespace and cas",
args{
resource: "ResourceType",
ns: "",
casType: "",
},
fmt.Errorf("no ResourceType found in your cluster"),
},
{
"Wrong cas or Namespace",
args{
resource: "ResourceType",
ns: "InValid",
casType: "jiva",
},
fmt.Errorf("no jiva ResourceType found in InValid namespace"),
},
{
"",
args{
resource: "ResourceType",
ns: "invalid",
casType: "",
},
fmt.Errorf("no ResourceType found in invalid namespace"),
},
{
"Wrong cas type in all namespace",
args{
resource: "ResourceType",
ns: "",
casType: "invalid",
},
fmt.Errorf("cas-type invalid not supported"),
},
{
"Wrong Namespace and all cas types",
args{
resource: "ResourceType",
ns: "InValid",
casType: "",
},
fmt.Errorf("no ResourceType found in InValid namespace"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tResult := HandleEmptyTableError(tt.args.resource, tt.args.ns, tt.args.casType); tResult.Error() != tt.expected.Error() {
t.Errorf("HandleEmptyTableError(): expected: %s, got: %s", tt.expected, tResult)
}
})
}
}
6 changes: 5 additions & 1 deletion pkg/volume/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@ func Get(vols []string, openebsNS, casType string) error {
}
}
}
// 3. Print the volumes from rows

// 3. Return Error or Print volumes from rows
if len(rows) == 0 {
return util.HandleEmptyTableError("Volume", openebsNS, casType)
}
util.TablePrinter(util.VolumeListColumnDefinations, rows, printers.PrintOptions{Wide: true})
return nil
}
Expand Down