-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathExceptionExtensions.cs
More file actions
65 lines (61 loc) · 2.87 KB
/
ExceptionExtensions.cs
File metadata and controls
65 lines (61 loc) · 2.87 KB
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
using System;
using System.Text;
namespace Platform.Exceptions
{
/// <summary>
/// <para>Provides a set of extension methods for <see cref="Exception"/> objects.</para>
/// </summary>
public static class ExceptionExtensions
{
/// <summary>
/// <para>Represents the separator used within the process of generating a representation string (<see cref="ToStringWithAllInnerExceptions(Exception)"/>) to separate different inner exceptions from each other. This field is constant.</para>
/// </summary>
public static readonly string ExceptionContentsSeparator = "---";
/// <summary>
/// <para>Represents a string returned from <see cref="ToStringWithAllInnerExceptions(Exception)"/> in the event of an unsuccessful attempt to format an exception. This field is a constant.</para>
/// </summary>
public static readonly string ExceptionStringBuildingFailed = "Unable to format exception.";
/// <summary>
/// <para>Ignores the exception, notifying the <see cref = "IgnoredExceptions" /> class about it.</para>
/// </summary>
/// <param name="exception"><para></para><para></para></param>
public static void Ignore(this Exception exception) => IgnoredExceptions.RaiseExceptionIgnoredEvent(exception);
/// <summary>
/// <para>Returns a string that represents the specified exception with all its inner exceptions.</para>
/// </summary>
/// <param name="exception"><para>The exception that will be represented as a string.</para></param>
/// <returns><para>A string that represents the specified exception with all its inner exceptions.</para></returns>
public static string ToStringWithAllInnerExceptions(this Exception exception)
{
try
{
var sb = new StringBuilder();
sb.BuildExceptionString(exception, 0);
return sb.ToString();
}
catch (Exception ex)
{
ex.Ignore();
return ExceptionStringBuildingFailed;
}
}
private static void BuildExceptionString(this StringBuilder sb, Exception exception, int level)
{
sb.Indent(level);
sb.AppendLine(exception.Message);
sb.Indent(level);
sb.AppendLine(ExceptionContentsSeparator);
if (exception.InnerException != null)
{
sb.Indent(level);
sb.AppendLine("Inner exception: ");
sb.BuildExceptionString(exception.InnerException, level + 1);
}
sb.Indent(level);
sb.AppendLine(ExceptionContentsSeparator);
sb.Indent(level);
sb.AppendLine(exception.StackTrace);
}
private static void Indent(this StringBuilder sb, int level) => sb.Append('\t', level);
}
}