From d1c54422c24057fa4d905080ef134f480dcda956 Mon Sep 17 00:00:00 2001 From: pf-lin Date: Mon, 30 Sep 2024 08:11:25 +0000 Subject: [PATCH] fix: refactor error handling in terminateProcedure function --- internal/sbi/consumer/nrf_service.go | 7 ++++--- pkg/service/init.go | 25 ++++++++++++++++++------- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/internal/sbi/consumer/nrf_service.go b/internal/sbi/consumer/nrf_service.go index 4fce4e4c..3aa24e07 100644 --- a/internal/sbi/consumer/nrf_service.go +++ b/internal/sbi/consumer/nrf_service.go @@ -167,13 +167,14 @@ func (s *nnrfService) RetrySendNFRegistration(maxRetry int) error { return fmt.Errorf("[SMF] Retry NF Registration has meet maximum") } -func (s *nnrfService) SendDeregisterNFInstance() (problemDetails *models.ProblemDetails, err error) { +func (s *nnrfService) SendDeregisterNFInstance() (err error) { logger.ConsumerLog.Infof("Send Deregister NFInstance") smfContext := s.consumer.Context() ctx, pd, err := smfContext.GetTokenCtx(models.ServiceName_NNRF_NFM, models.NrfNfManagementNfType_NRF) if err != nil { - return pd, err + logger.ConsumerLog.Errorf("Get token context failed, problem details: %+v", pd) + return err } client := s.getNFManagementClient(smfContext.NrfUri) @@ -183,7 +184,7 @@ func (s *nnrfService) SendDeregisterNFInstance() (problemDetails *models.Problem _, err = client.NFInstanceIDDocumentApi.DeregisterNFInstance(ctx, request) - return problemDetails, err + return err } func (s *nnrfService) SendSearchNFInstances( diff --git a/pkg/service/init.go b/pkg/service/init.go index 0a4341a1..82fe2cbe 100644 --- a/pkg/service/init.go +++ b/pkg/service/init.go @@ -9,6 +9,8 @@ import ( "github.com/sirupsen/logrus" + "github.com/free5gc/openapi" + "github.com/free5gc/openapi/nrf/NFManagement" smf_context "github.com/free5gc/smf/internal/context" "github.com/free5gc/smf/internal/logger" "github.com/free5gc/smf/internal/sbi" @@ -197,14 +199,23 @@ func (a *SmfApp) terminateProcedure() { logger.MainLog.Infof("Terminating SMF...") a.pfcpTerminate() // deregister with NRF - problemDetails, err := a.Consumer().SendDeregisterNFInstance() - if problemDetails != nil { - logger.MainLog.Errorf("Deregister NF instance Failed Problem[%+v]", problemDetails) - } else if err != nil { - logger.MainLog.Errorf("Deregister NF instance Error[%+v]", err) - } else { - logger.MainLog.Infof("Deregister from NRF successfully") + err := a.Consumer().SendDeregisterNFInstance() + if err != nil { + switch apiErr := err.(type) { + case openapi.GenericOpenAPIError: + switch errModel := apiErr.Model().(type) { + case NFManagement.DeregisterNFInstanceError: + pd := &errModel.ProblemDetails + logger.MainLog.Errorf("Deregister NF instance Failed Problem[%+v]", pd) + case error: + logger.MainLog.Errorf("Deregister NF instance Error[%+v]", err) + } + case error: + logger.MainLog.Errorf("Deregister NF instance Error[%+v]", err) + } } + logger.MainLog.Infof("Deregister from NRF successfully") + a.sbiServer.Stop() logger.MainLog.Infof("SMF SBI Server terminated") }