@@ -12,27 +12,27 @@ public class ServerError
1212 {
1313 public Error Error { get ; set ; }
1414 public int Status { get ; set ; }
15-
16- public static ServerError Create ( Stream stream ) => ElasticsearchDefaultSerializer . Instance . Deserialize < ServerError > ( stream ) ;
15+
16+ public static ServerError Create ( Stream stream ) => ElasticsearchDefaultSerializer . Instance . Deserialize < ServerError > ( stream ) ;
1717 public static Task < ServerError > CreateAsync ( Stream stream , CancellationToken token ) =>
1818 ElasticsearchDefaultSerializer . Instance . DeserializeAsync < ServerError > ( stream , token ) ;
19-
20- /// <summary>
21- /// Creating the server error might fail in cases where a proxy returns an http response which is not json at all
22- /// </summary>
23- public static bool TryCreate ( Stream stream , out ServerError serverError )
24- {
19+
20+ /// <summary>
21+ /// Creating the server error might fail in cases where a proxy returns an http response which is not json at all
22+ /// </summary>
23+ public static bool TryCreate ( Stream stream , out ServerError serverError )
24+ {
2525 serverError = null ;
26- try { serverError = Create ( stream ) ; }
27- catch { return false ; }
28- return true ;
26+ try { serverError = Create ( stream ) ; }
27+ catch { return false ; }
28+ return true ;
2929 }
30-
31- /// <summary>
32- /// Creating the server error might fail in cases where a proxy returns an http response which is not json at all
33- /// </summary>
34- public static async Task < ServerError > TryCreateAsync ( Stream stream , CancellationToken token )
35- {
30+
31+ /// <summary>
32+ /// Creating the server error might fail in cases where a proxy returns an http response which is not json at all
33+ /// </summary>
34+ public static async Task < ServerError > TryCreateAsync ( Stream stream , CancellationToken token )
35+ {
3636 try { return await CreateAsync ( stream , token ) . ConfigureAwait ( false ) ; }
3737 catch { // ignored
3838 }
@@ -48,11 +48,18 @@ internal static ServerError Create(IDictionary<string, object> dict, IJsonSerial
4848 statusCode = Convert . ToInt32 ( status ) ;
4949
5050 if ( ! dict . TryGetValue ( "error" , out error ) ) return null ;
51+ Error err ;
52+ var s = error as string ;
53+ if ( s != null )
54+ {
55+ err = new Error { Reason = s } ;
56+ }
57+ else err = ( Error ) strategy . DeserializeObject ( error , typeof ( Error ) ) ;
5158
5259 return new ServerError
5360 {
5461 Status = statusCode ,
55- Error = ( Error ) strategy . DeserializeObject ( error , typeof ( Error ) )
62+ Error = err
5663 } ;
5764 }
5865
@@ -88,8 +95,8 @@ public class Error : IRootCause
8895 internal static Error Create ( IDictionary < string , object > dict , IJsonSerializerStrategy strategy )
8996 {
9097 var error = new Error ( ) ;
91- error . FillValues ( dict ) ;
92-
98+ error . FillValues ( dict ) ;
99+
93100 object causedBy ;
94101 if ( dict . TryGetValue ( "caused_by" , out causedBy ) )
95102 error . CausedBy = ( CausedBy ) strategy . DeserializeObject ( causedBy , typeof ( CausedBy ) ) ;
@@ -116,12 +123,12 @@ public class CausedBy
116123
117124 internal static CausedBy Create ( IDictionary < string , object > dict , IJsonSerializerStrategy strategy )
118125 {
119- var causedBy = new CausedBy ( ) ;
120- object reason ;
121- if ( dict . TryGetValue ( "reason" , out reason ) ) causedBy . Reason = Convert . ToString ( reason ) ;
122- object type ;
123- if ( dict . TryGetValue ( "type" , out type ) ) causedBy . Type = Convert . ToString ( type ) ;
124- object innerCausedBy ;
126+ var causedBy = new CausedBy ( ) ;
127+ object reason ;
128+ if ( dict . TryGetValue ( "reason" , out reason ) ) causedBy . Reason = Convert . ToString ( reason ) ;
129+ object type ;
130+ if ( dict . TryGetValue ( "type" , out type ) ) causedBy . Type = Convert . ToString ( type ) ;
131+ object innerCausedBy ;
125132 if ( dict . TryGetValue ( "caused_by" , out innerCausedBy ) )
126133 causedBy . InnerCausedBy = ( CausedBy ) strategy . DeserializeObject ( innerCausedBy , typeof ( CausedBy ) ) ;
127134
@@ -146,24 +153,25 @@ internal static RootCause Create(IDictionary<string, object> dict, IJsonSerializ
146153 var rootCause = new RootCause ( ) ;
147154 rootCause . FillValues ( dict ) ;
148155 return rootCause ;
149- }
150-
156+ }
157+
151158 public override string ToString ( ) => $ "Type: { this . Type } Reason: \" { this . Reason } \" ";
152159 }
153160
154161 internal static class RootCauseExtensions
155162 {
156163 public static void FillValues ( this IRootCause rootCause , IDictionary < string , object > dict )
157164 {
165+ if ( dict == null ) return ;
158166 object index ;
159- if ( dict . TryGetValue ( "index" , out index ) ) rootCause . Index = Convert . ToString ( index ) ;
160- object reason ;
161- if ( dict . TryGetValue ( "reason" , out reason ) ) rootCause . Reason = Convert . ToString ( reason ) ;
162- object resourceId ;
167+ if ( dict . TryGetValue ( "index" , out index ) ) rootCause . Index = Convert . ToString ( index ) ;
168+ object reason ;
169+ if ( dict . TryGetValue ( "reason" , out reason ) ) rootCause . Reason = Convert . ToString ( reason ) ;
170+ object resourceId ;
163171 if ( dict . TryGetValue ( "resource.id" , out resourceId ) ) rootCause . ResourceId = Convert . ToString ( resourceId ) ;
164- object resourceType ;
165- if ( dict . TryGetValue ( "resource.type" , out resourceType ) ) rootCause . ResourceType = Convert . ToString ( resourceType ) ;
166- object type ;
172+ object resourceType ;
173+ if ( dict . TryGetValue ( "resource.type" , out resourceType ) ) rootCause . ResourceType = Convert . ToString ( resourceType ) ;
174+ object type ;
167175 if ( dict . TryGetValue ( "type" , out type ) ) rootCause . Type = Convert . ToString ( type ) ;
168176 }
169177 }
0 commit comments