@@ -16,7 +16,7 @@ class RequestException extends HttpClientException
1616 /**
1717 * The current truncation length for the exception message.
1818 *
19- * @var int|false
19+ * @var int|false|null
2020 */
2121 public $ truncateExceptionsAt ;
2222
@@ -27,6 +27,13 @@ class RequestException extends HttpClientException
2727 */
2828 public static $ truncateAt = 120 ;
2929
30+ /**
31+ * Whether the response has been summarized in the message.
32+ *
33+ * @var bool
34+ */
35+ public $ hasBeenSummarized = false ;
36+
3037 /**
3138 * Create a new exception instance.
3239 *
@@ -35,7 +42,7 @@ class RequestException extends HttpClientException
3542 */
3643 public function __construct (Response $ response , $ truncateExceptionsAt = null )
3744 {
38- parent ::__construct (" HTTP request returned status code { $ response -> status ()}" , $ response ->status ());
45+ parent ::__construct ($ this -> prepareMessage ( $ response ) , $ response ->status ());
3946
4047 $ this ->truncateExceptionsAt = $ truncateExceptionsAt ;
4148
@@ -76,18 +83,35 @@ public static function dontTruncate()
7683 /**
7784 * Prepare the exception message.
7885 *
79- * @return void
86+ * @param \Illuminate\Http\Client\Response $response
87+ * @return string
8088 */
81- public function report (): void
89+ protected function prepareMessage ( Response $ response )
8290 {
91+ $ message = "HTTP request returned status code {$ response ->status ()}" ;
92+
8393 $ truncateExceptionsAt = $ this ->truncateExceptionsAt ?? static ::$ truncateAt ;
8494
85- $ summary = $ truncateExceptionsAt
86- ? Message::bodySummary ($ this ->response ->toPsrResponse (), $ truncateExceptionsAt )
87- : Message::toString ($ this ->response ->toPsrResponse ());
95+ $ summary = is_int ($ truncateExceptionsAt )
96+ ? Message::bodySummary ($ response ->toPsrResponse (), $ truncateExceptionsAt )
97+ : Message::toString ($ response ->toPsrResponse ());
98+
99+ return is_null ($ summary ) ? $ message : $ message . ": \n{$ summary }\n" ;
100+ }
88101
89- if (! is_null ($ summary )) {
90- $ this ->message .= ": \n{$ summary }\n" ;
102+ /**
103+ * Prepare the exception message.
104+ *
105+ * @return void
106+ */
107+ public function report (): void
108+ {
109+ if ($ this ->hasBeenSummarized ) {
110+ return ;
91111 }
112+
113+ $ this ->message = $ this ->prepareMessage ($ this ->response );
114+
115+ $ this ->hasBeenSummarized = true ;
92116 }
93117}
0 commit comments