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

[11.x] Unify exception renderer #52395

Closed
wants to merge 2 commits into from

Conversation

seriquynh
Copy link
Contributor

Nuno's changed the default exception renderer since #51261. Now, there are two ways to customize how Laravel renders an exception.

// Bind/rebind the \Illuminate\Contracts\Foundation\ExceptionRenderer interface.
$this->app->singleton(Illuminate\Contracts\Foundation\ExceptionRenderer::class, function () {
    return new SomeExceptionRendererImpl;
});

// Or bind/rebind the \Illuminate\Foundation\Exceptions\Renderer class.
// It doesn't implement the ExceptionRenderer interface.
$this->app->singleton(Illuminate\Foundation\Exceptions\Renderer\Renderer::class, function () {
    return new SomeRendererSubClass;
});

I think it's complex, inconsistent and introduces more additional checks when actual displaying exceptions because both of them refer to the same thing/category. They defines how an exception should be rendered, right?

Therefore, we can create LaravelRenderer class which implements the ExceptionRenderer interface to unify the concept of rendering exceptions.

Then, inside FoundationServiceProvider, we'll bind the ExceptionRenderer interface to an instance of LaravelRenderer class by default. People can rebind it later.

use Illuminate\Foundation\Exceptions\Renderer\Renderer;

$this->app->singleton(ExceptionRenderer::class, function ($app) {
    return new LaravelRenderer(
        $app[Renderer::class], $app['request']
    );
});

// It's really clear, isn't it?
$this->app->singleton(ExceptionRenderer::class, IgnitionExceptionRenderer::class);
$this->app->singleton(ExceptionRenderer::class, WhoopsExceptionRenderer::class);
$this->app->singleton(ExceptionRenderer::class, AnotherCustomExceptionRenderer::class);

The renderExceptionContent method looks simple as it did before.

protected function renderExceptionContent(Throwable $e)
{
    try {
        if (config('app.debug') && app()->has(ExceptionRenderer::class)) {
            return $this->renderExceptionWithCustomRenderer($e);
        }

        return $this->renderExceptionWithSymfony($e, config('app.debug'));
    } catch (Throwable $e) {
        return $this->renderExceptionWithSymfony($e, config('app.debug'));
    }
}

@@ -135,8 +137,6 @@ public function registerDumper()
* Register the "validate" macro on the request.
*
* @return void
*
* @throws \Illuminate\Validation\ValidationException
Copy link
Contributor Author

@seriquynh seriquynh Aug 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method just registers macro methods for the Request class. Validation exceptions are only thrown when $request->validate() or $request->validateWithBag() are called and failed.

@taylorotwell
Copy link
Member

Thanks for your pull request to Laravel!

Unfortunately, I'm going to delay merging this code for now. To preserve our ability to adequately maintain the framework, we need to be very careful regarding the amount of code we include.

If applicable, please consider releasing your code as a package so that the community can still take advantage of your contributions!

@seriquynh seriquynh deleted the unify-exception-renderer branch August 6, 2024 14:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants