From 40eac4b3768b580d4a5d613868ebe636a154411f Mon Sep 17 00:00:00 2001 From: pf-lin Date: Tue, 24 Sep 2024 03:02:00 +0000 Subject: [PATCH] fix: refactor error handling in service functions --- internal/sbi/consumer/chf_service.go | 34 +++++++++++++++++++++------- internal/sbi/consumer/nrf_service.go | 30 +++++++++++++++++++----- internal/sbi/consumer/smf_service.go | 13 +++++++---- internal/sbi/consumer/udm_service.go | 26 +++++++++++++-------- 4 files changed, 74 insertions(+), 29 deletions(-) diff --git a/internal/sbi/consumer/chf_service.go b/internal/sbi/consumer/chf_service.go index 98643926..39d3ef11 100644 --- a/internal/sbi/consumer/chf_service.go +++ b/internal/sbi/consumer/chf_service.go @@ -146,10 +146,16 @@ func (s *nchfService) SendConvergedChargingRequest( switch err := localErr.(type) { case openapi.GenericOpenAPIError: - errorModel := err.Model().(ConvergedCharging.PostChargingDataError) - chargingDataRef := strings.Split(errorModel.Location, "/") - smContext.ChargingDataRef = chargingDataRef[len(chargingDataRef)-1] - return nil, &errorModel.ProblemDetails, nil + switch errModel := err.Model().(type) { + case ConvergedCharging.PostChargingDataError: + chargingDataRef := strings.Split(errModel.Location, "/") + smContext.ChargingDataRef = chargingDataRef[len(chargingDataRef)-1] + return nil, &errModel.ProblemDetails, nil + case error: + return nil, openapi.ProblemDetailsSystemFailure(errModel.Error()), nil + default: + return nil, nil, openapi.ReportError("openapi error") + } case error: return nil, openapi.ProblemDetailsSystemFailure(err.Error()), nil case nil: @@ -166,8 +172,14 @@ func (s *nchfService) SendConvergedChargingRequest( switch err := localErr.(type) { case openapi.GenericOpenAPIError: - errorModel := err.Model().(ConvergedCharging.UpdateChargingDataError) - return nil, &errorModel.ProblemDetails, nil + switch errModel := err.Model().(type) { + case ConvergedCharging.UpdateChargingDataError: + return nil, &errModel.ProblemDetails, nil + case error: + return nil, openapi.ProblemDetailsSystemFailure(errModel.Error()), nil + default: + return nil, nil, openapi.ReportError("openapi error") + } case error: return nil, openapi.ProblemDetailsSystemFailure(err.Error()), nil case nil: @@ -184,8 +196,14 @@ func (s *nchfService) SendConvergedChargingRequest( switch err := localErr.(type) { case openapi.GenericOpenAPIError: - errorModel := err.Model().(ConvergedCharging.ReleaseChargingDataError) - return nil, &errorModel.ProblemDetails, nil + switch errModel := err.Model().(type) { + case ConvergedCharging.ReleaseChargingDataError: + return nil, &errModel.ProblemDetails, nil + case error: + return nil, openapi.ProblemDetailsSystemFailure(errModel.Error()), nil + default: + return nil, nil, openapi.ReportError("openapi error") + } case error: return nil, openapi.ProblemDetailsSystemFailure(err.Error()), nil case nil: diff --git a/internal/sbi/consumer/nrf_service.go b/internal/sbi/consumer/nrf_service.go index c8d502f5..4fce4e4c 100644 --- a/internal/sbi/consumer/nrf_service.go +++ b/internal/sbi/consumer/nrf_service.go @@ -289,8 +289,14 @@ func (s *nnrfService) SendNFDiscoveryUDM() (*models.ProblemDetails, error) { switch err := localErr.(type) { case openapi.GenericOpenAPIError: - errorModel := err.Model().(NFDiscovery.SearchNFInstancesError) - return &errorModel.ProblemDetails, nil + switch errModel := err.Model().(type) { + case NFDiscovery.SearchNFInstancesError: + return &errModel.ProblemDetails, nil + case error: + return openapi.ProblemDetailsSystemFailure(errModel.Error()), nil + default: + return nil, openapi.ReportError("openapi error") + } case error: return openapi.ProblemDetailsSystemFailure(err.Error()), nil case nil: @@ -324,8 +330,14 @@ func (s *nnrfService) SendNFDiscoveryPCF() (*models.ProblemDetails, error) { switch err := localErr.(type) { case openapi.GenericOpenAPIError: - errorModel := err.Model().(NFDiscovery.SearchNFInstancesError) - return &errorModel.ProblemDetails, nil + switch errModel := err.Model().(type) { + case NFDiscovery.SearchNFInstancesError: + return &errModel.ProblemDetails, nil + case error: + return openapi.ProblemDetailsSystemFailure(errModel.Error()), nil + default: + return nil, openapi.ReportError("openapi error") + } case error: return openapi.ProblemDetailsSystemFailure(err.Error()), nil case nil: @@ -348,8 +360,14 @@ func (s *nnrfService) SendNFDiscoveryServingAMF(smContext *smf_context.SMContext switch err := localErr.(type) { case openapi.GenericOpenAPIError: - errorModel := err.Model().(NFDiscovery.SearchNFInstancesError) - return &errorModel.ProblemDetails, nil + switch errModel := err.Model().(type) { + case NFDiscovery.SearchNFInstancesError: + return &errModel.ProblemDetails, nil + case error: + return openapi.ProblemDetailsSystemFailure(errModel.Error()), nil + default: + return nil, openapi.ReportError("openapi error") + } case error: return openapi.ProblemDetailsSystemFailure(err.Error()), nil case nil: diff --git a/internal/sbi/consumer/smf_service.go b/internal/sbi/consumer/smf_service.go index 33f7bd8d..a0dfb7cb 100644 --- a/internal/sbi/consumer/smf_service.go +++ b/internal/sbi/consumer/smf_service.go @@ -58,16 +58,19 @@ func (s *nsmfService) SendSMContextStatusNotification(uri string) (*models.Probl switch err := localErr.(type) { case openapi.GenericOpenAPIError: - errorModel := err.Model().(PDUSession.PostSmContextsSmContextStatusNotificationPostError) - return &errorModel.ProblemDetails, nil - + switch errModel := err.Model().(type) { + case PDUSession.PostSmContextsSmContextStatusNotificationPostError: + return &errModel.ProblemDetails, nil + case error: + return openapi.ProblemDetailsSystemFailure(errModel.Error()), nil + default: + return nil, openapi.ReportError("openapi error") + } case error: return openapi.ProblemDetailsSystemFailure(err.Error()), nil - case nil: logger.PduSessLog.Tracef("Send SMContextStatus Notification Success") return nil, nil - default: logger.PduSessLog.Warnf("Send SMContextStatus Notification Unknown Error: %+v", err) return nil, openapi.ReportError("server no response") diff --git a/internal/sbi/consumer/udm_service.go b/internal/sbi/consumer/udm_service.go index 2e3915ce..0a7c9a09 100644 --- a/internal/sbi/consumer/udm_service.go +++ b/internal/sbi/consumer/udm_service.go @@ -117,17 +117,20 @@ func (s *nudmService) UeCmRegistration(smCtx *smf_context.SMContext) ( switch err := localErr.(type) { case openapi.GenericOpenAPIError: - errorModel := err.Model().(UEContextManagement.RegistrationError) - return &errorModel.ProblemDetails, nil - + switch errModel := err.Model().(type) { + case UEContextManagement.RegistrationError: + return &errModel.ProblemDetails, nil + case error: + return openapi.ProblemDetailsSystemFailure(errModel.Error()), nil + default: + return nil, openapi.ReportError("openapi error") + } case error: return openapi.ProblemDetailsSystemFailure(err.Error()), nil - case nil: logger.PduSessLog.Tracef("UECM Registration Success") smCtx.UeCmRegistered = true return nil, nil - default: return nil, openapi.ReportError("server no response") } @@ -157,17 +160,20 @@ func (s *nudmService) UeCmDeregistration(smCtx *smf_context.SMContext) (*models. switch err := localErr.(type) { case openapi.GenericOpenAPIError: - errorModel := err.Model().(UEContextManagement.SmfDeregistrationError) - return &errorModel.ProblemDetails, nil - + switch errModel := err.Model().(type) { + case UEContextManagement.SmfDeregistrationError: + return &errModel.ProblemDetails, nil + case error: + return openapi.ProblemDetailsSystemFailure(errModel.Error()), nil + default: + return nil, openapi.ReportError("openapi error") + } case error: return openapi.ProblemDetailsSystemFailure(err.Error()), nil - case nil: logger.PduSessLog.Tracef("UECM Deregistration Success") smCtx.UeCmRegistered = false return nil, nil - default: return nil, openapi.ReportError("server no response") }