LukeTowers / oc-easyfacebookfeed-plugin

OctoberCMS plugin to display a feed of posts from a Facebook page
Other
2 stars 3 forks source link

Error while syncing Facebook feed #6

Open tom-schmitz opened 4 years ago

tom-schmitz commented 4 years ago

Subject: Possible sync bug

October build: 458 Easy Facebook Feed: 1.10.0 PHP: 7.3

Error while syncing feed both manually (backend button) and via command line (or October schedule).

The Response content must be a string or object implementing __toString(), "boolean" given.

Thanks for your time.

Stack trance:

[2020-08-14 14:15:01] dev.ERROR: UnexpectedValueException: The Response content must be a string or object implementing __toString(), "boolean" given. in C:\xampp\htdocs\project\vendor\symfony\http-foundation\Response.php:409
Stack trace:
#0 C:\xampp\htdocs\project\vendor\laravel\framework\src\Illuminate\Http\Response.php(45): Symfony\Component\HttpFoundation\Response->setContent(false)
#1 C:\xampp\htdocs\project\modules\backend\classes\Controller.php(518): Illuminate\Http\Response->setContent(false)
#2 C:\xampp\htdocs\project\modules\backend\classes\Controller.php(267): Backend\Classes\Controller->execAjaxHandlers()
#3 C:\xampp\htdocs\project\modules\backend\classes\BackendController.php(165): Backend\Classes\Controller->run('update', Array)
#4 [internal function]: Backend\Classes\BackendController->run('luketowers/easy...')
#5 C:\xampp\htdocs\project\vendor\laravel\framework\src\Illuminate\Routing\Controller.php(54): call_user_func_array(Array, Array)
#6 C:\xampp\htdocs\project\vendor\laravel\framework\src\Illuminate\Routing\ControllerDispatcher.php(45): Illuminate\Routing\Controller->callAction('run', Array)
#7 C:\xampp\htdocs\project\vendor\laravel\framework\src\Illuminate\Routing\Route.php(212): Illuminate\Routing\ControllerDispatcher->dispatch(Object(Illuminate\Routing\Route), Object(Backend\Classes\BackendController), 'run')
#8 C:\xampp\htdocs\project\vendor\laravel\framework\src\Illuminate\Routing\Route.php(169): Illuminate\Routing\Route->runController()
#9 C:\xampp\htdocs\project\vendor\laravel\framework\src\Illuminate\Routing\Router.php(658): Illuminate\Routing\Route->run()
#10 C:\xampp\htdocs\project\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(30): Illuminate\Routing\Router->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#11 C:\xampp\htdocs\project\modules\backend\classes\BackendController.php(68): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#12 C:\xampp\htdocs\project\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(131): Backend\Classes\BackendController->Backend\Classes\{closure}(Object(Illuminate\Http\Request), Object(Closure))
#13 C:\xampp\htdocs\project\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#14 C:\xampp\htdocs\project\vendor\laravel\framework\src\Illuminate\Routing\Middleware\SubstituteBindings.php(41): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#15 C:\xampp\htdocs\project\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(149): Illuminate\Routing\Middleware\SubstituteBindings->handle(Object(Illuminate\Http\Request), Object(Closure))
#16 C:\xampp\htdocs\project\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#17 C:\xampp\htdocs\project\vendor\laravel\framework\src\Illuminate\View\Middleware\ShareErrorsFromSession.php(49): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#18 C:\xampp\htdocs\project\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(149): Illuminate\View\Middleware\ShareErrorsFromSession->handle(Object(Illuminate\Http\Request), Object(Closure))
#19 C:\xampp\htdocs\project\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#20 C:\xampp\htdocs\project\vendor\laravel\framework\src\Illuminate\Session\Middleware\StartSession.php(63): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#21 C:\xampp\htdocs\project\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(149): Illuminate\Session\Middleware\StartSession->handle(Object(Illuminate\Http\Request), Object(Closure))
#22 C:\xampp\htdocs\project\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#23 C:\xampp\htdocs\project\vendor\laravel\framework\src\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse.php(37): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#24 C:\xampp\htdocs\project\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(149): Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse->handle(Object(Illuminate\Http\Request), Object(Closure))
#25 C:\xampp\htdocs\project\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#26 C:\xampp\htdocs\project\vendor\laravel\framework\src\Illuminate\Cookie\Middleware\EncryptCookies.php(66): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#27 C:\xampp\htdocs\project\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(149): Illuminate\Cookie\Middleware\EncryptCookies->handle(Object(Illuminate\Http\Request), Object(Closure))
#28 C:\xampp\htdocs\project\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#29 C:\xampp\htdocs\project\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(102): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#30 C:\xampp\htdocs\project\vendor\laravel\framework\src\Illuminate\Routing\Router.php(660): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#31 C:\xampp\htdocs\project\vendor\laravel\framework\src\Illuminate\Routing\Router.php(635): Illuminate\Routing\Router->runRouteWithinStack(Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request))
#32 C:\xampp\htdocs\project\vendor\laravel\framework\src\Illuminate\Routing\Router.php(601): Illuminate\Routing\Router->runRoute(Object(Illuminate\Http\Request), Object(Illuminate\Routing\Route))
#33 C:\xampp\htdocs\project\vendor\october\rain\src\Router\CoreRouter.php(20): Illuminate\Routing\Router->dispatchToRoute(Object(Illuminate\Http\Request))
#34 C:\xampp\htdocs\project\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(176): October\Rain\Router\CoreRouter->dispatch(Object(Illuminate\Http\Request))
#35 C:\xampp\htdocs\project\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(30): Illuminate\Foundation\Http\Kernel->Illuminate\Foundation\Http\{closure}(Object(Illuminate\Http\Request))
#36 C:\xampp\htdocs\project\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode.php(46): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#37 C:\xampp\htdocs\project\vendor\october\rain\src\Foundation\Http\Middleware\CheckForMaintenanceMode.php(24): Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode->handle(Object(Illuminate\Http\Request), Object(Closure))
#38 C:\xampp\htdocs\project\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(149): October\Rain\Foundation\Http\Middleware\CheckForMaintenanceMode->handle(Object(Illuminate\Http\Request), Object(Closure))
#39 C:\xampp\htdocs\project\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#40 C:\xampp\htdocs\project\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(102): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#41 C:\xampp\htdocs\project\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(151): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#42 C:\xampp\htdocs\project\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(116): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request))
#43 C:\xampp\htdocs\project\index.php(43): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request))
#44 {main} 
tom-schmitz commented 4 years ago

