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
namespace Devolutions.Cryptography
{
using System;
/// <summary>
/// Wrapper for native exceptions & managed exceptions in the library.
/// </summary>
public class DevolutionsCryptoException : Exception
{
/// <summary>
/// Initializes a new instance of the <see cref="DevolutionsCryptoException"/> class.
/// </summary>
/// <param name="managedError">The managed error code.</param>
/// <param name="message">The exception message. (Optional).</param>
/// <param name="exception">The managed exception. (Optional).</param>
public DevolutionsCryptoException(ManagedError managedError, string message = null, Exception exception = null) : base(message)
{
this.ManagedError = managedError;
this.ManagedException = exception;
}
/// <summary>
/// Initializes a new instance of the <see cref="DevolutionsCryptoException"/> class.
/// </summary>
/// <param name="nativeError">The native error code.</param>
/// <param name="message">The exception message. (Optional).</param>
/// <param name="exception">The managed exception. (Optional).</param>
public DevolutionsCryptoException(NativeError nativeError, string message = null, Exception exception = null) : base(message)
{
this.NativeError = nativeError;
this.ManagedException = exception;
}
/// <summary>
/// Gets or sets if not null, this describes the error that happenened on the managed side.
/// </summary>
public ManagedError? ManagedError { get; set; }
/// <summary>
/// Gets or sets if an unknown exception happens this property will contain it..
/// </summary>
public Exception ManagedException { get; set; }
/// <summary>
/// Gets override to add additionnal info in the exception message.
/// </summary>
public override string Message
{
get
{
return this.GetDetailedMessage() + "===Original Message===\r\n" + base.Message;
}
}
/// <summary>
/// Gets or sets if not null, this describes the error that happenened on the native side (Rust).
/// </summary>
public NativeError? NativeError { get; set; }
/// <summary>
/// Gets override to add additionnal info in the exception stacktrace.
/// </summary>
public override string StackTrace
{
get
{
return this.GetDetailedStackTrace() + "===Original StackTrace===\r\n" + base.StackTrace;
}
}
/// <summary>
/// The detailed message for the exception.
/// </summary>
/// <returns>The detailed message depending on the managed or native error.</returns>
public string GetDetailedMessage()
{
string result = string.Empty;
if (this.NativeError != null)
{
result = "NativeError :\r\n";
result = result + this.NativeError.Value.ToString() + "\r\n";
if (this.Data != null)
{
this.Data["NativeError"] = this.NativeError.Value.ToString();
}
}
if (this.ManagedError != null)
{
result = "ManagedError : \r\n";
result = result + this.ManagedError.Value.ToString() + "\r\n";
if (this.Data != null)
{
this.Data["ManagedError"] = this.ManagedError.Value.ToString();
}
if (this.ManagedException != null)
{
result = this.ManagedException.Message + "\r\n";
}
}
return result;
}
/// <summary>
/// The Detailed stacktrace for the exception.
/// </summary>
/// <returns>The detailed stack trace depending on the managed or native error.</returns>
public string GetDetailedStackTrace()
{
string result = string.Empty;
if (this.ManagedException != null)
{
result = "ManagedException :\r\n";
result += this.ManagedException.StackTrace + "\r\n";
}
return result;
}
}
}