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 fcb5778705
commit e0258d2708
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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\Extend\LifecycleInterface;
use Flarum\Extension\Exception\ExtensionBootError;
use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Filesystem\Filesystem as FilesystemInterface;
use Illuminate\Contracts\Support\Arrayable;
@ -18,6 +19,7 @@ use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Throwable;
/**
* @property string $name
@ -50,7 +52,7 @@ class Extension implements Arrayable
protected static function nameToId($name)
{
list($vendor, $package) = explode('/', $name);
[$vendor, $package] = explode('/', $name);
$package = str_replace(['flarum-ext-', 'flarum-'], '', $package);
return "$vendor-$package";
@ -131,7 +133,11 @@ class Extension implements Arrayable
public function extend(Container $container)
{
foreach ($this->getExtenders() as $extender) {
$extender->extend($container, $this);
try {
$extender->extend($container, $this);
} catch (Throwable $e) {
throw new ExtensionBootError($this, $extender, $e);
}
}
}

View File

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