Hi @LukeTowers just a mention in case GitHub didn't send a notification

LukeTowers commented 4 years ago

@tom-schmitz please update to the newest version of October available and confirm that the bug still exists

tom-schmitz commented 4 years ago

@LukeTowers, I've just update to the latest version of October (468). However the error is still present. Error occurs when pressing the sync button or using the command.

[Screenshot] image

But I noticed one thing when I first create a new feed the sync works all post all listed. So then I deleted all of them via the database, and press the sync button once again in the backend I still get the same error and no new items show up in the list. But when I then refresh the page all of the posts are listed.

Do you have any other clue to what could cause this error to occur?

LukeTowers commented 4 years ago

@tom-schmitz hmm, how very odd indeed. I can't think of why false would be making it to that part of the code, are you able to send me a copy of your project with the database included so that I can test it locally? I can't reproduce this issue on any of my other environments.

tom-schmitz commented 4 years ago

@LukeTowers unfortunately I'm not able to share the project files or database. When I have some more time I will do some more debugging myself, if I find anything I will let you know. If in the meanwhile you find anything that could solve this, please do let me know.

LukeTowers commented 4 years ago

@tom-schmitz are you using any other plugins? Does this behaviour persist if you remove all other plugins?

tom-schmitz commented 4 years ago

@LukeTowers I managed to look into the issue some more, and I think I found the issue. After some debugging I noticed that the issue starts at plugins/luketowers/easyfacebookfeed/controllers/Feeds.php 43 ending at modules/backend/classes/Controller.php 526 where setContent() gets called from here it calls a morphToJson() this is where the error starts (vendor/laravel/framework/src/Illuminate/Http/Response.php 79) the json_encode() returns a false. From here I called json_last_error () which returned:

JSON_ERROR_UTF8 | Malformed UTF-8 characters, possibly incorrectly encoded

So as a temporarily solution I add / changed the following in the Response.php (Solution from https://stackoverflow.com/questions/10199017/how-to-solve-json-error-utf8-error-in-php-json-decode#answer-26760943)

/**
     * Morph the given content into JSON.
     *
     * @param  mixed   $content
     * @return string
     */
    protected function morphToJson($content)
    {
        if ($content instanceof Jsonable) {
            return $content->toJson();
        } elseif ($content instanceof Arrayable) {
            return json_encode($content->toArray());
        }

-       return json_encode($content);
+      return $this->safe_json_encode($content);
    }

+    protected function safe_json_encode($value, $options = 0, $depth = 512, $utfErrorFlag = false) {
+        $encoded = json_encode($value, $options, $depth);
+        switch (json_last_error()) {
+            case JSON_ERROR_NONE:
+                return $encoded;
+            case JSON_ERROR_DEPTH:
+                return 'Maximum stack depth exceeded'; // or trigger_error() or throw new Exception()
+            case JSON_ERROR_STATE_MISMATCH:
+                return 'Underflow or the modes mismatch'; // or trigger_error() or throw new Exception()
+            case JSON_ERROR_CTRL_CHAR:
+                return 'Unexpected control character found';
+            case JSON_ERROR_SYNTAX:
+                return 'Syntax error, malformed JSON'; // or trigger_error() or throw new Exception()
+            case JSON_ERROR_UTF8:
+                $clean = $this->utf8ize($value);
+                if ($utfErrorFlag) {
+                    return 'UTF8 encoding error'; // or trigger_error() or throw new Exception()
+                }
+                return $this->safe_json_encode($clean, $options, $depth, true);
+            default:
+                return 'Unknown error'; // or trigger_error() or throw new Exception()
+
+        }
+    }
+
+    protected function utf8ize($mixed) {
+        if (is_array($mixed)) {
+            foreach ($mixed as $key => $value) {
+                $mixed[$key] = $this->utf8ize($value);
+            }
+        } else if (is_string ($mixed)) {
+            return utf8_encode($mixed);
+        }
+        return $mixed;
+    }

Perhaps you could come up with a way to catch the UTF-8 issue at the plugin level? Cause with the current temporarily solution, the issue returns when running a October update.

LukeTowers commented 4 years ago

@tom-schmitz could you post the JSON / state that's causing the morphToJSON to fail?