Handle exception about unserializable route cache (#11625)

* Handle exception about unserializable route cache

* description

* Fix whitespace
This commit is contained in:
Tony Murray
2020-05-16 22:22:00 -05:00
committed by GitHub
parent f7d77777fc
commit a82cded552
2 changed files with 82 additions and 0 deletions

View File

@@ -0,0 +1,81 @@
<?php
/**
* UnserializableRouteCache.php
*
* This error is caused when the route cache is generated by an newer version of PHP than the one that loads it
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @package LibreNMS
* @link http://librenms.org
* @copyright 2020 Tony Murray
* @author Tony Murray <murraytony@gmail.com>
*/
namespace LibreNMS\Exceptions;
use LibreNMS\Interfaces\Exceptions\UpgradeableException;
use Throwable;
class UnserializableRouteCache extends \Exception implements UpgradeableException
{
protected $cli_php_version;
protected $web_php_version;
public function __construct($message = "", $code = 0, Throwable $previous = null, $cli_php_version = null, $web_php_version = null)
{
$this->cli_php_version = $cli_php_version;
$this->web_php_version = $web_php_version;
parent::__construct($message, $code, $previous);
}
/**
* Try to convert the given Exception to this exception
*
* @param \Exception $exception
* @return static
*/
public static function upgrade($exception)
{
$errorMessage = "Erroneous data format for unserializing 'Symfony\Component\Routing\CompiledRoute'";
if ($exception instanceof \ErrorException && $exception->message == $errorMessage) {
$cli = rtrim(shell_exec('php -r "echo PHP_VERSION;"'));
if (version_compare($cli, PHP_VERSION, '!=')) {
return new static($exception->getMessage(), $exception->getCode(), $exception, $cli, PHP_VERSION);
}
}
return null;
}
/**
* Render the exception into an HTTP or JSON response.
*
* @param \Illuminate\Http\Request
* @return \Illuminate\Http\Response|\Symfony\Component\HttpFoundation\Response
*/
public function render(\Illuminate\Http\Request $request)
{
$title = __('Error caused by PHP version mismatch');
$message = __('The version of PHP your webserver is running (:web_version) does not match the CLI version (:cli_version).', ['cli_version' => $this->cli_php_version, 'web_version' => $this->web_php_version]);
return $request->wantsJson() ? response()->json([
'status' => 'error',
'message' => "$title: $message",
]) : response()->view('errors.generic', [
'title' => $title,
'content' => $message,
]);
}
}

View File

@@ -31,6 +31,7 @@ class Handler extends ExceptionHandler
\LibreNMS\Exceptions\FilePermissionsException::class,
\LibreNMS\Exceptions\DatabaseConnectException::class,
\LibreNMS\Exceptions\DuskUnsafeException::class,
\LibreNMS\Exceptions\UnserializableRouteCache::class,
];
public function render($request, Exception $exception)