Skip to content

Commit

Permalink
Merge pull request #150 from sosodev/main
Browse files Browse the repository at this point in the history
Collect errors in a custom error type when fetching a collection
  • Loading branch information
stmcginnis authored Jun 24, 2021
2 parents 16f7bb2 + 41b4fd2 commit 6a4f26b
Show file tree
Hide file tree
Showing 61 changed files with 1,044 additions and 355 deletions.
41 changes: 41 additions & 0 deletions common/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package common

import (
"encoding/json"
"fmt"
)

// Collection represents a collection of entity references.
Expand Down Expand Up @@ -56,3 +57,43 @@ func GetCollection(c Client, uri string) (*Collection, error) {
}
return &result, nil
}

// CollectionError is used for collecting errors when working with collections
type CollectionError struct {
Failures map[string]error
}

// NewCollectionError gets you a new *CollectionError
// it's useful for collecting and formatting errors that occur when fetching a collection
func NewCollectionError() *CollectionError {
return &CollectionError{
Failures: make(map[string]error),
}
}

func (cr *CollectionError) Empty() bool {
return len(cr.Failures) == 0
}

// for associating a linked entity with its error
type entityError struct {
Link string `json:"link"`
Error string `json:"error"`
}

func (cr *CollectionError) Error() string {
var entityErrors []entityError
for link, err := range cr.Failures {
entityErrors = append(entityErrors, entityError{
Link: link,
Error: err.Error(),
})
}

errorsJSON, err := json.Marshal(entityErrors)
if err != nil {
panic(err)
}

return fmt.Sprintf("failed to retrieve some items: %s", errorsJSON)
}
12 changes: 9 additions & 3 deletions common/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,19 @@ func ListReferencedMessages(c Client, link string) ([]*Message, error) {
return result, err
}

collectionError := NewCollectionError()
for _, messageLink := range links.ItemLinks {
message, err := GetMessage(c, messageLink)
if err != nil {
return result, err
collectionError.Failures[messageLink] = err
} else {
result = append(result, message)
}
result = append(result, message)
}

return result, nil
if collectionError.Empty() {
return result, nil
}

return result, collectionError
}
14 changes: 10 additions & 4 deletions redfish/assembly.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func GetAssembly(c common.Client, uri string) (*Assembly, error) {
return &assembly, nil
}

// ListReferencedAssemblys gets the collection of Assembly from
//nolint:dupl // ListReferencedAssemblys gets the collection of Assembly from
// a provided reference.
func ListReferencedAssemblys(c common.Client, link string) ([]*Assembly, error) {
var result []*Assembly
Expand All @@ -102,15 +102,21 @@ func ListReferencedAssemblys(c common.Client, link string) ([]*Assembly, error)
return result, err
}

collectionError := common.NewCollectionError()
for _, assemblyLink := range links.ItemLinks {
assembly, err := GetAssembly(c, assemblyLink)
if err != nil {
return result, err
collectionError.Failures[assemblyLink] = err
} else {
result = append(result, assembly)
}
result = append(result, assembly)
}

return result, nil
if collectionError.Empty() {
return result, nil
}

return result, collectionError
}

// AssemblyData is information about an assembly.
Expand Down
14 changes: 10 additions & 4 deletions redfish/bios.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func GetBios(c common.Client, uri string) (*Bios, error) {
return &bios, nil
}

// ListReferencedBioss gets the collection of Bios from a provided reference.
//nolint:dupl // ListReferencedBioss gets the collection of Bios from a provided reference.
func ListReferencedBioss(c common.Client, link string) ([]*Bios, error) {
var result []*Bios
if link == "" {
Expand All @@ -160,15 +160,21 @@ func ListReferencedBioss(c common.Client, link string) ([]*Bios, error) {
return result, err
}

collectionError := common.NewCollectionError()
for _, biosLink := range links.ItemLinks {
bios, err := GetBios(c, biosLink)
if err != nil {
return result, err
collectionError.Failures[biosLink] = err
} else {
result = append(result, bios)
}
result = append(result, bios)
}

return result, nil
if collectionError.Empty() {
return result, nil
}

return result, collectionError
}

// ChangePassword shall change the selected BIOS password.
Expand Down
49 changes: 37 additions & 12 deletions redfish/chassis.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,15 +339,21 @@ func ListReferencedChassis(c common.Client, link string) ([]*Chassis, error) {
return result, err
}

collectionError := common.NewCollectionError()
for _, chassisLink := range links.ItemLinks {
chassis, err := GetChassis(c, chassisLink)
if err != nil {
return result, err
collectionError.Failures[chassisLink] = err
} else {
result = append(result, chassis)
}
result = append(result, chassis)
}

return result, nil
if collectionError.Empty() {
return result, nil
}

return result, collectionError
}

// Drives gets the drives attached to the storage controllers that this
Expand All @@ -370,14 +376,21 @@ func (chassis *Chassis) Drives() ([]*Drive, error) {
driveLinks = drives.ItemLinks
}

