From f36deb9933ab763141bbb64e1b8eecd75b520272 Mon Sep 17 00:00:00 2001 From: Nikita Voronchev Date: Thu, 30 May 2019 13:26:11 +0300 Subject: [PATCH] Add a stack trace of an exception to error messages --- src/ExportObject.cs | 18 ++++++++++++------ src/Protocol/MessageContainer.cs | 5 +++-- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/ExportObject.cs b/src/ExportObject.cs index 14c2eb3..5721557 100644 --- a/src/ExportObject.cs +++ b/src/ExportObject.cs @@ -7,6 +7,7 @@ using System.Collections.Generic; using System.Reflection; using System.Reflection.Emit; +using System.Text; using org.freedesktop.DBus; @@ -128,17 +129,22 @@ public virtual void HandleMethodCall (MessageContainer method_call) replyMsg.Signature = outSig; } else { // BusException allows precisely formatted Error messages. - BusException busException = raisedException as BusException; - if (busException != null) + if (raisedException is BusException busException) replyMsg = method_call.CreateError (busException.ErrorName, busException.ErrorMessage); else if (raisedException is ArgumentException && raisedException.TargetSite.Name == mi.Name) { // Name match trick above is a hack since we don't have the resolved MethodInfo. - ArgumentException argException = (ArgumentException)raisedException; - using (System.IO.StringReader sr = new System.IO.StringReader (argException.Message)) { + var argException = (ArgumentException)raisedException; + using (var sr = new System.IO.StringReader (argException.Message)) { replyMsg = method_call.CreateError ("org.freedesktop.DBus.Error.InvalidArgs", sr.ReadLine ()); } - } else - replyMsg = method_call.CreateError (Mapper.GetInterfaceName (raisedException.GetType ()), raisedException.Message); + } else { + var errorMessage = new StringBuilder (); + errorMessage.AppendLine (raisedException.Message); + errorMessage.AppendLine ("-- Original remote exception stack trace --"); + errorMessage.AppendLine (raisedException.StackTrace); + errorMessage.AppendLine ("-- (done) --"); + replyMsg = method_call.CreateError (Mapper.GetInterfaceName (raisedException.GetType ()), errorMessage.ToString ().Trim ()); + } } if (method_call.Sender != null) diff --git a/src/Protocol/MessageContainer.cs b/src/Protocol/MessageContainer.cs index 93b124f..8b97a31 100644 --- a/src/Protocol/MessageContainer.cs +++ b/src/Protocol/MessageContainer.cs @@ -33,7 +33,8 @@ public Message Message { public Message CreateError (string errorName, string errorMessage) { var message = Message; - MessageContainer error = new MessageContainer { + + var error = new MessageContainer { Type = MessageType.Error, ErrorName = errorName, ReplySerial = message.Header.Serial, @@ -41,7 +42,7 @@ public Message CreateError (string errorName, string errorMessage) Destination = Sender }; - MessageWriter writer = new MessageWriter (message.Header.Endianness); + var writer = new MessageWriter (message.Header.Endianness); writer.Write (errorMessage); message = error.Message; message.AttachBodyTo (writer);