From a0f3ea2d3d6c411a65c7140b94588ef8991daf9d Mon Sep 17 00:00:00 2001 From: Eduardo Menezes Date: Tue, 31 Oct 2023 17:16:03 -0300 Subject: [PATCH] Enhance errors handling --- .../f7/Meadow.F7/F7PlatformOS.cs | 26 +++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/source/implementations/f7/Meadow.F7/F7PlatformOS.cs b/source/implementations/f7/Meadow.F7/F7PlatformOS.cs index 668d7bc8..acb28e94 100644 --- a/source/implementations/f7/Meadow.F7/F7PlatformOS.cs +++ b/source/implementations/f7/Meadow.F7/F7PlatformOS.cs @@ -93,6 +93,16 @@ public int[] GetProcessorUtilization() return new[] {100 - Core.Interop.Nuttx.meadow_idle_monitor_get_value()}; } + + /// + /// Enum representing different server certificate validation error returns. + /// + public enum ServerCertificateValidationError + { + InvalidMode = -1, + CannotChangeAfterInitialization = -2 + } + /// public void SetServerCertificateValidationMode(ServerCertificateValidationMode authmode) { @@ -107,12 +117,24 @@ public void SetServerCertificateValidationMode(ServerCertificateValidationMode a } int ret = Core.Interop.Nuttx.mono_mbedtls_set_server_cert_authmode(authModeInt); - if (ret < 0) + if (ret == (int)ServerCertificateValidationError.InvalidMode) { - string errorMessage = $"Error setting validation mode."; + string errorMessage = $"Invalid validation mode: {authModeInt}"; + Resolver.Log.Error($"Invalid validation mode: {authModeInt}"); + throw new ArgumentException(errorMessage); + } + else if (ret == (int)ServerCertificateValidationError.CannotChangeAfterInitialization) + { + string errorMessage = $"The server certificate validation mode cannot be changed after the TLS initialization."; Resolver.Log.Error(errorMessage); throw new Exception(errorMessage); } + else if (ret < 0) + { + string errorMessage = $"Error setting validation mode."; + Resolver.Log.Error(errorMessage); + throw new Exception(errorMessage); + } Resolver.Log.Trace($"Server certificate validation mode set to {authmode} successfully!");