-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathExceptionSupport.cs
156 lines (132 loc) · 5.83 KB
/
ExceptionSupport.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//-----------------------------------------------------------------------
// <copyright file="ExceptionSupport.cs" company="Akka.NET Project">
// Copyright (C) 2009-2022 Lightbend Inc. <http://www.lightbend.com>
// Copyright (C) 2013-2022 .NET Foundation <https://github.com/akkadotnet/akka.net>
// </copyright>
//-----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.Serialization.Formatters;
using Akka.Actor;
using Akka.Util;
using Akka.Util.Internal;
using Google.Protobuf;
using System.Runtime.Serialization;
using Akka.Remote.Serialization.Proto.Msg;
namespace Akka.Remote.Serialization
{
internal class ExceptionSupport
{
private readonly WrappedPayloadSupport _wrappedPayloadSupport;
private const BindingFlags All = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public;
private readonly HashSet<string> _defaultProperties = new HashSet<string>
{
"ClassName",
"Message",
"StackTraceString",
"Source",
"InnerException",
"HelpURL",
"RemoteStackTraceString",
"RemoteStackIndex",
"ExceptionMethod",
"HResult",
"Data",
"TargetSite",
"HelpLink",
"StackTrace",
"WatsonBuckets"
};
public ExceptionSupport(ExtendedActorSystem system)
{
_wrappedPayloadSupport = new WrappedPayloadSupport(system);
}
public byte[] SerializeException(Exception exception)
{
return ExceptionToProto(exception).ToByteArray();
}
internal Proto.Msg.ExceptionData ExceptionToProto(Exception exception)
{
return ExceptionToProtoNet(exception);
}
public Exception DeserializeException(byte[] bytes)
{
var proto = Proto.Msg.ExceptionData.Parser.ParseFrom(bytes);
return ExceptionFromProto(proto);
}
internal Exception ExceptionFromProto(Proto.Msg.ExceptionData proto)
{
return ExceptionFromProtoNet(proto);
}
private readonly FormatterConverter _defaultFormatterConverter = new FormatterConverter();
public Proto.Msg.ExceptionData ExceptionToProtoNet(Exception exception)
{
var message = new Proto.Msg.ExceptionData();
if (exception == null)
return message;
var exceptionType = exception.GetType();
message.TypeName = exceptionType.TypeQualifiedName();
message.Message = exception.Message;
message.StackTrace = exception.StackTrace ?? "";
message.Source = exception.Source ?? "";
message.InnerException = ExceptionToProto(exception.InnerException);
var serializable = exception as ISerializable;
var serializationInfo = new SerializationInfo(exceptionType, _defaultFormatterConverter);
serializable.GetObjectData(serializationInfo, new StreamingContext());
foreach (var info in serializationInfo)
{
if (_defaultProperties.Contains(info.Name)) continue;
if (info.Value is Exception exceptionValue)
{
var exceptionPayload = ExceptionToProto(exceptionValue);
var preparedValue = _wrappedPayloadSupport.PayloadToProto(exceptionPayload);
message.CustomFields.Add(info.Name, preparedValue);
} else
{
var preparedValue = _wrappedPayloadSupport.PayloadToProto(info.Value);
message.CustomFields.Add(info.Name, preparedValue);
}
}
return message;
}
public Exception ExceptionFromProtoNet(Proto.Msg.ExceptionData proto)
{
if (string.IsNullOrEmpty(proto.TypeName))
return null;
Type exceptionType = Type.GetType(proto.TypeName);
var serializationInfo = new SerializationInfo(exceptionType, _defaultFormatterConverter);
serializationInfo.AddValue("ClassName", proto.TypeName);
serializationInfo.AddValue("Message", ValueOrNull(proto.Message));
serializationInfo.AddValue("StackTraceString", ValueOrNull(proto.StackTrace));
serializationInfo.AddValue("Source", ValueOrNull(proto.Source));
serializationInfo.AddValue("InnerException", ExceptionFromProto(proto.InnerException));
serializationInfo.AddValue("HelpURL", string.Empty);
serializationInfo.AddValue("RemoteStackTraceString", null);
serializationInfo.AddValue("RemoteStackIndex", 0);
serializationInfo.AddValue("ExceptionMethod", null);
serializationInfo.AddValue("HResult", int.MinValue);
foreach (var field in proto.CustomFields)
{
var payload = _wrappedPayloadSupport.PayloadFrom(field.Value);
if (payload is ExceptionData exception)
payload = ExceptionFromProto(exception);
serializationInfo.AddValue(field.Key, payload);
}
Exception obj = null;
ConstructorInfo constructorInfo = exceptionType.GetConstructor(
All,
null,
new[] { typeof(SerializationInfo), typeof(StreamingContext) },
null);
if (constructorInfo != null)
{
object[] args = { serializationInfo, new StreamingContext() };
obj = constructorInfo.Invoke(args).AsInstanceOf<Exception>();
}
return obj;
}
private string ValueOrNull(string value)
=> string.IsNullOrEmpty(value) ? null : value;
}
}