From a82cded55277c7f74da6a8238c614bbe8ed25456 Mon Sep 17 00:00:00 2001 From: Tony Murray Date: Sat, 16 May 2020 22:22:00 -0500 Subject: [PATCH] Handle exception about unserializable route cache (#11625) * Handle exception about unserializable route cache * description * Fix whitespace --- .../Exceptions/UnserializableRouteCache.php | 81 +++++++++++++++++++ app/Exceptions/Handler.php | 1 + 2 files changed, 82 insertions(+) create mode 100644 LibreNMS/Exceptions/UnserializableRouteCache.php diff --git a/LibreNMS/Exceptions/UnserializableRouteCache.php b/LibreNMS/Exceptions/UnserializableRouteCache.php new file mode 100644 index 0000000000..41d14bd3f5 --- /dev/null +++ b/LibreNMS/Exceptions/UnserializableRouteCache.php @@ -0,0 +1,81 @@ +. + * + * @package LibreNMS + * @link http://librenms.org + * @copyright 2020 Tony Murray + * @author Tony Murray + */ + +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, + ]); + } +} diff --git a/app/Exceptions/Handler.php b/app/Exceptions/Handler.php index 37fe033f5b..94e80c3b78 100644 --- a/app/Exceptions/Handler.php +++ b/app/Exceptions/Handler.php @@ -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)