11using System ;
22using System . Collections . Generic ;
3+ using System . IO ;
34using System . Linq ;
45using System . Net ;
56using InfluxDB . Client . Core . Internal ;
7+ using Newtonsoft . Json ;
68using Newtonsoft . Json . Linq ;
79using RestSharp ;
810
@@ -53,40 +55,64 @@ public HttpException(string message, int status) : base(message, 0)
5355 /// </summary>
5456 public int ? RetryAfter { get ; set ; }
5557
56- public static HttpException Create ( IRestResponse requestResult )
58+ public static HttpException Create ( IRestResponse requestResult , object body )
5759 {
5860 Arguments . CheckNotNull ( requestResult , nameof ( requestResult ) ) ;
5961
6062 var httpHeaders = LoggingHandler . ToHeaders ( requestResult . Headers ) ;
6163
62- return Create ( requestResult . Content , httpHeaders , requestResult . ErrorMessage , requestResult . StatusCode ) ;
64+ return Create ( body , httpHeaders , requestResult . ErrorMessage , requestResult . StatusCode ) ;
6365 }
6466
65- public static HttpException Create ( IHttpResponse requestResult )
67+ public static HttpException Create ( IHttpResponse requestResult , object body )
6668 {
6769 Arguments . CheckNotNull ( requestResult , nameof ( requestResult ) ) ;
6870
69- return Create ( requestResult . Content , requestResult . Headers , requestResult . ErrorMessage , requestResult . StatusCode ) ;
71+ return Create ( body , requestResult . Headers , requestResult . ErrorMessage , requestResult . StatusCode ) ;
7072 }
7173
72- public static HttpException Create ( string content , IList < HttpHeader > headers , string ErrorMessage , HttpStatusCode statusCode )
74+ public static HttpException Create ( object content , IList < HttpHeader > headers , string ErrorMessage , HttpStatusCode statusCode )
7375 {
76+ string stringBody = null ;
77+ var errorBody = new JObject ( ) ;
7478 string errorMessage = null ;
75- JObject errorBody ;
7679
7780 int ? retryAfter = null ;
7881 {
7982 var retryHeader = headers . FirstOrDefault ( header => header . Name . Equals ( "Retry-After" ) ) ;
8083 if ( retryHeader != null ) retryAfter = Convert . ToInt32 ( retryHeader . Value ) ;
8184 }
8285
83- if ( string . IsNullOrEmpty ( content ) )
84- errorBody = new JObject ( ) ;
85- else
86- errorBody = JObject . Parse ( content ) ;
87-
88- if ( errorBody . ContainsKey ( "message" ) ) errorMessage = errorBody . GetValue ( "message" ) . ToString ( ) ;
86+ if ( content != null )
87+ {
88+ if ( content is Stream )
89+ {
90+ var stream = content as Stream ;
91+ var sr = new StreamReader ( stream ) ;
92+ stringBody = sr . ReadToEnd ( ) ;
93+ }
94+ else
95+ {
96+ stringBody = content . ToString ( ) ;
97+ }
98+ }
8999
100+ if ( ! string . IsNullOrEmpty ( stringBody ) )
101+ {
102+ try
103+ {
104+ errorBody = JObject . Parse ( stringBody ) ;
105+ if ( errorBody . ContainsKey ( "message" ) )
106+ {
107+ errorMessage = errorBody . GetValue ( "message" ) . ToString ( ) ;
108+ }
109+ }
110+ catch ( JsonException )
111+ {
112+ errorBody = new JObject ( ) ;
113+ }
114+ }
115+
90116 var keys = new [ ] { "X-Platform-Error-Code" , "X-Influx-Error" , "X-InfluxDb-Error" } ;
91117
92118 if ( string . IsNullOrEmpty ( errorMessage ) )
@@ -95,6 +121,7 @@ public static HttpException Create(string content, IList<HttpHeader> headers, st
95121 . Select ( header => header . Value . ToString ( ) ) . FirstOrDefault ( ) ;
96122
97123 if ( string . IsNullOrEmpty ( errorMessage ) ) errorMessage = ErrorMessage ;
124+ if ( string . IsNullOrEmpty ( errorMessage ) ) errorMessage = stringBody ;
98125
99126 return new HttpException ( errorMessage , ( int ) statusCode )
100127 { ErrorBody = errorBody , RetryAfter = retryAfter } ;
0 commit comments