IGNITE-4692 .NET: Add ClassName and Message to JavaException
Project: http://git-wip-us.apache.org/repos/asf/ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/d8ad967c Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/d8ad967c Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/d8ad967c Branch: refs/heads/ignite-3477-merge2.0 Commit: d8ad967c85fb56810ee7fab76494b176f443d773 Parents: 00dd3d8 Author: Pavel Tupitsyn <ptupit...@apache.org> Authored: Mon Feb 13 19:34:34 2017 +0300 Committer: Pavel Tupitsyn <ptupit...@apache.org> Committed: Mon Feb 13 19:34:34 2017 +0300 ---------------------------------------------------------------------- .../Apache.Ignite.Core.Tests/ExceptionsTest.cs | 28 +++++++++ .../Apache.Ignite.Core/Common/JavaException.cs | 62 +++++++++++++++++++- .../Apache.Ignite.Core/Impl/ExceptionUtils.cs | 4 +- 3 files changed, 91 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ignite/blob/d8ad967c/modules/platforms/dotnet/Apache.Ignite.Core.Tests/ExceptionsTest.cs ---------------------------------------------------------------------- diff --git a/modules/platforms/dotnet/Apache.Ignite.Core.Tests/ExceptionsTest.cs b/modules/platforms/dotnet/Apache.Ignite.Core.Tests/ExceptionsTest.cs index 8c23ab7..7afb10f 100644 --- a/modules/platforms/dotnet/Apache.Ignite.Core.Tests/ExceptionsTest.cs +++ b/modules/platforms/dotnet/Apache.Ignite.Core.Tests/ExceptionsTest.cs @@ -111,6 +111,34 @@ namespace Apache.Ignite.Core.Tests Assert.IsNotNull(javaEx); Assert.IsTrue(javaEx.Message.Contains("at " + ExceptionTask)); + Assert.AreEqual(name, javaEx.JavaMessage); + Assert.IsTrue(javaEx.JavaClassName.EndsWith("." + name)); + + // Check serialization. + var formatter = new BinaryFormatter(); + using (var ms = new MemoryStream()) + { + formatter.Serialize(ms, ex); + + ms.Seek(0, SeekOrigin.Begin); + + var res = (T) formatter.Deserialize(ms); + + Assert.AreEqual(ex.Message, res.Message); + Assert.AreEqual(ex.Source, res.Source); + Assert.AreEqual(ex.StackTrace, res.StackTrace); + Assert.AreEqual(ex.HelpLink, res.HelpLink); + + var resJavaEx = res.InnerException as JavaException; + + Assert.IsNotNull(resJavaEx); + Assert.AreEqual(javaEx.Message, resJavaEx.Message); + Assert.AreEqual(javaEx.JavaClassName, resJavaEx.JavaClassName); + Assert.AreEqual(javaEx.JavaMessage, resJavaEx.JavaMessage); + Assert.AreEqual(javaEx.StackTrace, resJavaEx.StackTrace); + Assert.AreEqual(javaEx.Source, resJavaEx.Source); + Assert.AreEqual(javaEx.HelpLink, resJavaEx.HelpLink); + } } /// <summary> http://git-wip-us.apache.org/repos/asf/ignite/blob/d8ad967c/modules/platforms/dotnet/Apache.Ignite.Core/Common/JavaException.cs ---------------------------------------------------------------------- diff --git a/modules/platforms/dotnet/Apache.Ignite.Core/Common/JavaException.cs b/modules/platforms/dotnet/Apache.Ignite.Core/Common/JavaException.cs index 5f7ba66..1988335 100644 --- a/modules/platforms/dotnet/Apache.Ignite.Core/Common/JavaException.cs +++ b/modules/platforms/dotnet/Apache.Ignite.Core/Common/JavaException.cs @@ -26,6 +26,18 @@ namespace Apache.Ignite.Core.Common [Serializable] public class JavaException : IgniteException { + /** JavaClassName field. */ + private const string JavaClassNameField = "JavaClassName"; + + /** JavaMessage field. */ + private const string JavaMessageField = "JavaMessage"; + + /** Java exception class name. */ + private readonly string _javaClassName; + + /** Java exception message. */ + private readonly string _javaMessage; + /// <summary> /// Initializes a new instance of the <see cref="JavaException"/> class. /// </summary> @@ -44,6 +56,21 @@ namespace Apache.Ignite.Core.Common } /// <summary> + /// Initializes a new instance of the <see cref="JavaException" /> class. + /// </summary> + /// <param name="javaClassName">Java exception class name.</param> + /// <param name="javaMessage">Java exception message.</param> + /// <param name="stackTrace">Java stack trace.</param> + public JavaException(string javaClassName, string javaMessage, string stackTrace) + : base(stackTrace ?? javaMessage) + { + // Send stackTrace to base ctor because it has all information, including class names and messages. + // Store ClassName and Message separately for mapping purposes. + _javaClassName = javaClassName; + _javaMessage = javaMessage; + } + + /// <summary> /// Initializes a new instance of the <see cref="JavaException"/> class. /// </summary> /// <param name="message">The message.</param> @@ -60,7 +87,40 @@ namespace Apache.Ignite.Core.Common /// <param name="ctx">Streaming context.</param> protected JavaException(SerializationInfo info, StreamingContext ctx) : base(info, ctx) { - // No-op. + _javaClassName = info.GetString(JavaClassNameField); + _javaMessage = info.GetString(JavaMessageField); + } + + /// <summary> + /// When overridden in a derived class, sets the <see cref="SerializationInfo" /> + /// with information about the exception. + /// </summary> + /// <param name="info">The <see cref="SerializationInfo" /> that holds the serialized object data + /// about the exception being thrown.</param> + /// <param name="context">The <see cref="StreamingContext" /> that contains contextual information + /// about the source or destination.</param> + public override void GetObjectData(SerializationInfo info, StreamingContext context) + { + base.GetObjectData(info, context); + + info.AddValue(JavaClassNameField, _javaClassName); + info.AddValue(JavaMessageField, _javaMessage); + } + + /// <summary> + /// Gets the Java exception class name. + /// </summary> + public string JavaClassName + { + get { return _javaClassName; } + } + + /// <summary> + /// Gets the Java exception message. + /// </summary> + public string JavaMessage + { + get { return _javaMessage; } } } } http://git-wip-us.apache.org/repos/asf/ignite/blob/d8ad967c/modules/platforms/dotnet/Apache.Ignite.Core/Impl/ExceptionUtils.cs ---------------------------------------------------------------------- diff --git a/modules/platforms/dotnet/Apache.Ignite.Core/Impl/ExceptionUtils.cs b/modules/platforms/dotnet/Apache.Ignite.Core/Impl/ExceptionUtils.cs index 64f3ccc..4534e91 100644 --- a/modules/platforms/dotnet/Apache.Ignite.Core/Impl/ExceptionUtils.cs +++ b/modules/platforms/dotnet/Apache.Ignite.Core/Impl/ExceptionUtils.cs @@ -124,8 +124,8 @@ namespace Apache.Ignite.Core.Impl BinaryReader reader = null, Exception innerException = null) { // Set JavaException as inner only if there is no InnerException. - if (innerException == null && !string.IsNullOrEmpty(stackTrace)) - innerException = new JavaException(stackTrace); + if (innerException == null) + innerException = new JavaException(clsName, msg, stackTrace); ExceptionFactoryDelegate ctor;