Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

error handling when extending flarum from extensions fails #2740

Merged
merged 5 commits into from
Apr 29, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/Extension/Exception/ExtensionBootError.php
Original file line number Diff line number Diff line change
@@ -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);
}
}
10 changes: 8 additions & 2 deletions src/Extension/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use Flarum\Database\Migrator;
use Flarum\Extend\LifecycleInterface;
use Flarum\Extension\Exception\ExtensionBootError;
use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Arr;
Expand All @@ -21,6 +22,7 @@
use League\Flysystem\FilesystemInterface;
use League\Flysystem\MountManager;
use League\Flysystem\Plugin\ListFiles;
use Throwable;

/**
* @property string $name
Expand Down Expand Up @@ -53,7 +55,7 @@ class Extension implements Arrayable

protected static function nameToId($name)
{
list($vendor, $package) = explode('/', $name);
[$vendor, $package] = explode('/', $name);
luceos marked this conversation as resolved.
Show resolved Hide resolved
$package = str_replace(['flarum-ext-', 'flarum-'], '', $package);

return "$vendor-$package";
Expand Down Expand Up @@ -134,7 +136,11 @@ protected function assignId()
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);
}
}
}

Expand Down
24 changes: 9 additions & 15 deletions src/Foundation/ErrorServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -59,21 +53,21 @@ public function register()

$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);
}
}