From c9f28a19b617404fd1a6b0f4c9906e2fe8ef16b7 Mon Sep 17 00:00:00 2001 From: Mengling Ding Date: Tue, 20 Aug 2024 17:23:39 -0500 Subject: [PATCH 1/4] fix: optimize the client errors in cfm-service --- pkg/api/api_default_service.go | 7 +- pkg/common/parameters.go | 5 + pkg/common/status.go | 211 +++++++++++++-------------------- pkg/manager/appliance.go | 22 ++-- pkg/manager/blade.go | 46 ++++--- pkg/manager/cache.go | 10 +- pkg/manager/host.go | 4 +- pkg/manager/manager.go | 30 +++-- pkg/manager/memory.go | 8 +- pkg/manager/port.go | 6 +- pkg/manager/resource.go | 4 +- 11 files changed, 162 insertions(+), 191 deletions(-) diff --git a/pkg/api/api_default_service.go b/pkg/api/api_default_service.go index 5d489e7..1b3aaec 100644 --- a/pkg/api/api_default_service.go +++ b/pkg/api/api_default_service.go @@ -235,7 +235,12 @@ func (cfm *CfmApiService) BladesComposeMemory(ctx context.Context, applianceId s memory, err := blade.ComposeMemory(ctx, &r) if err != nil { if memory != nil { - return openapi.Response(http.StatusPartialContent, memory), nil + // Use the custom response structure to pass both the memory and partial error to the frontend. + customResponse := common.CustomResponse{ + Data: memory, + Error: err.Error(), + } + return openapi.Response(http.StatusPartialContent, customResponse), nil } else { return formatErrorResp(ctx, err.(*common.RequestError)) } diff --git a/pkg/common/parameters.go b/pkg/common/parameters.go index 533c96e..0c502aa 100644 --- a/pkg/common/parameters.go +++ b/pkg/common/parameters.go @@ -34,6 +34,11 @@ type Settings struct { HostIpOverride string // An user override option for the host server Ip address } +type CustomResponse struct { + Data interface{} `json:"data,omitempty"` + Error string `json:"error,omitempty"` +} + const ( KeyVerbosity = "cfmCtxVerbosity" KeyBackend = "cfmCtxBackend" diff --git a/pkg/common/status.go b/pkg/common/status.go index ab14bbb..a23b7dc 100644 --- a/pkg/common/status.go +++ b/pkg/common/status.go @@ -25,70 +25,61 @@ type StatusCodeType int // can be used programmatically by clients to differentiate various errors returned. // More error code will be added const ( - StatusOK StatusCodeType = iota - StatusCreatedSuccess //201 - StatusComposePartialSuccess //206 - StatusApplianceResyncPartialSuccess //206 + StatusOK StatusCodeType = iota + + StatusComposePartialSuccess //206 + StatusApplianceResyncPartialSuccess //206 StatusAppliancesExceedMaximum //422 StatusBladesExceedMaximum //422 StatusHostsExceedMaximum //422 - StatusInvalidBackend //400: StatusBadRequest - StatusApplianceComposeMemoryRequestBad //400 - StatusApplianceIdMismatch //400 - StatusResourceNotEnough //400 - StatusEndpointNotConnected //400 - StatusBackendInterfaceFailure //500: StatusInternalServerError + StatusBackendInterfaceFailure //500: StatusInternalServerError - StatusComposeMemoryFailure //409: StatusConflict - StatusComposeMemoryByResourceFailure //409 - StatusAssignMemoryFailure //409 - StatusUnassignMemoryFailure //409 - StatusHostGetMemoryFailure //409 - StatusApplianceGetMemoryFailure //409 - StatusApplianceGetMemoryByIdFailure //409 - StatusHostGetMemoryByIdFailure //409 - StatusApplianceFreeMemoryFailure //409 - StatusHostFreeMemoryFailure //409 - StatusGetMemoryResourceBlocksFailure //409 - StatusGetMemoryResourceBlockByIdFailure //409 - StatusApplianceGetPortsFailure //409 - StatusHostGetPortsFailure //409 - StatusGetPortDetailsFailure //409 - StatusGetMemoryDevicesFailure //409 - StatusGetMemoryDevicesDetailsFailure //409 - StatusApplianceResyncFailure //409 + StatusComposeMemoryByResourceFailure //409 - StatusApplianceNameDoesNotExist //404: StatusNotFound - StatusBladeIdDoesNotExist //404 - StatusApplianceCreateSessionFailure //500 - StatusApplianceDeleteSessionFailure //500 - StatusApplianceIdDoesNotExist //404 + StatusAssignMemoryFailure //409 + StatusUnassignMemoryFailure //409 + + StatusHostGetMemoryFailure //409 + StatusBladeGetMemoryFailure //409 + StatusBladeGetMemoryByIdFailure //409 + StatusHostGetMemoryByIdFailure //409 + StatusBladeFreeMemoryFailure //409 + StatusHostFreeMemoryFailure //409 - StatusHostNameDoesNotExist //404 - StatusHostCreateSessionFailure //500 - StatusHostDeleteSessionFailure //500 - StatusApplianceGetResourcesFailure //500 - StatusApplianceGetResourceFailure //500 - StatusApplianceGetEndpointsFailure //500 - StatusApplianceGetOneEndpointFailure //500 - StatusApplianceUnassginMemoryFailure //500 - StatusApplianceUnallocateMemoryFailure //500 + StatusBladeGetMemoryResourceBlocksFailure //409 + StatusBladeGetMemoryResourceBlockDetailsFailure //409 + StatusBladeGetPortsFailure //409 + StatusHostGetPortsFailure //409 + StatusGetPortDetailsFailure //409 + StatusGetMemoryDevicesFailure //409 + StatusGetMemoryDevicesDetailsFailure //409 - StatusHostIdDoesNotExist //404 + StatusApplianceResyncFailure //409 + StatusApplianceIdDuplicate //409 + StatusBladeIdDuplicate //409 + StatusMemoryIdDuplicate //409 + StatusPortIdDuplicate //409 + StatusHostIdDuplicate //409 + + StatusApplianceCreateSessionFailure //500 + StatusApplianceDeleteSessionFailure //500 + StatusBladeCreateSessionFailure //500 + StatusBladeDeleteSessionFailure //500 + StatusHostCreateSessionFailure //500 + StatusHostDeleteSessionFailure //500 + + StatusApplianceIdDoesNotExist //404 + StatusBladeIdDoesNotExist //404 + StatusHostIdDoesNotExist //404 StatusMemoryIdDoesNotExist //404 StatusPortIdDoesNotExist //404 StatusResourceIdDoesNotExist //404 StatusMemoryDeviceIdDoesNotExist //404 - StatusNoCapacityInBlock //404 - - StatusEndpointDoesNotExist //404 - StatusSessionDoesNotExist //404 StatusManagerInitializationFailure //500 - ) // Return a string representation of our StatusType. When this value is printed as a @@ -97,32 +88,26 @@ func (e StatusCodeType) String() string { switch e { case StatusOK: return "Success" - case StatusCreatedSuccess: - return "Created Successfully" case StatusComposePartialSuccess: return "Compose Partial Success" case StatusApplianceResyncPartialSuccess: return "Appliance Resync Partial Success" - case StatusComposeMemoryFailure: - return "Compose Memory Failed" case StatusComposeMemoryByResourceFailure: return "Compose Memory By Resource Failed" case StatusAssignMemoryFailure: return "Assign Memory Failed" case StatusUnassignMemoryFailure: return "Unassign Memory Failed" - case StatusApplianceNameDoesNotExist: - return "Appliance Name does not exist" - case StatusHostNameDoesNotExist: - return "Host Name does not exist" - case StatusInvalidBackend: - return "Backend interface does not exist" case StatusBackendInterfaceFailure: return "Backend Interface does not exist" case StatusApplianceCreateSessionFailure: return "Appliance Create Session Failure" case StatusApplianceDeleteSessionFailure: return "Appliance Delete Session Failure" + case StatusBladeCreateSessionFailure: + return "Blade Create Session Failure" + case StatusBladeDeleteSessionFailure: + return "Blade Delete Session Failure" case StatusApplianceIdDoesNotExist: return "Appliance Id Does Not Exist" case StatusHostCreateSessionFailure: @@ -137,24 +122,24 @@ func (e StatusCodeType) String() string { return "Port Id Does Not Exist" case StatusResourceIdDoesNotExist: return "Resource Id Does Not Exist" - case StatusApplianceGetMemoryFailure: - return "Appliance Get Memory Failure" + case StatusBladeGetMemoryFailure: + return "Blade Get Memory Failure" case StatusHostGetMemoryFailure: return "Host Get Memory Failure" - case StatusApplianceGetMemoryByIdFailure: - return "Appliance Get Memory By Id Failure" + case StatusBladeGetMemoryByIdFailure: + return "Blade Get Memory By Id Failure" case StatusHostGetMemoryByIdFailure: return "Host Get Memory By Id Failure" - case StatusApplianceFreeMemoryFailure: - return "Appliance Free Memory Failure" + case StatusBladeFreeMemoryFailure: + return "Blade Free Memory Failure" case StatusHostFreeMemoryFailure: return "Host Free Memory Failure" - case StatusGetMemoryResourceBlocksFailure: - return "Appliance Get Memory Resource Blocks Failure" - case StatusGetMemoryResourceBlockByIdFailure: - return "Appliance Get Memory Resource Block By Id Failure" - case StatusApplianceGetPortsFailure: - return "Appliance Get Ports Failure" + case StatusBladeGetMemoryResourceBlocksFailure: + return "Blade Get Memory Resource Blocks Failure" + case StatusBladeGetMemoryResourceBlockDetailsFailure: + return "Blade Get Memory Resource Block By Id Failure" + case StatusBladeGetPortsFailure: + return "Blade Get Ports Failure" case StatusHostGetPortsFailure: return "Host Get Ports Failure" case StatusGetPortDetailsFailure: @@ -165,32 +150,6 @@ func (e StatusCodeType) String() string { return "Get Memory Devices Details Failure" case StatusApplianceResyncFailure: return "Appliance Resync Failure" - case StatusResourceNotEnough: - return "Resource Not Enough" - case StatusSessionDoesNotExist: - return "Session Does Not Exist" - case StatusApplianceGetResourceFailure: - return "Appliance Get A Specific Resource Failure" - case StatusApplianceGetResourcesFailure: - return "Appliance Get Resources Failure" - case StatusNoCapacityInBlock: - return "No Capacity In Block" - case StatusEndpointDoesNotExist: - return "Appliance Does Not Exist" - case StatusApplianceGetEndpointsFailure: - return "Appliance Get Endpoints Failure" - case StatusApplianceGetOneEndpointFailure: - return "Appliance Get One Endpoint Failure" - case StatusApplianceIdMismatch: - return "Appliance Id Mismatch" - case StatusApplianceComposeMemoryRequestBad: - return "Appliance Compose Memory Request Empty" - case StatusEndpointNotConnected: - return "Endpoint Not Connected" - case StatusApplianceUnassginMemoryFailure: - return "Appliance Unassgin Memory Failure" - case StatusApplianceUnallocateMemoryFailure: - return "Appliance Unallocate Memory Failure" case StatusBladeIdDoesNotExist: return "Blade Id Does Not Exist" case StatusAppliancesExceedMaximum: @@ -201,6 +160,16 @@ func (e StatusCodeType) String() string { return "Maximum Host count exceeded" case StatusManagerInitializationFailure: return "Manager Initialization Failure" + case StatusApplianceIdDuplicate: + return "Appliance Id Already Exists" + case StatusBladeIdDuplicate: + return "Blade Id Already Exists" + case StatusMemoryIdDuplicate: + return "Memory Id Already Exist" + case StatusPortIdDuplicate: + return "Port Id Already Exist" + case StatusHostIdDuplicate: + return "Host Id Already Exist" } return "Unknown" @@ -212,60 +181,46 @@ func (e StatusCodeType) HttpStatusCode() int { switch e { case StatusOK: return http.StatusOK // 200 - case StatusCreatedSuccess: - return http.StatusCreated // 201 case StatusComposePartialSuccess, StatusApplianceResyncPartialSuccess: return http.StatusPartialContent // 206 - case StatusInvalidBackend, - StatusApplianceComposeMemoryRequestBad, - StatusApplianceIdMismatch, - StatusResourceNotEnough, - StatusEndpointNotConnected, - StatusBladeIdDoesNotExist: - return http.StatusBadRequest // 400 - case StatusApplianceNameDoesNotExist, + case StatusBladeIdDoesNotExist, StatusApplianceIdDoesNotExist, - StatusHostNameDoesNotExist, StatusHostIdDoesNotExist, StatusMemoryIdDoesNotExist, StatusPortIdDoesNotExist, StatusResourceIdDoesNotExist, - StatusEndpointDoesNotExist, - StatusMemoryDeviceIdDoesNotExist, - StatusNoCapacityInBlock, - StatusSessionDoesNotExist: + StatusMemoryDeviceIdDoesNotExist: return http.StatusNotFound // 404 - case StatusComposeMemoryFailure, - StatusComposeMemoryByResourceFailure, + case StatusComposeMemoryByResourceFailure, StatusAssignMemoryFailure, StatusUnassignMemoryFailure, StatusHostGetMemoryFailure, - StatusApplianceGetMemoryFailure, - StatusApplianceGetMemoryByIdFailure, + StatusBladeGetMemoryFailure, + StatusBladeGetMemoryByIdFailure, StatusHostGetMemoryByIdFailure, - StatusApplianceFreeMemoryFailure, + StatusBladeFreeMemoryFailure, StatusHostFreeMemoryFailure, - StatusGetMemoryResourceBlocksFailure, - StatusGetMemoryResourceBlockByIdFailure, - StatusApplianceGetPortsFailure, + StatusBladeGetMemoryResourceBlocksFailure, + StatusBladeGetMemoryResourceBlockDetailsFailure, + StatusBladeGetPortsFailure, StatusHostGetPortsFailure, StatusGetPortDetailsFailure, StatusGetMemoryDevicesFailure, StatusGetMemoryDevicesDetailsFailure, - StatusApplianceResyncFailure: + StatusApplianceResyncFailure, + StatusApplianceIdDuplicate, + StatusBladeIdDuplicate, + StatusPortIdDuplicate, + StatusHostIdDuplicate: return http.StatusConflict // 409 case StatusBackendInterfaceFailure, - StatusApplianceCreateSessionFailure, - StatusApplianceDeleteSessionFailure, + StatusBladeCreateSessionFailure, + StatusBladeDeleteSessionFailure, StatusHostCreateSessionFailure, StatusHostDeleteSessionFailure, - StatusApplianceGetResourcesFailure, - StatusApplianceGetResourceFailure, - StatusApplianceGetEndpointsFailure, - StatusApplianceGetOneEndpointFailure, - StatusApplianceUnassginMemoryFailure, - StatusApplianceUnallocateMemoryFailure, + StatusApplianceCreateSessionFailure, + StatusApplianceDeleteSessionFailure, StatusManagerInitializationFailure: return http.StatusInternalServerError // 500 case StatusAppliancesExceedMaximum, diff --git a/pkg/manager/appliance.go b/pkg/manager/appliance.go index d010595..c1b256b 100644 --- a/pkg/manager/appliance.go +++ b/pkg/manager/appliance.go @@ -38,7 +38,8 @@ func NewAppliance(ctx context.Context, id string) (*Appliance, error) { // Check for duplicate ID _, exists := deviceCache.GetApplianceByIdOk(applianceId) if exists { - return nil, fmt.Errorf("invalid id: applianceId [%s] already exists in cfm-service", applianceId) + newErr := fmt.Errorf("invalid id: applianceId [%s] already exists in cfm-service", applianceId) + return nil, &common.RequestError{StatusCode: common.StatusApplianceIdDuplicate, Err: newErr} } a := Appliance{ @@ -93,7 +94,7 @@ func (a *Appliance) AddBlade(ctx context.Context, c *openapi.Credentials) (*Blad if err != nil || response == nil { newErr := fmt.Errorf("create session failure at [%s:%d] using interface [%s]: %w", c.IpAddress, c.Port, backendName, err) logger.Error(newErr, "failure: add blade") - return nil, &common.RequestError{StatusCode: common.StatusApplianceCreateSessionFailure, Err: newErr} + return nil, &common.RequestError{StatusCode: common.StatusBladeCreateSessionFailure, Err: newErr} } bladeId := c.CustomId @@ -113,12 +114,12 @@ func (a *Appliance) AddBlade(ctx context.Context, c *openapi.Credentials) (*Blad if err != nil || response == nil { newErr := fmt.Errorf("failed to delete session [%s:%d] after failed duplicate bladeId [%s] check: %w", c.IpAddress, c.Port, bladeId, err) logger.Error(newErr, "failure: add blade") - return nil, &common.RequestError{StatusCode: common.StatusApplianceDeleteSessionFailure, Err: newErr} + return nil, &common.RequestError{StatusCode: common.StatusBladeDeleteSessionFailure, Err: newErr} } newErr := fmt.Errorf("invalid id: bladeId [%s] already exists on appliance [%s] ", bladeId, a.Id) logger.Error(newErr, "failure: add blade") - return nil, &common.RequestError{StatusCode: common.StatusManagerInitializationFailure, Err: newErr} + return nil, &common.RequestError{StatusCode: common.StatusBladeIdDuplicate, Err: newErr} } // Create the new Blade @@ -134,11 +135,11 @@ func (a *Appliance) AddBlade(ctx context.Context, c *openapi.Credentials) (*Blad blade, err := NewBlade(ctx, &r) if err != nil || blade == nil { req := backend.DeleteSessionRequest{} - response, err := ops.DeleteSession(ctx, &settings, &req) - if err != nil || response == nil { + response, deleErr := ops.DeleteSession(ctx, &settings, &req) + if deleErr != nil || response == nil { newErr := fmt.Errorf("failed to delete session [%s:%d] after failed blade [%s] object creation: %w", c.IpAddress, c.Port, bladeId, err) logger.Error(newErr, "failure: add blade") - return nil, &common.RequestError{StatusCode: common.StatusApplianceDeleteSessionFailure, Err: newErr} + return nil, &common.RequestError{StatusCode: common.StatusBladeDeleteSessionFailure, Err: newErr} } newErr := fmt.Errorf("appliance [%s] new blade object creation failure: %w", a.Id, err) @@ -178,8 +179,9 @@ func (a *Appliance) DeleteBladeById(ctx context.Context, bladeId string) (*Blade // query for blade blade, ok := a.Blades[bladeId] if !ok { - logger.V(2).Info("blade not found during delete: do nothing", "bladeId", bladeId, "applianceId", a.Id) - return &Blade{Id: bladeId}, nil + logger.V(2).Info("blade not found during delete:", "bladeId", bladeId, "applianceId", a.Id) + newErr := fmt.Errorf("blade [%s] not found during delete", bladeId) + return nil, &common.RequestError{StatusCode: common.StatusBladeIdDoesNotExist, Err: newErr} } // get blade backend @@ -198,7 +200,7 @@ func (a *Appliance) DeleteBladeById(ctx context.Context, bladeId string) (*Blade logger.V(2).Info("force blade deletion after backend session failure", "bladeId", blade.Id, "applianceId", a.Id) delete(a.Blades, blade.Id) - return nil, &common.RequestError{StatusCode: common.StatusApplianceDeleteSessionFailure, Err: newErr} + return nil, &common.RequestError{StatusCode: common.StatusBladeDeleteSessionFailure, Err: newErr} } // delete blade diff --git a/pkg/manager/blade.go b/pkg/manager/blade.go index 5623d20..fbfb1ea 100644 --- a/pkg/manager/blade.go +++ b/pkg/manager/blade.go @@ -173,14 +173,14 @@ func (b *Blade) ComposeMemory(ctx context.Context, r *RequestComposeMemory) (*op if err != nil { newErr := fmt.Errorf("get details failure on blade [%s] resource [%s]: %w", b.Id, resource.Id, err) logger.Error(newErr, "failure: compose memory") - return nil, &common.RequestError{StatusCode: common.StatusComposeMemoryFailure, Err: newErr} + return nil, &common.RequestError{StatusCode: err.(*common.RequestError).StatusCode, Err: newErr} } } resourceIds := b.findResourcesByQoS(r.SizeMib, r.Qos) if resourceIds == nil { newErr := fmt.Errorf("unable to find resources by qos during compose: appliance [%s] blade [%s] request [%v]", b.ApplianceId, b.Id, r) logger.Error(newErr, "failure: compose memory") - return nil, &common.RequestError{StatusCode: common.StatusComposeMemoryFailure, Err: newErr} + return nil, &common.RequestError{StatusCode: common.StatusBladeGetMemoryResourceBlocksFailure, Err: newErr} } memoryRegion, err := b.ComposeMemoryByResource(ctx, r.PortId, resourceIds) @@ -193,7 +193,7 @@ func (b *Blade) ComposeMemory(ctx context.Context, r *RequestComposeMemory) (*op } else { newErr := fmt.Errorf("compose memory by resource failure during compose memory: %w", err) logger.Error(newErr, "failure: compose memory") - return nil, &common.RequestError{StatusCode: common.StatusComposeMemoryFailure, Err: newErr} + return nil, &common.RequestError{StatusCode: common.StatusComposeMemoryByResourceFailure, Err: newErr} } } @@ -227,7 +227,7 @@ func (b *Blade) ComposeMemoryByResource(ctx context.Context, portId string, reso if err != nil { newErr := fmt.Errorf("add memory by id failed [%s] during compose by resource: appliance [%s] blade [%s] resourceIds [%s]: %w", responseAlloc.MemoryId, b.ApplianceId, b.Id, resourceIds, err) logger.Error(newErr, "failure: compose memory by resource") - return nil, &common.RequestError{StatusCode: common.StatusComposeMemoryByResourceFailure, Err: newErr} + return nil, &common.RequestError{StatusCode: err.(*common.RequestError).StatusCode, Err: newErr} } // Save there resource ids. Used when memory is freed @@ -238,7 +238,7 @@ func (b *Blade) ComposeMemoryByResource(ctx context.Context, portId string, reso if err != nil { newErr := fmt.Errorf("get details failure for memory [%s] during compose by resource: appliance [%s] blade [%s] resourceIds [%s]: %w", responseAlloc.MemoryId, b.ApplianceId, b.Id, resourceIds, err) logger.Error(newErr, "failure: compose memory by resource") - return nil, &common.RequestError{StatusCode: common.StatusComposeMemoryByResourceFailure, Err: newErr} + return nil, &common.RequestError{StatusCode: err.(*common.RequestError).StatusCode, Err: newErr} } //Invalidate all related object cache's @@ -292,7 +292,7 @@ func (b *Blade) FreeMemoryById(ctx context.Context, memoryId string) (*openapi.M if getMemoryErr != nil { newErr := fmt.Errorf("get memory by id (backend) failure on appliance [%s] blade [%s] memory [%s]: %w", b.ApplianceId, b.Id, memoryId, getMemoryErr) logger.Error(newErr, "failure: free memory by id") - return nil, &common.RequestError{StatusCode: common.StatusApplianceFreeMemoryFailure, Err: newErr} + return nil, &common.RequestError{StatusCode: common.StatusBladeGetMemoryByIdFailure, Err: newErr} } // If present, remove the assigned port before deallocating the memory region @@ -305,7 +305,7 @@ func (b *Blade) FreeMemoryById(ctx context.Context, memoryId string) (*openapi.M if unassignErr != nil { newErr := fmt.Errorf("unassign memory (backend) failure on appliance [%s] blade [%s] memory [%s]: %w", b.ApplianceId, b.Id, memoryId, unassignErr) logger.Error(newErr, "failure: free memory by id") - return nil, &common.RequestError{StatusCode: common.StatusApplianceFreeMemoryFailure, Err: newErr} + return nil, &common.RequestError{StatusCode: common.StatusUnassignMemoryFailure, Err: newErr} } //Invalidate all related object cache's @@ -321,7 +321,7 @@ func (b *Blade) FreeMemoryById(ctx context.Context, memoryId string) (*openapi.M if err != nil || response == nil { newErr := fmt.Errorf("free memory by id (backend) failure on appliance [%s] blade [%s] memory [%s]: %w", b.ApplianceId, b.Id, memoryId, err) logger.Error(newErr, "failure: free memory by id") - return nil, &common.RequestError{StatusCode: common.StatusApplianceFreeMemoryFailure, Err: newErr} + return nil, &common.RequestError{StatusCode: common.StatusBladeFreeMemoryFailure, Err: newErr} } // Invalidate the cooresponding resource caches @@ -434,7 +434,7 @@ func (b *Blade) GetMemoryBackend(ctx context.Context) ([]string, error) { if err != nil || response == nil { newErr := fmt.Errorf("get memory (backend) [%s] failure on blade [%s]: %w", b.backendOps.GetBackendInfo(ctx).BackendName, b.Id, err) logger.Error(newErr, "failure: get memory(backend)") - return nil, &common.RequestError{StatusCode: common.StatusApplianceGetMemoryFailure, Err: newErr} + return nil, &common.RequestError{StatusCode: common.StatusBladeGetMemoryFailure, Err: newErr} } logger.V(2).Info("success: get memory(backend)", "memoryIds", response.MemoryIds, "bladeId", b.Id, "applianceId", b.ApplianceId) @@ -487,7 +487,7 @@ func (b *Blade) GetPortsBackend(ctx context.Context) ([]string, error) { if err != nil || response == nil { newErr := fmt.Errorf("get ports (backend) [%s] failure on blade [%s]: %w", b.backendOps.GetBackendInfo(ctx).BackendName, b.Id, err) logger.Error(newErr, "failure: get ports(backend)") - return nil, &common.RequestError{StatusCode: common.StatusApplianceGetPortsFailure, Err: newErr} + return nil, &common.RequestError{StatusCode: common.StatusBladeGetPortsFailure, Err: newErr} } logger.V(2).Info("success: get ports(backend)", "portIds", response.PortIds, "bladeId", b.Id, "applianceId", b.ApplianceId) @@ -532,7 +532,7 @@ func (b *Blade) GetResourcesBackend(ctx context.Context) ([]string, error) { if err != nil || response == nil { newErr := fmt.Errorf("get resources (backend) [%s] failure on blade [%s]: %w", b.backendOps.GetBackendInfo(ctx).BackendName, b.Id, err) logger.Error(newErr, "failure: get resources(backend)") - return nil, &common.RequestError{StatusCode: common.StatusGetMemoryResourceBlocksFailure, Err: newErr} + return nil, &common.RequestError{StatusCode: common.StatusBladeGetMemoryResourceBlocksFailure, Err: newErr} } logger.V(2).Info("success: get resources(backend)", "resourceIds", response.MemoryResources, "bladeId", b.Id, "applianceId", b.ApplianceId) @@ -592,21 +592,19 @@ func (b *Blade) addMemoryById(ctx context.Context, memoryId string) (*BladeMemor _, exists := b.Memory[memoryId] if exists { - return nil, fmt.Errorf("appliance [%s] blade [%s] already contains memory with id [%s]", b.ApplianceId, b.Id, memoryId) + newErr := fmt.Errorf("appliance [%s] blade [%s] already contains memory with id [%s]", b.ApplianceId, b.Id, memoryId) + return nil, &common.RequestError{StatusCode: common.StatusMemoryIdDuplicate, Err: newErr} } // Create the new object - memory, err := NewBladeMemoryById(ctx, b.ApplianceId, b.Id, memoryId, b.backendOps) - if err != nil { - return nil, fmt.Errorf("memory object creation failed for appliance [%s] blade [%s]: %w", b.ApplianceId, b.Id, err) - } + memory := NewBladeMemoryById(ctx, b.ApplianceId, b.Id, memoryId, b.backendOps) // Initialize new object - err = memory.init(ctx) + err := memory.init(ctx) if err != nil { newErr := fmt.Errorf("memory [%s] object init failed for appliance [%s] blade [%s]: %w", memory.Id, b.ApplianceId, b.Id, err) logger.Error(newErr, "failure: add memory by id") - return nil, newErr + return nil, &common.RequestError{StatusCode: err.(*common.RequestError).StatusCode, Err: newErr} } // Add the new object @@ -620,21 +618,19 @@ func (b *Blade) addPortById(ctx context.Context, portId string) (*CxlBladePort, _, exists := b.Ports[portId] if exists { - return nil, fmt.Errorf("appliance [%s] blade [%s] already contains port with id [%s]", b.ApplianceId, b.Id, portId) + newErr := fmt.Errorf("appliance [%s] blade [%s] already contains port with id [%s]", b.ApplianceId, b.Id, portId) + return nil, &common.RequestError{StatusCode: common.StatusPortIdDuplicate, Err: newErr} } // Create the new object - port, err := NewCxlBladePortById(ctx, b.ApplianceId, b.Id, portId, b.backendOps) - if err != nil { - return nil, fmt.Errorf("port object creation failed for appliance [%s] blade [%s]: %w", b.ApplianceId, b.Id, err) - } + port := NewCxlBladePortById(ctx, b.ApplianceId, b.Id, portId, b.backendOps) // Initialize new object - err = port.init(ctx) + err := port.init(ctx) if err != nil { newErr := fmt.Errorf("port [%s] object init failed for appliance [%s] blade [%s]: %w", port.Id, b.ApplianceId, b.Id, err) logger.Error(newErr, "failure: add port by id") - return nil, newErr + return nil, &common.RequestError{StatusCode: err.(*common.RequestError).StatusCode, Err: newErr} } // Add the new object diff --git a/pkg/manager/cache.go b/pkg/manager/cache.go index 9f9a9a4..527e3de 100644 --- a/pkg/manager/cache.go +++ b/pkg/manager/cache.go @@ -22,6 +22,7 @@ ability to easily create there own searches through the cache. package manager import ( + "cfm/pkg/common" "fmt" ) @@ -46,7 +47,8 @@ func NewDevicesCache() *DevicesCache { func (c *DevicesCache) AddAppliance(appliance *Appliance) error { _, ok := c.appliances[appliance.Id] if ok { - return fmt.Errorf("cache already contains appliance with id [%s]", appliance.Id) + newErr := fmt.Errorf("cache already contains appliance with id [%s]", appliance.Id) + return &common.RequestError{StatusCode: common.StatusApplianceIdDuplicate, Err: newErr} } c.appliances[appliance.Id] = appliance @@ -79,7 +81,8 @@ func (c *DevicesCache) GetAllApplianceIds() []string { func (c *DevicesCache) GetApplianceById(applianceId string) (*Appliance, error) { appliance, ok := c.GetApplianceByIdOk(applianceId) if !ok { - return nil, fmt.Errorf("appliance [%s] doesn't exist", applianceId) + newErr := fmt.Errorf("appliance [%s] doesn't exist", applianceId) + return nil, &common.RequestError{StatusCode: common.StatusApplianceIdDoesNotExist, Err: newErr} } return appliance, nil @@ -262,7 +265,8 @@ func (c *DevicesCache) GetAllHostIds() []string { func (c *DevicesCache) GetHostById(hostId string) (*Host, error) { host, ok := c.GetHostByIdOk(hostId) if !ok { - return nil, fmt.Errorf("host [%s] doesn't exist", hostId) + newErr := fmt.Errorf("host [%s] doesn't exist", hostId) + return nil, &common.RequestError{StatusCode: common.StatusHostIdDoesNotExist, Err: newErr} } return host, nil diff --git a/pkg/manager/host.go b/pkg/manager/host.go index 4ea5a1b..2913577 100644 --- a/pkg/manager/host.go +++ b/pkg/manager/host.go @@ -78,7 +78,7 @@ func (h *Host) ComposeMemory(ctx context.Context, r *RequestComposeMemory) (*ope if err != nil { newErr := fmt.Errorf("connected blade port not found: host [%s] request [%v]", h.Id, r) logger.Error(newErr, "failure: compose memory(host)") - return nil, &common.RequestError{StatusCode: common.StatusComposeMemoryFailure, Err: newErr} + return nil, &common.RequestError{StatusCode: err.(*common.RequestError).StatusCode, Err: newErr} } req := RequestComposeMemory{ @@ -91,7 +91,7 @@ func (h *Host) ComposeMemory(ctx context.Context, r *RequestComposeMemory) (*ope if err != nil { newErr := fmt.Errorf("blade [%s] port [%s] compose memory failure: host [%s] request [%v]", blade.Id, bladePort.Id, h.Id, r) logger.Error(newErr, "failure: compose memory(host)") - return nil, &common.RequestError{StatusCode: common.StatusComposeMemoryFailure, Err: newErr} + return nil, &common.RequestError{StatusCode: err.(*common.RequestError).StatusCode, Err: newErr} } memory.MappedHostId = h.Id diff --git a/pkg/manager/manager.go b/pkg/manager/manager.go index 7ef05b5..e010b70 100644 --- a/pkg/manager/manager.go +++ b/pkg/manager/manager.go @@ -46,7 +46,7 @@ func AddAppliance(ctx context.Context, c *openapi.Credentials) (*Appliance, erro if err != nil || appliance == nil { newErr := fmt.Errorf("new appliance creation failure: %w", err) logger.Error(newErr, "failure: add appliance") - return nil, &common.RequestError{StatusCode: common.StatusManagerInitializationFailure, Err: newErr} + return nil, &common.RequestError{StatusCode: err.(*common.RequestError).StatusCode, Err: newErr} } // Cache it @@ -54,7 +54,7 @@ func AddAppliance(ctx context.Context, c *openapi.Credentials) (*Appliance, erro if err != nil { newErr := fmt.Errorf("add appliance [%s] failure: %w", appliance.Id, err) logger.Error(newErr, "failure: add appliance") - return nil, &common.RequestError{StatusCode: common.StatusManagerInitializationFailure, Err: newErr} + return nil, &common.RequestError{StatusCode: err.(*common.RequestError).StatusCode, Err: newErr} } logger.V(2).Info("success: add appliance", "applianceId", appliance.Id) @@ -69,8 +69,9 @@ func DeleteApplianceById(ctx context.Context, applianceId string) (*Appliance, e // query cache appliance, ok := deviceCache.GetApplianceByIdOk(applianceId) if !ok { - logger.V(2).Info("appliance not found during delete: do nothing", "applianceId", applianceId) - return &Appliance{Id: applianceId}, nil + newErr := fmt.Errorf("failed to get appliance [%s]", applianceId) + logger.Error(newErr, "failure: delete appliance by id") + return nil, &common.RequestError{StatusCode: common.StatusApplianceIdDoesNotExist, Err: newErr} } err := appliance.DeleteAllBlades(ctx) // cache and hardware interactions here @@ -85,7 +86,7 @@ func DeleteApplianceById(ctx context.Context, applianceId string) (*Appliance, e if a == nil { newErr := fmt.Errorf("appliance [%s] cache delete failed", appliance.Id) logger.Error(newErr, "failure: delete appliance by id") - return nil, &common.RequestError{StatusCode: common.StatusHostDeleteSessionFailure, Err: newErr} + return nil, &common.RequestError{StatusCode: common.StatusApplianceDeleteSessionFailure, Err: newErr} } logger.V(2).Info("success: delete appliance by id", "applianceId", a.Id) @@ -104,7 +105,8 @@ func GetApplianceById(ctx context.Context, applianceId string) (*Appliance, erro appliance, err := deviceCache.GetApplianceById(applianceId) if err != nil { logger.Error(err, "failure: get appliance by id") - return nil, &common.RequestError{StatusCode: common.StatusApplianceIdDoesNotExist, Err: err} + newErr := fmt.Errorf("appliance [%s] doesn't exist", applianceId) + return nil, &common.RequestError{StatusCode: err.(*common.RequestError).StatusCode, Err: newErr} } logger.V(2).Info("success: get appliance by id", "applianceId", applianceId) @@ -133,7 +135,7 @@ func ResyncApplianceById(ctx context.Context, applianceId string) (*Appliance, * if err != nil { newErr := fmt.Errorf("get appliance by id [%s] failure: %w", appliance.Id, err) logger.Error(newErr, "failure: resync appliance by id") - return nil, &failedBladeIds, &common.RequestError{StatusCode: common.StatusApplianceIdDoesNotExist, Err: newErr} + return nil, &failedBladeIds, &common.RequestError{StatusCode: err.(*common.RequestError).StatusCode, Err: newErr} } bladeIds := appliance.GetAllBladeIds() @@ -230,7 +232,7 @@ func AddHost(ctx context.Context, c *openapi.Credentials) (*Host, error) { newErr := fmt.Errorf("invalid id: hostId [%s] already exists in cfm-service", hostId) logger.Error(newErr, "failure: add host") - return nil, &common.RequestError{StatusCode: common.StatusManagerInitializationFailure, Err: newErr} + return nil, &common.RequestError{StatusCode: common.StatusHostIdDuplicate, Err: newErr} } // Create a new cfm-service Host object @@ -245,8 +247,8 @@ func AddHost(ctx context.Context, c *openapi.Credentials) (*Host, error) { host, err := NewHost(ctx, &r) if err != nil || host == nil { req := backend.DeleteSessionRequest{} - response, err := ops.DeleteSession(ctx, &settings, &req) - if err != nil || response == nil { + response, deleErr := ops.DeleteSession(ctx, &settings, &req) + if deleErr != nil || response == nil { newErr := fmt.Errorf("failed to delete session [%s:%d] after failed host [%s] object creation: %w", c.IpAddress, c.Port, hostId, err) logger.Error(newErr, "failure: add host") return nil, &common.RequestError{StatusCode: common.StatusHostDeleteSessionFailure, Err: newErr} @@ -272,8 +274,9 @@ func DeleteHostById(ctx context.Context, hostId string) (*Host, error) { // query cache host, ok := deviceCache.GetHostByIdOk(hostId) if !ok { - logger.V(2).Info("host not found during delete: do nothing", "hostId", hostId) - return &Host{Id: hostId}, nil + newErr := fmt.Errorf("failed to get host [%s]", hostId) + logger.Error(newErr, "failure: delete host by id") + return nil, &common.RequestError{StatusCode: common.StatusHostIdDoesNotExist, Err: newErr} } ops := host.backendOps @@ -319,7 +322,8 @@ func GetHostById(ctx context.Context, hostId string) (*Host, error) { host, err := deviceCache.GetHostById(hostId) if err != nil { logger.Error(err, "failure: get host by id") - return nil, &common.RequestError{StatusCode: common.StatusHostIdDoesNotExist, Err: err} + newErr := fmt.Errorf("failure: get host by id [%s]", hostId) + return nil, &common.RequestError{StatusCode: err.(*common.RequestError).StatusCode, Err: newErr} } logger.V(2).Info("success: get host by id", "hostId", hostId) diff --git a/pkg/manager/memory.go b/pkg/manager/memory.go index 62bd6ae..65d6db1 100644 --- a/pkg/manager/memory.go +++ b/pkg/manager/memory.go @@ -33,7 +33,7 @@ type BladeMemory struct { backendOps backend.BackendOperations } -func NewBladeMemoryById(ctx context.Context, applianceId, bladeId, memoryId string, ops backend.BackendOperations) (*BladeMemory, error) { +func NewBladeMemoryById(ctx context.Context, applianceId, bladeId, memoryId string, ops backend.BackendOperations) *BladeMemory { logger := klog.FromContext(ctx) logger.V(4).Info(">>>>>> NewBladeMemoryById: ", "memoryId", memoryId, "bladeId", bladeId, "applianceId", applianceId, "backend", ops.GetBackendInfo(ctx).BackendName) @@ -48,7 +48,7 @@ func NewBladeMemoryById(ctx context.Context, applianceId, bladeId, memoryId stri logger.V(2).Info("success: new blade memory", "memoryId", m.Id, "bladeId", m.BladeId, "applianceId", m.ApplianceId) - return &m, nil + return &m } func (m *BladeMemory) GetDetails(ctx context.Context) (openapi.MemoryRegion, error) { @@ -70,7 +70,7 @@ func (m *BladeMemory) GetDetails(ctx context.Context) (openapi.MemoryRegion, err } newErr := fmt.Errorf("failed to get memory by id (backend): appliance [%s] blade [%s] memory [%s]: %w", m.ApplianceId, m.BladeId, m.Id, err) logger.Error(newErr, "failure: get details") - return openapi.MemoryRegion{}, &common.RequestError{StatusCode: common.StatusApplianceGetMemoryByIdFailure, Err: newErr} + return openapi.MemoryRegion{}, &common.RequestError{StatusCode: common.StatusBladeGetMemoryByIdFailure, Err: newErr} } m.details = openapi.MemoryRegion{ @@ -121,7 +121,7 @@ func (m *BladeMemory) init(ctx context.Context) error { if err != nil { newErr := fmt.Errorf("memory [%s] init failed on blade [%s]: %w", m.Id, m.BladeId, err) logger.Error(newErr, "failure: init blade memory") - return newErr + return &common.RequestError{StatusCode: err.(*common.RequestError).StatusCode, Err: newErr} } logger.V(2).Info("success: init blade memory", "memoryId", m.Id, "bladeId", m.BladeId, "applianceId", m.ApplianceId) diff --git a/pkg/manager/port.go b/pkg/manager/port.go index 0e08ec3..c99bf65 100644 --- a/pkg/manager/port.go +++ b/pkg/manager/port.go @@ -35,7 +35,7 @@ type CxlBladePort struct { backendOps backend.BackendOperations } -func NewCxlBladePortById(ctx context.Context, applianceId, bladeId, portId string, ops backend.BackendOperations) (*CxlBladePort, error) { +func NewCxlBladePortById(ctx context.Context, applianceId, bladeId, portId string, ops backend.BackendOperations) (*CxlBladePort) { logger := klog.FromContext(ctx) logger.V(4).Info(">>>>>> NewCxlBladePortById: ", "portId", portId, "bladeId", bladeId, "applianceId", applianceId, "backend", ops.GetBackendInfo(ctx).BackendName) @@ -50,7 +50,7 @@ func NewCxlBladePortById(ctx context.Context, applianceId, bladeId, portId strin logger.V(2).Info("success: new cxl blade port", "portId", p.Id, "bladeId", p.BladeId, "applianceId", p.ApplianceId) - return &p, nil + return &p } func (p *CxlBladePort) GetDetails(ctx context.Context) (openapi.PortInformation, error) { @@ -125,7 +125,7 @@ func (p *CxlBladePort) init(ctx context.Context) error { if err != nil { newErr := fmt.Errorf("port [%s] init failed on blade [%s]: %w", p.Id, p.BladeId, err) logger.Error(newErr, "failure: init blade port") - return newErr + return &common.RequestError{StatusCode: err.(*common.RequestError).StatusCode, Err: newErr} } logger.V(2).Info("success: init blade port", "portId", p.Id, "bladeId", p.BladeId, "applianceId", p.ApplianceId) diff --git a/pkg/manager/resource.go b/pkg/manager/resource.go index 718f760..cc584c1 100644 --- a/pkg/manager/resource.go +++ b/pkg/manager/resource.go @@ -73,7 +73,7 @@ func (r *BladeResource) GetDetails(ctx context.Context) (openapi.MemoryResourceB if err != nil || response == nil { newErr := fmt.Errorf("failed to get resource by id (backend): appliance [%s] blade [%s] resource [%s]: %w", r.ApplianceId, r.BladeId, r.Id, err) logger.Error(newErr, "failure: get details") - return openapi.MemoryResourceBlock{}, &common.RequestError{StatusCode: common.StatusGetMemoryResourceBlockByIdFailure, Err: newErr} + return openapi.MemoryResourceBlock{}, &common.RequestError{StatusCode: common.StatusBladeGetMemoryResourceBlockDetailsFailure, Err: newErr} } r.details = openapi.MemoryResourceBlock{ @@ -161,7 +161,7 @@ func (r *BladeResource) init(ctx context.Context) error { if err != nil { newErr := fmt.Errorf("resource [%s] init failed on blade [%s]: %w", r.Id, r.BladeId, err) logger.Error(newErr, "failure: init blade resource") - return newErr + return &common.RequestError{StatusCode: err.(*common.RequestError).StatusCode, Err: newErr} } logger.V(2).Info("success: init blade resource", "resourceId", r.Id, "bladeId", r.BladeId, "applianceId", r.ApplianceId) From dad726c4025bb58838f01dc8f8a91afe4e544beb Mon Sep 17 00:00:00 2001 From: Mengling Ding Date: Wed, 21 Aug 2024 17:27:53 -0500 Subject: [PATCH 2/4] feat: pass the client errors to cfm-webui --- pkg/api/api_default_service.go | 7 +---- pkg/common/parameters.go | 5 --- .../Appliance/ComposeMemoryButton.vue | 4 +-- webui/src/components/Appliance/Memory.vue | 2 +- webui/src/components/Stores/ApplianceStore.ts | 21 +++++++++++-- .../src/components/Stores/BladeMemoryStore.ts | 31 +++++++++++++++++-- webui/src/components/Stores/BladeStore.ts | 31 +++++++++++++++++-- webui/src/components/Stores/HostStore.ts | 31 +++++++++++++++++-- 8 files changed, 106 insertions(+), 26 deletions(-) diff --git a/pkg/api/api_default_service.go b/pkg/api/api_default_service.go index 1b3aaec..5d489e7 100644 --- a/pkg/api/api_default_service.go +++ b/pkg/api/api_default_service.go @@ -235,12 +235,7 @@ func (cfm *CfmApiService) BladesComposeMemory(ctx context.Context, applianceId s memory, err := blade.ComposeMemory(ctx, &r) if err != nil { if memory != nil { - // Use the custom response structure to pass both the memory and partial error to the frontend. - customResponse := common.CustomResponse{ - Data: memory, - Error: err.Error(), - } - return openapi.Response(http.StatusPartialContent, customResponse), nil + return openapi.Response(http.StatusPartialContent, memory), nil } else { return formatErrorResp(ctx, err.(*common.RequestError)) } diff --git a/pkg/common/parameters.go b/pkg/common/parameters.go index 0c502aa..533c96e 100644 --- a/pkg/common/parameters.go +++ b/pkg/common/parameters.go @@ -34,11 +34,6 @@ type Settings struct { HostIpOverride string // An user override option for the host server Ip address } -type CustomResponse struct { - Data interface{} `json:"data,omitempty"` - Error string `json:"error,omitempty"` -} - const ( KeyVerbosity = "cfmCtxVerbosity" KeyBackend = "cfmCtxBackend" diff --git a/webui/src/components/Appliance/ComposeMemoryButton.vue b/webui/src/components/Appliance/ComposeMemoryButton.vue index 48232fc..5d4b91f 100644 --- a/webui/src/components/Appliance/ComposeMemoryButton.vue +++ b/webui/src/components/Appliance/ComposeMemoryButton.vue @@ -153,8 +153,6 @@ >

Compose memory failed!

- Error Message: -
{{ composeMemoryError }}

@@ -269,7 +267,7 @@ export default { }, qoSs: [Qos.NUMBER_1, Qos.NUMBER_2, Qos.NUMBER_4, Qos.NUMBER_8], showQoS: Qos.NUMBER_1, - showPort: "", + showPort: "port0", partialSuccess: "", newMemoryId: "", // Be used on success popup diff --git a/webui/src/components/Appliance/Memory.vue b/webui/src/components/Appliance/Memory.vue index 130033c..62d7c32 100644 --- a/webui/src/components/Appliance/Memory.vue +++ b/webui/src/components/Appliance/Memory.vue @@ -294,7 +294,7 @@ >

Free memory failed!

- Error: {{ this.freeMemoryError }} + {{ this.freeMemoryError }}

diff --git a/webui/src/components/Stores/ApplianceStore.ts b/webui/src/components/Stores/ApplianceStore.ts index 2d21f0f..39ef5b4 100644 --- a/webui/src/components/Stores/ApplianceStore.ts +++ b/webui/src/components/Stores/ApplianceStore.ts @@ -2,6 +2,7 @@ import { defineStore } from 'pinia' import { Appliance, Credentials, DefaultApi } from "@/axios/api"; import { BASE_PATH } from "@/axios/base"; +import axios from 'axios'; // Use API_BASE_PATH to overwrite the BASE_PATH in the generated client code const API_BASE_PATH = process.env.BASE_PATH || BASE_PATH; @@ -69,7 +70,15 @@ export const useApplianceStore = defineStore('appliance', { this.appliances.push(addedAppliance); return addedAppliance; } catch (error) { - this.addApplianceError = error; + if (axios.isAxiosError(error)) { + this.addApplianceError = error.message; + if (error.response) { + this.addApplianceError = error.response?.data.status.message + " (" + error.message + ")"; + } + } + else { + this.addApplianceError = error; + } console.error("Error:", error); } }, @@ -89,7 +98,15 @@ export const useApplianceStore = defineStore('appliance', { } return deletedAppliance; } catch (error) { - this.deleteApplianceError = error; + if (axios.isAxiosError(error)) { + this.deleteApplianceError = error.message; + if (error.response) { + this.deleteApplianceError = error.response?.data.status.message + " (" + error.message + ")"; + } + } + else { + this.deleteApplianceError = error; + } console.error("Error:", error); } }, diff --git a/webui/src/components/Stores/BladeMemoryStore.ts b/webui/src/components/Stores/BladeMemoryStore.ts index 1810fd3..d9c01f5 100644 --- a/webui/src/components/Stores/BladeMemoryStore.ts +++ b/webui/src/components/Stores/BladeMemoryStore.ts @@ -2,6 +2,7 @@ import { defineStore } from 'pinia' import { MemoryRegion, DefaultApi, ComposeMemoryRequest, AssignMemoryRequest } from "@/axios/api"; import { BASE_PATH } from "@/axios/base"; +import axios from 'axios'; // Use API_BASE_PATH to overwrite the BASE_PATH in the generated client code const API_BASE_PATH = process.env.BASE_PATH || BASE_PATH; @@ -63,7 +64,15 @@ export const useBladeMemoryStore = defineStore('bladeMemory', { this.bladeMemory.push(newMemory); return newMemory; } catch (error) { - this.composeMemoryError = error; + if (axios.isAxiosError(error)) { + this.composeMemoryError = error.message; + if (error.response) { + this.composeMemoryError = error.response?.data.status.message + " (" + error.message + ")"; + } + } + else { + this.composeMemoryError = error; + } console.error("Error:", error); } }, @@ -79,7 +88,15 @@ export const useBladeMemoryStore = defineStore('bladeMemory', { ); return response; } catch (error) { - this.assignOrUnassignMemoryError = error; + if (axios.isAxiosError(error)) { + this.assignOrUnassignMemoryError = error.message; + if (error.response) { + this.assignOrUnassignMemoryError = error.response?.data.status.message + " (" + error.message + ")"; + } + } + else { + this.assignOrUnassignMemoryError = error; + } console.error("Error assign or unassign memory:", error); } }, @@ -100,7 +117,15 @@ export const useBladeMemoryStore = defineStore('bladeMemory', { } return response; } catch (error) { - this.freeMemoryError = error; + if (axios.isAxiosError(error)) { + this.freeMemoryError = error.message; + if (error.response) { + this.freeMemoryError = error.response?.data.status.message + " (" + error.message + ")"; + } + } + else { + this.freeMemoryError = error; + } console.error("Error free memory:", error); } } diff --git a/webui/src/components/Stores/BladeStore.ts b/webui/src/components/Stores/BladeStore.ts index bb1e57f..59fcafe 100644 --- a/webui/src/components/Stores/BladeStore.ts +++ b/webui/src/components/Stores/BladeStore.ts @@ -2,6 +2,7 @@ import { defineStore } from 'pinia' import { Blade, Credentials, DefaultApi } from "@/axios/api"; import { BASE_PATH } from "@/axios/base"; +import axios from 'axios'; // Use API_BASE_PATH to overwrite the BASE_PATH in the generated client code const API_BASE_PATH = process.env.BASE_PATH || BASE_PATH; @@ -75,7 +76,15 @@ export const useBladeStore = defineStore('blade', { const resyncedBlade = response.data; return resyncedBlade; } catch (error) { - this.resyncBladeError = error; + if (axios.isAxiosError(error)) { + this.resyncBladeError = error.message; + if (error.response) { + this.resyncBladeError = error.response?.data.status.message + " (" + error.message + ")"; + } + } + else { + this.resyncBladeError = error; + } console.error("Error:", error); } }, @@ -94,7 +103,15 @@ export const useBladeStore = defineStore('blade', { this.blades.push(newBlade); return newBlade; } catch (error) { - this.addBladeError = error; + if (axios.isAxiosError(error)) { + this.addBladeError = error.message; + if (error.response) { + this.addBladeError = error.response?.data.status.message + " (" + error.message + ")"; + } + } + else { + this.addBladeError = error; + } console.error("Error:", error); } }, @@ -114,7 +131,15 @@ export const useBladeStore = defineStore('blade', { } return deletedBlade; } catch (error) { - this.deleteBladeError = error; + if (axios.isAxiosError(error)) { + this.deleteBladeError = error.message; + if (error.response) { + this.deleteBladeError = error.response?.data.status.message + " (" + error.message + ")"; + } + } + else { + this.deleteBladeError = error; + } console.error("Error:", error); } }, diff --git a/webui/src/components/Stores/HostStore.ts b/webui/src/components/Stores/HostStore.ts index 4caadfe..daf649d 100644 --- a/webui/src/components/Stores/HostStore.ts +++ b/webui/src/components/Stores/HostStore.ts @@ -2,6 +2,7 @@ import { defineStore } from 'pinia' import { Host, Credentials, DefaultApi } from "@/axios/api"; import { BASE_PATH } from "@/axios/base"; +import axios from 'axios'; // Use API_BASE_PATH to overwrite the BASE_PATH in the generated client code const API_BASE_PATH = process.env.BASE_PATH || BASE_PATH; @@ -59,7 +60,15 @@ export const useHostStore = defineStore('host', { this.hosts.push(addedHost); return addedHost; } catch (error) { - this.addHostError = error; + if (axios.isAxiosError(error)) { + this.addHostError = error.message; + if (error.response) { + this.addHostError = error.response?.data.status.message + " (" + error.message + ")"; + } + } + else { + this.addHostError = error; + } console.error("Error:", error); } }, @@ -79,7 +88,15 @@ export const useHostStore = defineStore('host', { } return deletedHost; } catch (error) { - this.deleteHostError = error; + if (axios.isAxiosError(error)) { + this.deleteHostError = error.message; + if (error.response) { + this.deleteHostError = error.response?.data.status.message + " (" + error.message + ")"; + } + } + else { + this.deleteHostError = error; + } console.error("Error:", error); } }, @@ -93,7 +110,15 @@ export const useHostStore = defineStore('host', { const resyncedHost = response.data; return resyncedHost; } catch (error) { - this.resyncHostError = error; + if (axios.isAxiosError(error)) { + this.resyncHostError = error.message; + if (error.response) { + this.resyncHostError = error.response?.data.status.message + " (" + error.message + ")"; + } + } + else { + this.resyncHostError = error; + } console.error("Error:", error); } }, From 2ee38b30c4bf447f5bc1e579371b0fc0307d3cdc Mon Sep 17 00:00:00 2001 From: Mengling Ding Date: Wed, 21 Aug 2024 17:29:58 -0500 Subject: [PATCH 3/4] fix: reset the port in compose memory popup --- webui/src/components/Appliance/ComposeMemoryButton.vue | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/webui/src/components/Appliance/ComposeMemoryButton.vue b/webui/src/components/Appliance/ComposeMemoryButton.vue index 5d4b91f..fb14720 100644 --- a/webui/src/components/Appliance/ComposeMemoryButton.vue +++ b/webui/src/components/Appliance/ComposeMemoryButton.vue @@ -267,7 +267,7 @@ export default { }, qoSs: [Qos.NUMBER_1, Qos.NUMBER_2, Qos.NUMBER_4, Qos.NUMBER_8], showQoS: Qos.NUMBER_1, - showPort: "port0", + showPort: "", partialSuccess: "", newMemoryId: "", // Be used on success popup From 4876edcb50b4cce6c354a9180f66278d41d66bc9 Mon Sep 17 00:00:00 2001 From: Mengling Ding Date: Thu, 22 Aug 2024 11:12:08 -0500 Subject: [PATCH 4/4] feat: only display the http code after the client error --- webui/src/components/Appliance/ComposeMemoryButton.vue | 2 +- webui/src/components/Stores/ApplianceStore.ts | 4 ++-- webui/src/components/Stores/BladeMemoryStore.ts | 6 +++--- webui/src/components/Stores/BladeStore.ts | 6 +++--- webui/src/components/Stores/HostStore.ts | 6 +++--- 5 files changed, 12 insertions(+), 12 deletions(-) diff --git a/webui/src/components/Appliance/ComposeMemoryButton.vue b/webui/src/components/Appliance/ComposeMemoryButton.vue index fb14720..aee07d5 100644 --- a/webui/src/components/Appliance/ComposeMemoryButton.vue +++ b/webui/src/components/Appliance/ComposeMemoryButton.vue @@ -354,7 +354,7 @@ export default { this.newMemoryCredentials.port ) { this.partialSuccess = - "Note: Memory allocation succeeded but memory port assignment failed."; + "Memory allocation succeeded but memory port assignment failed (206)"; } // Get the size of the new memory chunk to update the blade memory, // The final allocated memory size may be changed by the compose memory algorithm, so it may not be the same with the input one diff --git a/webui/src/components/Stores/ApplianceStore.ts b/webui/src/components/Stores/ApplianceStore.ts index 39ef5b4..530647e 100644 --- a/webui/src/components/Stores/ApplianceStore.ts +++ b/webui/src/components/Stores/ApplianceStore.ts @@ -73,7 +73,7 @@ export const useApplianceStore = defineStore('appliance', { if (axios.isAxiosError(error)) { this.addApplianceError = error.message; if (error.response) { - this.addApplianceError = error.response?.data.status.message + " (" + error.message + ")"; + this.addApplianceError = error.response?.data.status.message + " (" + error.response?.request.status + ")"; } } else { @@ -101,7 +101,7 @@ export const useApplianceStore = defineStore('appliance', { if (axios.isAxiosError(error)) { this.deleteApplianceError = error.message; if (error.response) { - this.deleteApplianceError = error.response?.data.status.message + " (" + error.message + ")"; + this.deleteApplianceError = error.response?.data.status.message + " (" + error.response?.request.status + ")"; } } else { diff --git a/webui/src/components/Stores/BladeMemoryStore.ts b/webui/src/components/Stores/BladeMemoryStore.ts index d9c01f5..6c1e993 100644 --- a/webui/src/components/Stores/BladeMemoryStore.ts +++ b/webui/src/components/Stores/BladeMemoryStore.ts @@ -67,7 +67,7 @@ export const useBladeMemoryStore = defineStore('bladeMemory', { if (axios.isAxiosError(error)) { this.composeMemoryError = error.message; if (error.response) { - this.composeMemoryError = error.response?.data.status.message + " (" + error.message + ")"; + this.composeMemoryError = error.response?.data.status.message + " (" + error.response?.request.status + ")"; } } else { @@ -91,7 +91,7 @@ export const useBladeMemoryStore = defineStore('bladeMemory', { if (axios.isAxiosError(error)) { this.assignOrUnassignMemoryError = error.message; if (error.response) { - this.assignOrUnassignMemoryError = error.response?.data.status.message + " (" + error.message + ")"; + this.assignOrUnassignMemoryError = error.response?.data.status.message + " (" + error.response?.request.status + ")"; } } else { @@ -120,7 +120,7 @@ export const useBladeMemoryStore = defineStore('bladeMemory', { if (axios.isAxiosError(error)) { this.freeMemoryError = error.message; if (error.response) { - this.freeMemoryError = error.response?.data.status.message + " (" + error.message + ")"; + this.freeMemoryError = error.response?.data.status.message + " (" + error.response?.request.status + ")"; } } else { diff --git a/webui/src/components/Stores/BladeStore.ts b/webui/src/components/Stores/BladeStore.ts index 59fcafe..b93761c 100644 --- a/webui/src/components/Stores/BladeStore.ts +++ b/webui/src/components/Stores/BladeStore.ts @@ -79,7 +79,7 @@ export const useBladeStore = defineStore('blade', { if (axios.isAxiosError(error)) { this.resyncBladeError = error.message; if (error.response) { - this.resyncBladeError = error.response?.data.status.message + " (" + error.message + ")"; + this.resyncBladeError = error.response?.data.status.message + " (" + error.response?.request.status + ")"; } } else { @@ -106,7 +106,7 @@ export const useBladeStore = defineStore('blade', { if (axios.isAxiosError(error)) { this.addBladeError = error.message; if (error.response) { - this.addBladeError = error.response?.data.status.message + " (" + error.message + ")"; + this.addBladeError = error.response?.data.status.message + " (" + error.response?.request.status + ")"; } } else { @@ -134,7 +134,7 @@ export const useBladeStore = defineStore('blade', { if (axios.isAxiosError(error)) { this.deleteBladeError = error.message; if (error.response) { - this.deleteBladeError = error.response?.data.status.message + " (" + error.message + ")"; + this.deleteBladeError = error.response?.data.status.message + " (" + error.response?.request.status + ")"; } } else { diff --git a/webui/src/components/Stores/HostStore.ts b/webui/src/components/Stores/HostStore.ts index daf649d..3c04cae 100644 --- a/webui/src/components/Stores/HostStore.ts +++ b/webui/src/components/Stores/HostStore.ts @@ -63,7 +63,7 @@ export const useHostStore = defineStore('host', { if (axios.isAxiosError(error)) { this.addHostError = error.message; if (error.response) { - this.addHostError = error.response?.data.status.message + " (" + error.message + ")"; + this.addHostError = error.response?.data.status.message + " (" + error.response?.request.status + ")"; } } else { @@ -91,7 +91,7 @@ export const useHostStore = defineStore('host', { if (axios.isAxiosError(error)) { this.deleteHostError = error.message; if (error.response) { - this.deleteHostError = error.response?.data.status.message + " (" + error.message + ")"; + this.deleteHostError = error.response?.data.status.message + " (" + error.response?.request.status + ")"; } } else { @@ -113,7 +113,7 @@ export const useHostStore = defineStore('host', { if (axios.isAxiosError(error)) { this.resyncHostError = error.message; if (error.response) { - this.resyncHostError = error.response?.data.status.message + " (" + error.message + ")"; + this.resyncHostError = error.response?.data.status.message + " (" + error.response?.request.status + ")"; } } else {