@@ -61,6 +61,8 @@ public class RestApiClient : IRestApiClient, IDisposable
6161 private bool _disposed ;
6262
6363 public HttpClient HttpClient { get ; set ; }
64+
65+ private readonly bool _dataExpectedOnErrorResponse ;
6466
6567 public RestApiClient ( string baseUri )
6668 {
@@ -77,6 +79,12 @@ public RestApiClient(IHttpClientFactory httpClientFactory, string httpClientName
7779 {
7880 HttpClient = httpClientFactory . CreateClient ( httpClientName ) ;
7981 }
82+
83+ public RestApiClient ( HttpClient httpClient , bool dataExpectedOnErrorResponse )
84+ {
85+ HttpClient = httpClient ;
86+ _dataExpectedOnErrorResponse = dataExpectedOnErrorResponse ;
87+ }
8088
8189 public Uri GetBaseUri ( )
8290 => HttpClient . BaseAddress ?? new Uri ( MoneyMoovUrlBuilder . DEFAULT_MONEYMOOV_BASE_URL ) ;
@@ -200,8 +208,18 @@ private async Task<RestApiResponse<T>> ToApiResponse<T>(HttpResponseMessage resp
200208 }
201209 else if ( response . Content . Headers . ContentLength > 0 )
202210 {
203- string contentStr = await response . Content . ReadAsStringAsync ( ) ;
211+ var contentStr = await response . Content . ReadAsStringAsync ( ) ;
204212 var problem = DeserialiseProblem ( response . StatusCode , contentStr ) ;
213+
214+ if ( _dataExpectedOnErrorResponse )
215+ {
216+ var result = TryDeserialiseDataOnErrorResponse < T > ( contentStr ) ;
217+
218+ if ( result != null )
219+ {
220+ return new RestApiResponse < T > ( response . StatusCode , requestUri , response . Headers , problem , result ) ;
221+ }
222+ }
205223 return new RestApiResponse < T > ( response . StatusCode , requestUri , response . Headers , problem ) ;
206224 }
207225 else
@@ -210,6 +228,18 @@ private async Task<RestApiResponse<T>> ToApiResponse<T>(HttpResponseMessage resp
210228 new NoFrixionProblem ( response . StatusCode , "Response content was expected." ) ) ;
211229 }
212230 }
231+
232+ private T ? TryDeserialiseDataOnErrorResponse < T > ( string contentStr )
233+ {
234+ try
235+ {
236+ return contentStr . FromJson < T > ( ) ;
237+ }
238+ catch ( Newtonsoft . Json . JsonException )
239+ {
240+ return default ;
241+ }
242+ }
213243
214244 /// <summary>
215245 /// Attempts to deserialise a problem object from the contents of a failure response.
0 commit comments