44using System . Net . Http ;
55using System . Threading ;
66using System . Threading . Tasks ;
7+ using CSF . Screenplay . Stopwatch ;
78using Newtonsoft . Json ;
89
910namespace CSF . Screenplay . JsonApis . Abilities
@@ -52,18 +53,24 @@ HttpResponseMessage GetResponseRespectingTimeout(HttpRequestMessage request, Tim
5253 }
5354 catch ( TaskCanceledException ex )
5455 {
55- throw new TimeoutException ( $ "The JSON API request timed out after { timeout . ToString ( "g" ) } .", ex ) ;
56+ var timeFormatter = new TimeSpanFormatter ( ) ;
57+ var message = String . Format ( Resources . ExceptionFormats . ApiRequestTimedOut , timeFormatter . Format ( timeout ) ) ;
58+ throw new TimeoutException ( message , ex ) ;
5659 }
5760 catch ( HttpRequestException ex )
5861 {
59- throw new JsonApiException ( "The JSON API request failed." , ex ) ;
62+ throw new JsonApiException ( Resources . ExceptionFormats . UnexpectedApiRequestFailure , ex ) ;
6063 }
6164 catch ( AggregateException ex )
6265 {
6366 if ( ex . InnerExceptions . OfType < TaskCanceledException > ( ) . Any ( ) )
64- throw new TimeoutException ( $ "The JSON API request timed out after { timeout . ToString ( "g" ) } .", ex ) ;
67+ {
68+ var timeFormatter = new TimeSpanFormatter ( ) ;
69+ var message = String . Format ( Resources . ExceptionFormats . ApiRequestTimedOut , timeFormatter . Format ( timeout ) ) ;
70+ throw new TimeoutException ( message , ex ) ;
71+ }
6572 else if ( ex . InnerExceptions . OfType < HttpRequestException > ( ) . Any ( ) )
66- throw new JsonApiException ( "The JSON API request failed." , ex ) ;
73+ throw new JsonApiException ( Resources . ExceptionFormats . UnexpectedApiRequestFailure , ex ) ;
6774
6875 throw ;
6976 }
@@ -74,8 +81,11 @@ void AssertThatResultIsSuccess(HttpResponseMessage result)
7481 if ( result . IsSuccessStatusCode ) return ;
7582
7683 var responseBody = result . Content . ReadAsStringAsync ( ) . Result ;
77- throw new JsonApiException ( $@ "The JSON API request failed: HTTP { result . StatusCode . ToString ( ) }
78- { responseBody } ") ;
84+
85+ var message = String . Format ( Resources . ExceptionFormats . ApiRequestFailedWithStatusCode , ( int ) result . StatusCode ) ;
86+ throw new JsonApiException ( message ) {
87+ ErrorResponseBody = responseBody
88+ } ;
7989 }
8090
8191 protected virtual T ConvertResponse < T > ( HttpResponseMessage response )
@@ -85,7 +95,7 @@ protected virtual T ConvertResponse<T>(HttpResponseMessage response)
8595 var copyTask = response . Content . CopyToAsync ( buffer ) ;
8696 var waitSuccess = copyTask . Wait ( TimeSpan . FromSeconds ( 1 ) ) ;
8797 if ( ! waitSuccess )
88- throw new JsonApiException ( "Timeout whilst converting JSON response to string. This should probably never happen?!" ) ;
98+ throw new JsonApiException ( Resources . ExceptionFormats . TimeoutConvertingResponseBody ) ;
8999
90100 buffer . Position = 0 ;
91101
0 commit comments