Skip to content

Commit

Permalink
fix: optimize the client errors in cfm-service
Browse files Browse the repository at this point in the history
  • Loading branch information
Meng-20 committed Aug 20, 2024
1 parent 012ae65 commit c9f28a1
Show file tree
Hide file tree
Showing 11 changed files with 162 additions and 191 deletions.
7 changes: 6 additions & 1 deletion pkg/api/api_default_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down
5 changes: 5 additions & 0 deletions pkg/common/parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
211 changes: 83 additions & 128 deletions pkg/common/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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"

Expand All @@ -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,
Expand Down
Loading

0 comments on commit c9f28a1

Please sign in to comment.