|
3 | 3 | using System.Globalization;
|
4 | 4 | using System.Reflection;
|
5 | 5 | using System.Runtime.Serialization;
|
| 6 | +using Elasticsearch.Net.Extensions; |
6 | 7 | using Elasticsearch.Net.Utf8Json;
|
7 | 8 |
|
8 | 9 | namespace Elasticsearch.Net
|
9 | 10 | {
|
10 | 11 | internal class ExceptionFormatterResolver : IJsonFormatterResolver
|
11 | 12 | {
|
12 |
| - public static ExceptionFormatterResolver Instance = new ExceptionFormatterResolver(); |
| 13 | + public static readonly ExceptionFormatterResolver Instance = new ExceptionFormatterResolver(); |
13 | 14 |
|
14 | 15 | private ExceptionFormatterResolver() { }
|
15 | 16 |
|
16 |
| - private static readonly ExceptionFormatter ExceptionFormatter = new ExceptionFormatter(); |
17 |
| - |
18 | 17 | public IJsonFormatter<T> GetFormatter<T>()
|
19 | 18 | {
|
20 | 19 | if (typeof(Exception).IsAssignableFrom(typeof(T)))
|
21 |
| - return (IJsonFormatter<T>)ExceptionFormatter; |
| 20 | + { |
| 21 | + var type = typeof(ExceptionFormatter<>).MakeGenericType(typeof(T)); |
| 22 | + return (IJsonFormatter<T>)type.CreateInstance(); |
| 23 | + } |
22 | 24 |
|
23 | 25 | return null;
|
24 | 26 | }
|
25 | 27 | }
|
26 | 28 |
|
27 |
| - internal class ExceptionFormatter : IJsonFormatter<Exception> |
| 29 | + internal class ExceptionFormatter<TException> : IJsonFormatter<TException> where TException : Exception |
28 | 30 | {
|
29 |
| - private List<Dictionary<string, object>> FlattenExceptions(Exception e) |
| 31 | + private static List<Dictionary<string, object>> FlattenExceptions(Exception e) |
30 | 32 | {
|
31 | 33 | var maxExceptions = 20;
|
32 | 34 | var exceptions = new List<Dictionary<string, object>>(maxExceptions);
|
@@ -100,7 +102,7 @@ private static void WriteStructuredExceptionMethod(Dictionary<string,object> o,
|
100 | 102 | o.Add("ExceptionMethod", exceptionMethod);
|
101 | 103 | }
|
102 | 104 |
|
103 |
| - public void Serialize(ref JsonWriter writer, Exception value, IJsonFormatterResolver formatterResolver) |
| 105 | + public void Serialize(ref JsonWriter writer, TException value, IJsonFormatterResolver formatterResolver) |
104 | 106 | {
|
105 | 107 | if (value == null)
|
106 | 108 | {
|
@@ -134,7 +136,7 @@ public void Serialize(ref JsonWriter writer, Exception value, IJsonFormatterReso
|
134 | 136 | writer.WriteEndArray();
|
135 | 137 | }
|
136 | 138 |
|
137 |
| - public Exception Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver) => |
| 139 | + public TException Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver) => |
138 | 140 | throw new NotSupportedException();
|
139 | 141 | }
|
140 | 142 | }
|
0 commit comments