Skip to content

Commit

Permalink
usability: Handle Exceptions for no resource lists
Browse files Browse the repository at this point in the history
Signed-off-by: Abhishek-kumar09 <abhimait1909@gmail.com>
  • Loading branch information
Abhishek-kumar09 committed Sep 6, 2021
1 parent 46046a9 commit 01bfa72
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 5 deletions.
8 changes: 6 additions & 2 deletions pkg/blockdevice/blockdevice.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,11 @@ 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{}{"", "", "", "", "", "", ""}})
}
// Show the output using cli-runtime
util.TablePrinter(util.BDTreeListColumnDefinations, rows, printers.PrintOptions{Wide: true})
if len(rows) == 0 {
util.HandleEmptyTableError("Block Device", k.Ns, "")
} else {
// Show the output using cli-runtime
util.TablePrinter(util.BDTreeListColumnDefinations, rows, printers.PrintOptions{Wide: true})
}
return nil
}
14 changes: 13 additions & 1 deletion pkg/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,32 @@ func Get(pools []string, openebsNS string, casType string) error {
if err != nil {
return err
}
util.TablePrinter(header, rows, printers.PrintOptions{Wide: true})
if len(rows) == 0 {
return util.HandleEmptyTableError("Storage", openebsNS, casType)
} else {
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
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 Table Error
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 != "" {
return fmt.Errorf("unknown cas-type %s", casType)
} else {
return fmt.Errorf("no %s found in %s namespace", resource, ns)
}
}
50 changes: 50 additions & 0 deletions pkg/util/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,53 @@ 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
}{
{
"No Namespace and cas",
args{
resource: "ResourceType",
ns: "",
casType: "",
},
},
{
"Wrong cas or Namespace",
args{
resource: "ResourceType",
ns: "InValidNamespace",
casType: "jiva",
},
},
{
"Wrong cas type in all namespace",
args{
resource: "ResourceType",
ns: "",
casType: "invalid",
},
},
{
"Wrong Namespace and all cas types",
args{
resource: "ResourceType",
ns: "InValidNamespace",
casType: "",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
HandleEmptyTableError(tt.args.resource, tt.args.ns, tt.args.casType)
})
}
}
9 changes: 7 additions & 2 deletions pkg/volume/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,13 @@ func Get(vols []string, openebsNS, casType string) error {
}
}
}
// 3. Print the volumes from rows
util.TablePrinter(util.VolumeListColumnDefinations, rows, printers.PrintOptions{Wide: true})

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

Expand Down

0 comments on commit 01bfa72

Please sign in to comment.