Skip to content
This repository has been archived by the owner on Nov 8, 2022. It is now read-only.

Add a stack trace of an exception to error messages #73

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions src/ExportObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Collections.Generic;
using System.Reflection;
using System.Reflection.Emit;
using System.Text;

using org.freedesktop.DBus;

Expand Down Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions src/Protocol/MessageContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,16 @@ 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,
Signature = Signature.StringSig,
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);
Expand Down