collectionError := common.NewCollectionError()
for _, driveLink := range driveLinks {
drive, err := GetDrive(chassis.Client, driveLink)
if err != nil {
return result, nil
collectionError.Failures[driveLink] = err
} else {
result = append(result, drive)
}
result = append(result, drive)
}
return result, nil

if collectionError.Empty() {
return result, nil
}

return result, collectionError
}

// Thermal gets the thermal temperature and cooling information for the chassis
Expand Down Expand Up @@ -411,31 +424,43 @@ func (chassis *Chassis) Power() (*Power, error) {
// ComputerSystems returns the collection of systems from this chassis
func (chassis *Chassis) ComputerSystems() ([]*ComputerSystem, error) {
var result []*ComputerSystem

collectionError := common.NewCollectionError()
for _, uri := range chassis.computerSystems {
cs, err := GetComputerSystem(chassis.Client, uri)
if err != nil {
return nil, err
collectionError.Failures[uri] = err
} else {
result = append(result, cs)
}
}

result = append(result, cs)
if collectionError.Empty() {
return result, nil
}

return result, nil
return result, collectionError
}

// ManagedBy gets the collection of managers of this chassis
func (chassis *Chassis) ManagedBy() ([]*Manager, error) {
var result []*Manager

collectionError := common.NewCollectionError()
for _, uri := range chassis.managedBy {
manager, err := GetManager(chassis.Client, uri)
if err != nil {
return nil, err
collectionError.Failures[uri] = err
} else {
result = append(result, manager)
}
}

result = append(result, manager)
if collectionError.Empty() {
return result, nil
}

return result, nil
return result, collectionError
}

// NetworkAdapters gets the collection of network adapters of this chassis
Expand Down
14 changes: 10 additions & 4 deletions redfish/compositionservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func GetCompositionService(c common.Client, uri string) (*CompositionService, er
return &compositionservice, nil
}

// ListReferencedCompositionServices gets the collection of CompositionService from
//nolint:dupl // ListReferencedCompositionServices gets the collection of CompositionService from
// a provided reference.
func ListReferencedCompositionServices(c common.Client, link string) ([]*CompositionService, error) {
var result []*CompositionService
Expand All @@ -118,13 +118,19 @@ func ListReferencedCompositionServices(c common.Client, link string) ([]*Composi
return result, err
}

collectionError := common.NewCollectionError()
for _, compositionserviceLink := range links.ItemLinks {
compositionservice, err := GetCompositionService(c, compositionserviceLink)
if err != nil {
return result, err
collectionError.Failures[compositionserviceLink] = err
} else {
result = append(result, compositionservice)
}
result = append(result, compositionservice)
}

return result, nil
if collectionError.Empty() {
return result, nil
}

return result, collectionError
}
40 changes: 31 additions & 9 deletions redfish/computersystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -626,15 +626,21 @@ func ListReferencedComputerSystems(c common.Client, link string) ([]*ComputerSys
return result, err
}

collectionError := common.NewCollectionError()
for _, computersystemLink := range links.ItemLinks {
computersystem, err := GetComputerSystem(c, computersystemLink)
if err != nil {
return result, err
collectionError.Failures[computersystemLink] = err
} else {
result = append(result, computersystem)
}
result = append(result, computersystem)
}

return result, nil
if collectionError.Empty() {
return result, nil
}

return result, collectionError
}

// Bios gets the Bios information for this ComputerSystem.
Expand Down Expand Up @@ -674,27 +680,43 @@ func (computersystem *ComputerSystem) NetworkInterfaces() ([]*NetworkInterface,
// PCIeDevices gets all PCIeDevices for this system.
func (computersystem *ComputerSystem) PCIeDevices() ([]*PCIeDevice, error) {
var result []*PCIeDevice

collectionError := common.NewCollectionError()
for _, pciedeviceLink := range computersystem.pcieDevices {
pciedevice, err := GetPCIeDevice(computersystem.Client, pciedeviceLink)
if err != nil {
return result, err
collectionError.Failures[pciedeviceLink] = err
} else {
result = append(result, pciedevice)
}
result = append(result, pciedevice)
}
return result, nil

if collectionError.Empty() {
return result, nil
}

return result, collectionError
}

// PCIeFunctions gets all PCIeFunctions for this system.
func (computersystem *ComputerSystem) PCIeFunctions() ([]*PCIeFunction, error) {
var result []*PCIeFunction

collectionError := common.NewCollectionError()
for _, pciefunctionLink := range computersystem.pcieFunctions {
pciefunction, err := GetPCIeFunction(computersystem.Client, pciefunctionLink)
if err != nil {
return result, err
collectionError.Failures[pciefunctionLink] = err
} else {
result = append(result, pciefunction)
}
result = append(result, pciefunction)
}
return result, nil

if collectionError.Empty() {
return result, nil
}

return result, collectionError
}

// Processors returns a collection of processors from this system
Expand Down
Loading

0 comments on commit 6a4f26b

Please sign in to comment.