Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

vincent15000's avatar

Manage exceptions only in production ?

Hello,

Hmmm ... I'd like to manage exceptions only in production.

Why ? In development, when an error occurs, built-in in Laravel, it's automatically displayed on the screen.

If I add custom exceptions, I won't have the standard error display on the screen, but my custom exception rendering. But this custom rendering if for the end users and not for me as developer.

What is the best way to customize the exceptions only in production ?

Thanks for your help.

V

0 likes
5 replies
tykus's avatar
tykus
Best Answer
Level 104

You can check the APP_ENV whenever rendering the Exception, e.g.

// bootstrap/app.php
->withExceptions(function (Exceptions $exceptions) {
    $exceptions->render(function (YourCustomException $e, Request $request) {
        if (app()->isProduction()) {
            return response()->view('errors.your-custom-template', status: 500);
        }
        throw $e;
    });
})

Obviously, move this logic into the Exception class if it is a renderable Exception.

1 like
vincent15000's avatar

@tykus Thank you ... hmmm ... and what if I want to simply return to the page and only display an error message (flash message).

The problem is : the user triggers an action (it can simply be a post action). I don't want that the rendering is on the post route.

The best for me would be to redirect to the page (as it would be if the action was successful) and only display an error message on the screen for the end user (and also log the error message).

Sure I can return an error message directly from the redirection in the controller, but is there a way to do that from a custom Exception if needed ?

Snapey's avatar

if you want to show the error in the page, you should catch the exception with try-catch and in the catch, show your message to your user and Log the error for your own review later.

1 like
martinbean's avatar

Hmmm ... I'd like to manage exceptions only in production.

@vincent15000 Use the logging-related environment variables to dictate how and where log entries are written.

Usually, you write logs to the storage/logs/laravel.log file in local environments, and use something like Sentry to capture errors in a production environment. You’d achieve this by setting the LOG_CHANNEL environment to the appropriate value given the environment, i.e. LOG_CHANNEL=stack locally but LOG_CHANNEL=sentry in production. You don’t need custom code to handle exceptions differently for different environments.

Please or to participate in this conversation.