Refactor Redfish ClientError

This change refactors the Redfish ClientError to match the error pattern
used elsewhere in airshipctl.

Change-Id: Ie309ba9ac41e5b618cf2f4c18f1f381d875e9cdb
Signed-off-by: Drew Walters <andrew.walters@att.com>
This commit is contained in:
Drew Walters 2020-03-17 15:35:02 +00:00
parent 3a6332f4de
commit 0cf1068c5f
5 changed files with 22 additions and 20 deletions

View File

@ -7,16 +7,17 @@ import (
aerror "opendev.org/airship/airshipctl/pkg/errors" aerror "opendev.org/airship/airshipctl/pkg/errors"
) )
type ClientError struct { // ErrRedfishClient describes an error encountered by the go-redfish client.
type ErrRedfishClient struct {
aerror.AirshipError aerror.AirshipError
Message string
} }
func NewRedfishClientErrorf(format string, v ...interface{}) error { func (e ErrRedfishClient) Error() string {
e := &ClientError{} return fmt.Sprintf("redfish client encountered an error: %s", e.Message)
e.Message = fmt.Sprintf(format, v...)
return e
} }
// ErrRedfishMissingConfig describes an error encountered due to a missing configuration option.
type ErrRedfishMissingConfig struct { type ErrRedfishMissingConfig struct {
What string What string
} }
@ -31,10 +32,10 @@ func (e ErrRedfishMissingConfig) Error() string {
func ScreenRedfishError(httpResp *http.Response, err error) error { func ScreenRedfishError(httpResp *http.Response, err error) error {
if err != nil && httpResp != nil { if err != nil && httpResp != nil {
if httpResp.StatusCode < 200 || httpResp.StatusCode >= 400 { if httpResp.StatusCode < 200 || httpResp.StatusCode >= 400 {
return NewRedfishClientErrorf("%s", err.Error()) return ErrRedfishClient{Message: err.Error()}
} }
} else if err != nil { } else if err != nil {
return NewRedfishClientErrorf("%s", err.Error()) return ErrRedfishClient{Message: err.Error()}
} }
return nil return nil
} }

View File

@ -39,7 +39,7 @@ func (cfg RemoteDirect) DoRemoteDirect() error {
systemID := cfg.EphemeralNodeID systemID := cfg.EphemeralNodeID
system, _, err := cfg.RedfishAPI.GetSystem(cfg.Context, systemID) system, _, err := cfg.RedfishAPI.GetSystem(cfg.Context, systemID)
if err != nil { if err != nil {
return NewRedfishClientErrorf("Get System[%s] failed with err: %s", systemID, err.Error()) return ErrRedfishClient{Message: fmt.Sprintf("Get System[%s] failed with err: %v", systemID, err)}
} }
alog.Debugf("Ephemeral Node System ID: '%s'", systemID) alog.Debugf("Ephemeral Node System ID: '%s'", systemID)

View File

@ -75,7 +75,7 @@ func TestRedfishRemoteDirectInvalidSystemId(t *testing.T) {
err := localRDCfg.DoRemoteDirect() err := localRDCfg.DoRemoteDirect()
_, ok := err.(*ClientError) _, ok := err.(ErrRedfishClient)
assert.True(t, ok) assert.True(t, ok)
} }
@ -96,7 +96,7 @@ func TestRedfishRemoteDirectGetSystemNetworkError(t *testing.T) {
err := rDCfg.DoRemoteDirect() err := rDCfg.DoRemoteDirect()
_, ok := err.(*ClientError) _, ok := err.(ErrRedfishClient)
assert.True(t, ok) assert.True(t, ok)
} }
@ -123,7 +123,7 @@ func TestRedfishRemoteDirectInvalidIsoPath(t *testing.T) {
err := localRDCfg.DoRemoteDirect() err := localRDCfg.DoRemoteDirect()
_, ok := err.(*ClientError) _, ok := err.(ErrRedfishClient)
assert.True(t, ok) assert.True(t, ok)
} }
@ -148,7 +148,7 @@ func TestRedfishRemoteDirectCdDvdNotAvailableInBootSources(t *testing.T) {
err := rDCfg.DoRemoteDirect() err := rDCfg.DoRemoteDirect()
_, ok := err.(*ClientError) _, ok := err.(ErrRedfishClient)
assert.True(t, ok) assert.True(t, ok)
} }
@ -175,7 +175,7 @@ func TestRedfishRemoteDirectSetSystemBootSourceFailed(t *testing.T) {
err := rDCfg.DoRemoteDirect() err := rDCfg.DoRemoteDirect()
_, ok := err.(*ClientError) _, ok := err.(ErrRedfishClient)
assert.True(t, ok) assert.True(t, ok)
} }
@ -209,7 +209,7 @@ func TestRedfishRemoteDirectSystemRebootFailed(t *testing.T) {
err := rDCfg.DoRemoteDirect() err := rDCfg.DoRemoteDirect()
_, ok := err.(*ClientError) _, ok := err.(ErrRedfishClient)
assert.True(t, ok) assert.True(t, ok)
} }

