|
| 1 | +using System; |
| 2 | +using System.Text; |
| 3 | +using Elastic.Xunit.XunitPlumbing; |
| 4 | +using Elasticsearch.Net; |
| 5 | +using FluentAssertions; |
| 6 | +using Nest; |
| 7 | +using Tests.Core.Extensions; |
| 8 | + |
| 9 | +namespace Tests.Reproduce |
| 10 | +{ |
| 11 | + public class GithubPR3815 |
| 12 | + { |
| 13 | + [U] |
| 14 | + public void CanDeserializeNestedError() |
| 15 | + { |
| 16 | + var nestedCausedByError = @"{ |
| 17 | + ""status"": 400, |
| 18 | + ""error"": { |
| 19 | + ""type"": ""illegal_argument_exception"", |
| 20 | + ""reason"": ""failed to execute script"", |
| 21 | + ""caused_by"": { |
| 22 | + ""type"": ""script_exception"", |
| 23 | + ""reason"": ""failed to run inline script [use(java.lang.Exception) {throw new Exception(\""Customized Exception\"")}] using lang [groovy]"", |
| 24 | + ""caused_by"": { |
| 25 | + ""type"": ""privileged_action_exception"", |
| 26 | + ""reason"": null, |
| 27 | + ""caused_by"": { |
| 28 | + ""type"": ""exception"", |
| 29 | + ""reason"": ""Custom Exception"" |
| 30 | + } |
| 31 | + } |
| 32 | + } |
| 33 | + } |
| 34 | + }"; |
| 35 | + |
| 36 | + var bytes = Encoding.UTF8.GetBytes(nestedCausedByError); |
| 37 | + var connection = new InMemoryConnection(bytes, 400); |
| 38 | + var settings = new ConnectionSettings(new SingleNodeConnectionPool(new Uri("http://localhost:9200")), connection); |
| 39 | + var client = new ElasticClient(settings); |
| 40 | + |
| 41 | + CheckCustomDeserialiseMethods(client, () => client.MultiGet()); |
| 42 | + CheckCustomDeserialiseMethods(client, () => client.Cat.Help()); |
| 43 | + CheckCustomDeserialiseMethods(client, () => client.Nodes.HotThreads()); |
| 44 | + CheckCustomDeserialiseMethods(client, () => client.MultiSearch()); |
| 45 | + CheckCustomDeserialiseMethods(client, () => client.Sql.Translate()); |
| 46 | + CheckCustomDeserialiseMethods(client, () => client.Security.GetCertificates()); |
| 47 | + } |
| 48 | + |
| 49 | + private static void CheckCustomDeserialiseMethods(ElasticClient client, Func<ResponseBase> perform) |
| 50 | + { |
| 51 | + var response = perform(); |
| 52 | + response.ShouldNotBeValid(); |
| 53 | + var se = response.ServerError; |
| 54 | + se.Status.Should().Be(400); |
| 55 | + se.Should().NotBeNull(); |
| 56 | + ShouldDeserialize(se.Error); |
| 57 | + ShouldDeserialize(se.Error.CausedBy); |
| 58 | + ShouldDeserialize(se.Error.CausedBy.CausedBy, true); |
| 59 | + ShouldDeserialize(se.Error.CausedBy.CausedBy.CausedBy); |
| 60 | + } |
| 61 | + |
| 62 | + private static void ShouldDeserialize(ErrorCause error, bool nullReason = false) |
| 63 | + { |
| 64 | + error.Should().NotBeNull(); |
| 65 | + error.Type.Should().NotBeNullOrEmpty(); |
| 66 | + if (nullReason) |
| 67 | + error.Reason.Should().BeNull(); |
| 68 | + else |
| 69 | + error.Reason.Should().NotBeNullOrEmpty(); |
| 70 | + } |
| 71 | + } |
| 72 | +} |
0 commit comments