[BTLS]: Improve error handling. (#4317)
authorMartin Baulig <martin.baulig@xamarin.com>
Thu, 2 Feb 2017 17:43:02 +0000 (18:43 +0100)
committerGitHub <noreply@github.com>
Thu, 2 Feb 2017 17:43:02 +0000 (18:43 +0100)
* Add new native mono_btls_error_peek_error_line() and
  mono_btls_error_get_error_line().

* Add `MonoBtlsError.PeekError(out string, out int)` and
  `MonoBtlsError.GetError(out string, out int)` overloads.

* MonoBtlsContext.GetException(): use it here.

mcs/class/System/Mono.Btls/MonoBtlsContext.cs
mcs/class/System/Mono.Btls/MonoBtlsError.cs
mono/btls/btls-error.c
mono/btls/btls-error.h

index 339ce4e57ced28d5dc8fe88eea4da5206abda013..5bdac307dc4cadee764cf594332aa87d369d7dc1 100644 (file)
@@ -156,9 +156,20 @@ namespace Mono.Btls
 
                static Exception GetException (MonoBtlsSslError status)
                {
-                       var error = MonoBtlsError.GetError ();
+                       string file;
+                       int line;
+                       var error = MonoBtlsError.GetError (out file, out line);
+                       if (error == null)
+                               return new MonoBtlsException (status);
+
                        var text = MonoBtlsError.GetErrorString (error);
-                       return new MonoBtlsException ("{0} {1}", status, text);
+
+                       string message;
+                       if (file != null)
+                               message = string.Format ("{0} {1}\n  at {2}:{3}", status, text, file, line);
+                       else
+                               message = string.Format ("{0} {1}", status, text);
+                       return new MonoBtlsException (message);
                }
 
                public override bool ProcessHandshake ()
index 5e61bcf04634b91e2c80d4c93668ddd9ebae6034..8371bdd0a63c16d0f6fd86bc54946e5662bfbe24 100644 (file)
@@ -47,6 +47,12 @@ namespace Mono.Btls
                [DllImport (MonoBtlsObject.BTLS_DYLIB)]
                extern static void mono_btls_error_clear_error ();
 
+               [DllImport (MonoBtlsObject.BTLS_DYLIB)]
+               extern static int mono_btls_error_peek_error_line (out IntPtr file, out int line);
+
+               [DllImport (MonoBtlsObject.BTLS_DYLIB)]
+               extern static int mono_btls_error_get_error_line (out IntPtr file, out int line);
+
                [DllImport (MonoBtlsObject.BTLS_DYLIB)]
                extern static void mono_btls_error_get_error_string_n (int error, IntPtr buf, int len);
 
@@ -78,6 +84,28 @@ namespace Mono.Btls
                                Marshal.FreeHGlobal (buffer);
                        }
                }
+
+               public static int PeekError (out string file, out int line)
+               {
+                       IntPtr filePtr;
+                       var error = mono_btls_error_peek_error_line (out filePtr, out line);
+                       if (filePtr != IntPtr.Zero)
+                               file = Marshal.PtrToStringAnsi (filePtr);
+                       else
+                               file = null;
+                       return error;
+               }
+
+               public static int GetError (out string file, out int line)
+               {
+                       IntPtr filePtr;
+                       var error = mono_btls_error_get_error_line (out filePtr, out line);
+                       if (filePtr != IntPtr.Zero)
+                               file = Marshal.PtrToStringAnsi (filePtr);
+                       else
+                               file = null;
+                       return error;
+               }
        }
 }
 #endif
index 75252162a2a24392761f5ae7ec98ea0ce43a713a..3d81b32f341fb8cdda66cca272876624fde5d71e 100644 (file)
@@ -22,6 +22,18 @@ mono_btls_error_get_error (void)
        return ERR_get_error ();
 }
 
+MONO_API int
+mono_btls_error_peek_error_line (const char **file, int *line)
+{
+       return ERR_peek_error_line (file, line);
+}
+
+MONO_API int
+mono_btls_error_get_error_line (const char **file, int *line)
+{
+       return ERR_get_error_line (file, line);
+}
+
 MONO_API void
 mono_btls_error_clear_error (void)
 {
index 6f791c372a4eb61feadba5cd5ec29ce48bf16a26..418727e9a09048a1b087d5c604da7a54b19c5523 100644 (file)
@@ -23,6 +23,12 @@ mono_btls_error_get_error (void);
 void
 mono_btls_error_clear_error (void);
 
+int
+mono_btls_error_peek_error_line (const char **file, int *line);
+
+int
+mono_btls_error_get_error_line (const char **file, int *line);
+
 void
 mono_btls_error_get_error_string_n (int error, char *buf, int len);