View File

@ -2,6 +2,7 @@ package redfish
import ( import (
"context" "context"
"fmt"
"net/url" "net/url"
"strings" "strings"
"time" "time"
@ -63,7 +64,7 @@ func SetSystemBootSourceForMediaType(ctx context.Context,
/* Check available boot sources for system */ /* Check available boot sources for system */
system, _, err := api.GetSystem(ctx, systemID) system, _, err := api.GetSystem(ctx, systemID)
if err != nil { if err != nil {
return NewRedfishClientErrorf("Get System[%s] failed with err: %s", systemID, err.Error()) return ErrRedfishClient{Message: fmt.Sprintf("Get System[%s] failed with err: %v", systemID, err)}
} }
allowableValues := system.Boot.BootSourceOverrideTargetRedfishAllowableValues allowableValues := system.Boot.BootSourceOverrideTargetRedfishAllowableValues
@ -77,7 +78,7 @@ func SetSystemBootSourceForMediaType(ctx context.Context,
} }
} }
return NewRedfishClientErrorf("failed to set system[%s] boot source", systemID) return ErrRedfishClient{Message: fmt.Sprintf("failed to set system[%s] boot source", systemID)}
} }
// Reboots a system by force shutoff and turning on. // Reboots a system by force shutoff and turning on.

View File

@ -25,7 +25,7 @@ func TestRedfishErrorNonNilErrorWithoutHttpResp(t *testing.T) {
realErr := fmt.Errorf("sample error") realErr := fmt.Errorf("sample error")
err := ScreenRedfishError(nil, realErr) err := ScreenRedfishError(nil, realErr)
assert.Error(t, err) assert.Error(t, err)
_, ok := err.(*ClientError) _, ok := err.(ErrRedfishClient)
assert.True(t, ok) assert.True(t, ok)
} }
@ -34,15 +34,15 @@ func TestRedfishErrorNonNilErrorWithHttpRespError(t *testing.T) {
httpResp := &http.Response{StatusCode: 408} httpResp := &http.Response{StatusCode: 408}
err := ScreenRedfishError(httpResp, realErr) err := ScreenRedfishError(httpResp, realErr)
assert.Equal(t, err, NewRedfishClientErrorf(realErr.Error())) assert.Equal(t, err, ErrRedfishClient{Message: realErr.Error()})
httpResp.StatusCode = 400 httpResp.StatusCode = 400
err = ScreenRedfishError(httpResp, realErr) err = ScreenRedfishError(httpResp, realErr)
assert.Equal(t, err, NewRedfishClientErrorf(realErr.Error())) assert.Equal(t, err, ErrRedfishClient{Message: realErr.Error()})
httpResp.StatusCode = 199 httpResp.StatusCode = 199
err = ScreenRedfishError(httpResp, realErr) err = ScreenRedfishError(httpResp, realErr)
assert.Equal(t, err, NewRedfishClientErrorf(realErr.Error())) assert.Equal(t, err, ErrRedfishClient{Message: realErr.Error()})
} }
func TestRedfishErrorNonNilErrorWithHttpRespOK(t *testing.T) { func TestRedfishErrorNonNilErrorWithHttpRespOK(t *testing.T) {