Skip to content

Commit 47d83c1

Browse files
elachlanniemyjski
andauthored
Fix Exception.HResult is not being mapped to error.code property (#274)
* Fixes Exception.HResult is not being mapped to error.code property #273 * Refactor to make a bit more clean Co-authored-by: Blake Niemyjski <[email protected]>
1 parent dc84e00 commit 47d83c1

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

src/Exceptionless/Extensions/ToErrorModelExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ private static Error ToErrorModelInternal(Exception exception, ExceptionlessClie
5454
error.PopulateStackTrace(error, exception, log);
5555

5656
try {
57-
PropertyInfo info = type.GetProperty("HResult", BindingFlags.NonPublic | BindingFlags.Instance);
57+
PropertyInfo info = type.GetProperty("HResult", BindingFlags.Public | BindingFlags.Instance);
5858
if (info != null)
5959
error.Code = info.GetValue(exception, null).ToString();
6060
} catch (Exception) { }

test/Exceptionless.Tests/Plugins/020_ErrorPluginTests.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,5 +286,21 @@ public void WillUnwrapExceptionTypeName() {
286286
Assert.Equal(type, context.Event.GetSimpleError().Type);
287287
}
288288
}
289+
290+
[Fact]
291+
public void TrackException() {
292+
var plugin = new ErrorPlugin();
293+
var client = CreateClient();
294+
var context = new EventPluginContext(client, new Event());
295+
var exception = new TestOutOfMemoryException("Test");
296+
context.ContextData.SetException(exception);
297+
plugin.Run(context);
298+
Assert.False(context.Cancel);
299+
Assert.Equal(Event.KnownTypes.Error, context.Event.Type);
300+
var error = context.Event.GetError();
301+
if (error != null) {
302+
Assert.Equal(E_OUTOFMEMORY.ToString(), error.Code);
303+
}
304+
}
289305
}
290306
}

test/Exceptionless.Tests/Plugins/PluginTestBase.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,5 +90,18 @@ public enum ErrorCategory {
9090
FirstErrorBucket,
9191
SecondErrorBucket,
9292
}
93+
94+
/// <summary>
95+
/// Hresult for 'Failed to allocate necessary memory'.
96+
/// <see href="https://docs.microsoft.com/en-us/windows/win32/seccrypto/common-hresult-values"/>
97+
/// </summary>
98+
public const int E_OUTOFMEMORY = unchecked((int)0x8007000E);
99+
100+
public class TestOutOfMemoryException : Exception {
101+
public TestOutOfMemoryException(string message) : base(message) {
102+
// Out of Memory Exception
103+
HResult = E_OUTOFMEMORY;
104+
}
105+
}
93106
}
94107
}

0 commit comments

Comments
 (0)