@@ -66,7 +66,7 @@ private ElasticsearchResponse<TReturn> Initialize(int? statusCode, Exception exc
6666 private void SetBody ( ElasticsearchResponse < TReturn > response , Stream stream )
6767 {
6868 byte [ ] bytes = null ;
69- if ( NeedsToEagerReadStream ( ) )
69+ if ( NeedsToEagerReadStream ( response ) )
7070 {
7171 var inMemoryStream = this . _requestData . MemoryStreamFactory . Create ( ) ;
7272 stream . CopyTo ( inMemoryStream , BufferSize ) ;
@@ -76,29 +76,25 @@ private void SetBody(ElasticsearchResponse<TReturn> response, Stream stream)
7676 var needsDispose = typeof ( TReturn ) != typeof ( Stream ) ;
7777 using ( needsDispose ? stream : EmptyDisposable )
7878 {
79- if ( response . Success )
79+ if ( response . Success || response . AllowAllStatusCodes )
8080 {
8181 if ( ! SetSpecialTypes ( stream , response , bytes ) )
8282 {
8383 if ( this . _requestData . CustomConverter != null ) response . Body = this . _requestData . CustomConverter ( response , stream ) as TReturn ;
8484 else response . Body = this . _requestData . ConnectionSettings . Serializer . Deserialize < TReturn > ( stream ) ;
8585 }
86+ if ( response . AllowAllStatusCodes )
87+ ReadServerError ( response , new MemoryStream ( bytes ) , bytes ) ;
8688 }
8789 else if ( response . HttpStatusCode != null )
88- {
89- ServerError serverError ;
90- if ( ServerError . TryCreate ( stream , out serverError ) )
91- response . ServerError = serverError ;
92- if ( _disableDirectStreaming )
93- response . ResponseBodyInBytes = bytes ;
94- }
90+ ReadServerError ( response , stream , bytes ) ;
9591 }
9692 }
9793
9894 private async Task SetBodyAsync ( ElasticsearchResponse < TReturn > response , Stream stream )
9995 {
10096 byte [ ] bytes = null ;
101- if ( NeedsToEagerReadStream ( ) )
97+ if ( NeedsToEagerReadStream ( response ) )
10298 {
10399 var inMemoryStream = this . _requestData . MemoryStreamFactory . Create ( ) ;
104100 await stream . CopyToAsync ( inMemoryStream , BufferSize , this . _cancellationToken ) . ConfigureAwait ( false ) ;
@@ -108,23 +104,37 @@ private async Task SetBodyAsync(ElasticsearchResponse<TReturn> response, Stream
108104 var needsDispose = typeof ( TReturn ) != typeof ( Stream ) ;
109105 using ( needsDispose ? stream : EmptyDisposable )
110106 {
111- if ( response . Success )
107+ if ( response . Success || response . AllowAllStatusCodes )
112108 {
113109 if ( ! SetSpecialTypes ( stream , response , bytes ) )
114110 {
115111 if ( this . _requestData . CustomConverter != null ) response . Body = this . _requestData . CustomConverter ( response , stream ) as TReturn ;
116112 else response . Body = await this . _requestData . ConnectionSettings . Serializer . DeserializeAsync < TReturn > ( stream , this . _cancellationToken ) . ConfigureAwait ( false ) ;
117113 }
114+ if ( response . AllowAllStatusCodes )
115+ await ReadServerErrorAsync ( response , new MemoryStream ( bytes ) , bytes ) ;
118116 }
119117 else if ( response . HttpStatusCode != null )
120- {
121- response . ServerError = await ServerError . TryCreateAsync ( stream , this . _cancellationToken ) . ConfigureAwait ( false ) ;
122- if ( _disableDirectStreaming )
123- response . ResponseBodyInBytes = bytes ;
124- }
118+ await ReadServerErrorAsync ( response , stream , bytes ) ;
125119 }
126120 }
127121
122+ private void ReadServerError ( ElasticsearchResponse < TReturn > response , Stream stream , byte [ ] bytes )
123+ {
124+ ServerError serverError ;
125+ if ( ServerError . TryCreate ( stream , out serverError ) )
126+ response . ServerError = serverError ;
127+ if ( _disableDirectStreaming )
128+ response . ResponseBodyInBytes = bytes ;
129+ }
130+
131+ private async Task ReadServerErrorAsync ( ElasticsearchResponse < TReturn > response , Stream stream , byte [ ] bytes )
132+ {
133+ response . ServerError = await ServerError . TryCreateAsync ( stream , this . _cancellationToken ) . ConfigureAwait ( false ) ;
134+ if ( _disableDirectStreaming )
135+ response . ResponseBodyInBytes = bytes ;
136+ }
137+
128138 private void Finalize ( ElasticsearchResponse < TReturn > response )
129139 {
130140 var passAlongConnectionStatus = response . Body as IBodyWithApiCallDetails ;
@@ -134,8 +144,9 @@ private void Finalize(ElasticsearchResponse<TReturn> response)
134144 }
135145 }
136146
137- private bool NeedsToEagerReadStream ( ) =>
138- _disableDirectStreaming || typeof ( TReturn ) == typeof ( string ) || typeof ( TReturn ) == typeof ( byte [ ] ) ;
147+ private bool NeedsToEagerReadStream ( ElasticsearchResponse < TReturn > response ) =>
148+ response . AllowAllStatusCodes //need to double read for error and TReturn
149+ || _disableDirectStreaming || typeof ( TReturn ) == typeof ( string ) || typeof ( TReturn ) == typeof ( byte [ ] ) ;
139150
140151 private byte [ ] SwapStreams ( ref Stream responseStream , ref MemoryStream ms )
141152 {
0 commit comments