From 55cc3010003b5ae01ac6fb434fd4459ee9259a91 Mon Sep 17 00:00:00 2001 From: Luke Towers Date: Fri, 2 Apr 2021 00:13:39 -0600 Subject: [PATCH 1/2] Improve the exception thrown when JSON encoding response contents fails MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This improves on the opaque exception of "The Response content must be a string or object implementing __toString(), “boolean” given." that occurs whenever a call to $response->setContent($varThatFailsJsonEncoding) is made. It is difficult to debug the original exception, because to the developer they are not providing a boolean anywhere in their code, they are passing an array or an object to setContent() and expecting it to work; not knowing that Laravel internally tries to json_encode() that value before passing it along to Symfony, which only understands string values as response contents. Refs: - https://stackoverflow.com/a/38772790/6652884 - https://octobercms.com/forum/post/error-on-reset-password - https://octobercms.com/plugin/support/pixel-shop/bug-and-italian-translation - https://laravelquestions.com/2017/08/16/the-response-content-must-be-a-string-or-object-implementing-__tostring-boolean-given/ - https://github.com/rainlab/user-plugin/issues/294 --- src/Illuminate/Http/Response.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Illuminate/Http/Response.php b/src/Illuminate/Http/Response.php index f8bc37899cee..883ac9b96620 100755 --- a/src/Illuminate/Http/Response.php +++ b/src/Illuminate/Http/Response.php @@ -53,6 +53,10 @@ public function setContent($content) $this->header('Content-Type', 'application/json'); $content = $this->morphToJson($content); + + if ($content === false) { + throw new \UnexpectedValueException(sprintf("Failed to convert the provided Response content to JSON with the message: %s", json_last_error_msg())); + } } // If this content implements the "Renderable" interface then we will call the From 1168ec7503449633d6b4b5630a75a5d6f272f8d3 Mon Sep 17 00:00:00 2001 From: Luke Towers Date: Fri, 2 Apr 2021 00:16:12 -0600 Subject: [PATCH 2/2] Styling fixes --- src/Illuminate/Http/Response.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Http/Response.php b/src/Illuminate/Http/Response.php index 883ac9b96620..b48ea7ffcbb1 100755 --- a/src/Illuminate/Http/Response.php +++ b/src/Illuminate/Http/Response.php @@ -53,9 +53,9 @@ public function setContent($content) $this->header('Content-Type', 'application/json'); $content = $this->morphToJson($content); - + if ($content === false) { - throw new \UnexpectedValueException(sprintf("Failed to convert the provided Response content to JSON with the message: %s", json_last_error_msg())); + throw new \UnexpectedValueException(sprintf('Failed to convert the provided Response content to JSON with the message: %s', json_last_error_msg())); } }