error handling when extending flarum from extensions fails (#2740)

This commit is contained in:
Daniël Klabbers 2021-04-29 22:17:41 +02:00 committed by GitHub
parent d77cc5440b
commit b64698d79f
3 changed files with 47 additions and 17 deletions

View File

@ -0,0 +1,30 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Extension\Exception;
use Exception;
use Flarum\Extension\Extension;
use Throwable;
class ExtensionBootError extends Exception
{
public $extension;
public $extender;
public function __construct(Extension $extension, $extender, Throwable $previous = null)
{
$this->extension = $extension;
$this->extender = $extender;
$extenderClass = get_class($extender);
parent::__construct("Experienced an error while booting extension: {$extension->getTitle()}.\n\nError occurred while applying an extender of type: $extenderClass.", null, $previous);
}
}

View File

@ -11,6 +11,7 @@ namespace Flarum\Extension;
use Flarum\Database\Migrator; use Flarum\Database\Migrator;
use Flarum\Extend\LifecycleInterface; use Flarum\Extend\LifecycleInterface;
use Flarum\Extension\Exception\ExtensionBootError;
use Illuminate\Contracts\Container\Container; use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Filesystem\Filesystem as FilesystemInterface; use Illuminate\Contracts\Filesystem\Filesystem as FilesystemInterface;
use Illuminate\Contracts\Support\Arrayable; use Illuminate\Contracts\Support\Arrayable;
@ -18,6 +19,7 @@ use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Arr; use Illuminate\Support\Arr;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use Throwable;
/** /**
* @property string $name * @property string $name
@ -50,7 +52,7 @@ class Extension implements Arrayable
protected static function nameToId($name) protected static function nameToId($name)
{ {
list($vendor, $package) = explode('/', $name); [$vendor, $package] = explode('/', $name);
$package = str_replace(['flarum-ext-', 'flarum-'], '', $package); $package = str_replace(['flarum-ext-', 'flarum-'], '', $package);
return "$vendor-$package"; return "$vendor-$package";
@ -131,7 +133,11 @@ class Extension implements Arrayable
public function extend(Container $container) public function extend(Container $container)
{ {
foreach ($this->getExtenders() as $extender) { foreach ($this->getExtenders() as $extender) {
try {
$extender->extend($container, $this); $extender->extend($container, $this);
} catch (Throwable $e) {
throw new ExtensionBootError($this, $extender, $e);
}
} }
} }

View File

@ -9,14 +9,8 @@
namespace Flarum\Foundation; namespace Flarum\Foundation;
use Flarum\Extension\Exception\DependentExtensionsException; use Flarum\Extension\Exception as ExtensionException;
use Flarum\Extension\Exception\DependentExtensionsExceptionHandler; use Flarum\Foundation\ErrorHandling as Handling;
use Flarum\Extension\Exception\MissingDependenciesException;
use Flarum\Extension\Exception\MissingDependenciesExceptionHandler;
use Flarum\Foundation\ErrorHandling\ExceptionHandler;
use Flarum\Foundation\ErrorHandling\LogReporter;
use Flarum\Foundation\ErrorHandling\Registry;
use Flarum\Foundation\ErrorHandling\Reporter;
use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Validation\ValidationException as IlluminateValidationException; use Illuminate\Validation\ValidationException as IlluminateValidationException;
use Tobscure\JsonApi\Exception\InvalidParameterException; use Tobscure\JsonApi\Exception\InvalidParameterException;
@ -59,21 +53,21 @@ class ErrorServiceProvider extends AbstractServiceProvider
$this->container->singleton('flarum.error.handlers', function () { $this->container->singleton('flarum.error.handlers', function () {
return [ return [
IlluminateValidationException::class => ExceptionHandler\IlluminateValidationExceptionHandler::class, IlluminateValidationException::class => Handling\ExceptionHandler\IlluminateValidationExceptionHandler::class,
ValidationException::class => ExceptionHandler\ValidationExceptionHandler::class, ValidationException::class => Handling\ExceptionHandler\ValidationExceptionHandler::class,
DependentExtensionsException::class => DependentExtensionsExceptionHandler::class, ExtensionException\DependentExtensionsException::class => ExtensionException\DependentExtensionsExceptionHandler::class,
MissingDependenciesException::class => MissingDependenciesExceptionHandler::class, ExtensionException\MissingDependenciesException::class => ExtensionException\MissingDependenciesExceptionHandler::class,
]; ];
}); });
$this->container->singleton(Registry::class, function () { $this->container->singleton(Handling\Registry::class, function () {
return new Registry( return new Handling\Registry(
$this->container->make('flarum.error.statuses'), $this->container->make('flarum.error.statuses'),
$this->container->make('flarum.error.classes'), $this->container->make('flarum.error.classes'),
$this->container->make('flarum.error.handlers') $this->container->make('flarum.error.handlers')
); );
}); });
$this->container->tag(LogReporter::class, Reporter::class); $this->container->tag(Handling\LogReporter::class, Handling\Reporter::class);
} }
} }