@@ -549,54 +549,50 @@ public void RemoveCACertificate(string alias)
549549 throw new Exception ( e . Message ) ;
550550 }
551551 }
552-
552+
553553 /// <summary>
554554 /// Removes a certificate with private key from the device.
555555 /// </summary>
556556 /// <param name="alias">Unique identifier of the CA certificate to be removed</param>
557- public void RemoveCertificate ( string alias )
557+ public HttpResult RemoveCertificate ( string alias )
558558 {
559559 try
560560 {
561561 Logger . MethodEntry ( ) ;
562562
563+ var context = new HttpContext ( ) ;
564+
563565 var deleteCertResource = $ "{ Constants . RestApiEntryPoint } /certificates/{ alias } ";
564566 var httpResponse = ExecuteHttp ( deleteCertResource , Method . Delete ) ;
565567
566568 // Decode the HTTP response if failed
567569 if ( httpResponse is { IsSuccessful : false } )
568570 {
569- Logger . LogError ( $ "HTTP Request unsuccessful - HTTP Response: { DecodeHttpStatus ( httpResponse ) } ") ;
570- throw new Exception ( $ "HTTP Request unsuccessful.") ;
571+ var decodedStatus = DecodeHttpStatus ( httpResponse ) ;
572+
573+ Logger . LogWarning ( $ "HTTP Request unsuccessful - HTTP Response: { decodedStatus } ") ;
574+ context . AddWarning ( decodedStatus ) ;
571575 }
572576
573- // Decode the API response when HTTP response is successful
577+ // Decode the API response for more information
574578 if ( httpResponse != null && string . IsNullOrEmpty ( httpResponse . Content ) )
575579 {
576- throw new Exception ( "No content returned from HTTP Response" ) ;
577- }
578-
579- RestApiResponse apiResponse = JsonConvert . DeserializeObject < RestApiResponse > ( httpResponse . Content ) ;
580- if ( apiResponse . Status == Constants . Status . Success )
581- {
582- Logger . MethodExit ( ) ;
580+ Logger . LogError ( "No content returned from HTTP Response" ) ;
581+ context . AddError ( $ "No content returned from HTTP Response for { nameof ( Method . Delete ) } { deleteCertResource } ") ;
583582 }
584583 else
585584 {
586- ErrorData error = JsonConvert . DeserializeObject < ErrorData > ( httpResponse . Content ) ;
587-
588- // Check for error code 5 - "Validation error: Certificate is in use" or "Validation error: Certificate is not deletable" ---
589- // This will capture all device ID certs, which we do not want to delete anyway
590- if ( error . ErrorInfo is { Code : 5 , Message : "Validation error: Certificate is not deletable" or "Validation error: Certificate is in use" } )
591- {
592- Logger . LogWarning ( $ "API warning encountered - { error . ErrorInfo . Message } - (Code: { error . ErrorInfo . Code } )") ;
593- }
594- else
585+ RestApiResponse apiResponse = JsonConvert . DeserializeObject < RestApiResponse > ( httpResponse . Content ) ;
586+ if ( apiResponse . Status != Constants . Status . Success )
595587 {
596- throw new Exception (
597- $ "API error encountered - { error . ErrorInfo . Message } - (Code: { error . ErrorInfo . Code } )") ;
588+ ErrorData error = JsonConvert . DeserializeObject < ErrorData > ( httpResponse . Content ) ;
589+ Logger . LogWarning ( $ "API error encountered - { error . ErrorInfo . Message } - (Code: { error . ErrorInfo . Code } )") ;
590+ context . AddWarning ( $ "HTTP Request { nameof ( Method . Delete ) } { deleteCertResource } : API error encountered - { error . ErrorInfo . Message } - (Code: { error . ErrorInfo . Code } )") ;
598591 }
599592 }
593+
594+ Logger . MethodExit ( ) ;
595+ return context . ToResult ( ) ;
600596 }
601597 catch ( Exception e )
602598 {
0 commit comments