-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Low level client exception structure should match SimpleJson strategy #3662
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
src/Elasticsearch.Net/Serialization/Formatters/ExceptionFormatter.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Globalization; | ||
| using System.Reflection; | ||
| using System.Runtime.Serialization; | ||
|
|
||
| namespace Elasticsearch.Net | ||
| { | ||
| internal class ExceptionFormatterResolver : IJsonFormatterResolver | ||
| { | ||
| public static ExceptionFormatterResolver Instance = new ExceptionFormatterResolver(); | ||
|
|
||
| private ExceptionFormatterResolver() { } | ||
|
|
||
| private static readonly ExceptionFormatter ExceptionFormatter = new ExceptionFormatter(); | ||
|
|
||
| public IJsonFormatter<T> GetFormatter<T>() | ||
| { | ||
| if (typeof(Exception).IsAssignableFrom(typeof(T))) | ||
| return (IJsonFormatter<T>)ExceptionFormatter; | ||
|
|
||
| return null; | ||
| } | ||
| } | ||
|
|
||
| internal class ExceptionFormatter : IJsonFormatter<Exception> | ||
| { | ||
| private List<Dictionary<string, object>> FlattenExceptions(Exception e) | ||
| { | ||
| var maxExceptions = 20; | ||
| var exceptions = new List<Dictionary<string, object>>(maxExceptions); | ||
| var depth = 0; | ||
| do | ||
| { | ||
| var o = ToDictionary(e, depth); | ||
| exceptions.Add(o); | ||
| depth++; | ||
| e = e.InnerException; | ||
| } while (depth < maxExceptions && e != null); | ||
|
|
||
| return exceptions; | ||
| } | ||
|
|
||
| private static Dictionary<string, object> ToDictionary(Exception e, int depth) | ||
| { | ||
| var o = new Dictionary<string, object>(10); | ||
| var si = new SerializationInfo(e.GetType(), new FormatterConverter()); | ||
| var sc = new StreamingContext(); | ||
| e.GetObjectData(si, sc); | ||
|
|
||
| var helpUrl = si.GetString("HelpURL"); | ||
| var stackTrace = si.GetString("StackTraceString"); | ||
| var remoteStackTrace = si.GetString("RemoteStackTraceString"); | ||
| var remoteStackIndex = si.GetInt32("RemoteStackIndex"); | ||
| var exceptionMethod = si.GetString("ExceptionMethod"); | ||
| var hresult = si.GetInt32("HResult"); | ||
| var source = si.GetString("Source"); | ||
| var className = si.GetString("ClassName"); | ||
|
|
||
| o.Add("Depth", depth); | ||
| o.Add("ClassName", className); | ||
| o.Add("Message", e.Message); | ||
| o.Add("Source", source); | ||
| o.Add("StackTraceString", stackTrace); | ||
| o.Add("RemoteStackTraceString", remoteStackTrace); | ||
| o.Add("RemoteStackIndex", remoteStackIndex); | ||
| o.Add("HResult", hresult); | ||
| o.Add("HelpURL", helpUrl); | ||
|
|
||
| WriteStructuredExceptionMethod(o, exceptionMethod); | ||
| return o; | ||
| } | ||
|
|
||
| private static void WriteStructuredExceptionMethod(Dictionary<string,object> o, string exceptionMethodString) | ||
| { | ||
| if (string.IsNullOrWhiteSpace(exceptionMethodString)) return; | ||
|
|
||
| var args = exceptionMethodString.Split('\0', '\n'); | ||
|
|
||
| if (args.Length != 5) return; | ||
|
|
||
| var memberType = int.Parse(args[0], CultureInfo.InvariantCulture); | ||
| var name = args[1]; | ||
| var assemblyName = args[2]; | ||
| var className = args[3]; | ||
| var signature = args[4]; | ||
| var an = new AssemblyName(assemblyName); | ||
| var exceptionMethod = new Dictionary<string, object>(7) | ||
| { | ||
| { "Name", name }, | ||
| { "AssemblyName", an.Name }, | ||
| { "AssemblyVersion", an.Version.ToString() }, | ||
| { "AssemblyCulture", an.CultureName }, | ||
| { "ClassName", className }, | ||
| { "Signature", signature }, | ||
| { "MemberType", memberType } | ||
| }; | ||
|
|
||
| o.Add("ExceptionMethod", exceptionMethod); | ||
| } | ||
|
|
||
| public void Serialize(ref JsonWriter writer, Exception value, IJsonFormatterResolver formatterResolver) | ||
| { | ||
| if (value == null) | ||
| { | ||
| writer.WriteNull(); | ||
| return; | ||
| } | ||
|
|
||
| var flattenedExceptions = FlattenExceptions(value); | ||
| var formatter = formatterResolver.GetFormatter<List<Dictionary<string, object>>>(); | ||
| formatter.Serialize(ref writer, flattenedExceptions, formatterResolver); | ||
| } | ||
|
|
||
| public Exception Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver) => | ||
| throw new NotSupportedException(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
src/Tests/Tests/Framework/SerializationTests/ExceptionSerializationTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| using System; | ||
| using Elastic.Xunit.XunitPlumbing; | ||
| using Elasticsearch.Net; | ||
| using FluentAssertions; | ||
| using Newtonsoft.Json; | ||
| using Newtonsoft.Json.Linq; | ||
|
|
||
| namespace Tests.Framework.SerializationTests | ||
| { | ||
| public class ExceptionSerializationTests | ||
| { | ||
| private readonly IElasticsearchSerializer _elasticsearchNetSerializer; | ||
|
|
||
| private readonly Exception Exception = new Exception("outer_exception", | ||
| new InnerException("inner_exception", | ||
| new InnerInnerException("inner_inner_exception"))); | ||
|
|
||
| public ExceptionSerializationTests() | ||
| { | ||
| var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200")); | ||
| var connection = new InMemoryConnection(); | ||
| var values = new ConnectionConfiguration(pool, connection); | ||
| var lowlevelClient = new ElasticLowLevelClient(values); | ||
| _elasticsearchNetSerializer = lowlevelClient.Serializer; | ||
| } | ||
|
|
||
| [U] | ||
| public void LowLevelExceptionSerializationMatchesJsonNet() | ||
| { | ||
| var serialized = _elasticsearchNetSerializer.SerializeToString(Exception); | ||
|
|
||
| object CreateException(Type exceptionType, string message, int depth) | ||
| { | ||
| return new | ||
| { | ||
| Depth = depth, | ||
| ClassName = exceptionType.FullName, | ||
| Message = message, | ||
| Source = (object)null, | ||
| StackTraceString = (object)null, | ||
| RemoteStackTraceString = (object)null, | ||
| RemoteStackIndex = 0, | ||
| HResult = -2146233088, | ||
| HelpURL = (object)null | ||
| }; | ||
| } | ||
|
|
||
| var simpleJsonException = new[] | ||
| { | ||
| CreateException(typeof(Exception), "outer_exception", 0), | ||
| CreateException(typeof(InnerException), "inner_exception", 1), | ||
| CreateException(typeof(InnerInnerException), "inner_inner_exception", 2), | ||
| }; | ||
|
|
||
| var jArray = JArray.Parse(serialized); | ||
| var jArray2 = JArray.Parse(JsonConvert.SerializeObject(simpleJsonException)); | ||
|
|
||
| JToken.DeepEquals(jArray, jArray2).Should().BeTrue(); | ||
| } | ||
|
|
||
| public class InnerException : Exception | ||
| { | ||
| public InnerException(string message, Exception innerException) : base(message, innerException) { } | ||
| } | ||
|
|
||
| public class InnerInnerException : Exception | ||
| { | ||
| public InnerInnerException(string message) : base(message) { } | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.