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

kitensei's avatar

After middleware not invoked when raisin exception

When using an after middleware to transform data :

<?php namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;

class JsonMiddleware
{
    /**
     * Handle an incoming request.
     * @param  Request $request
     * @param  Closure|callable $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $response = $next($request);

        dd($response);

        return $response;
    }
}

Kernel.php

<?php

namespace App\Http;

use App\Http\Middleware\JsonMiddleware;
use Illuminate\Foundation\Http\Kernel as HttpKernel;

class Kernel extends HttpKernel
{
    protected $middleware = [
        JsonMiddleware::class,
    ];

    protected $routeMiddleware = [
    ];
}

Exception\Handler

<?php

namespace App\Exceptions;

use Exception;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;

class Handler extends ExceptionHandler
{
    protected $dontReport = [
        HttpException::class,
        ModelNotFoundException::class,
    ];

    public function report(Exception $e)
    {
        return parent::report($e);
    }

    public function render($request, Exception $e)
    {    
        return response($e->getMessage());
    }
}

routes.php

<?php

use App\Exceptions\FunException;

Route::get('/', function () {
    return response('This should be displayed as JSON payload - code 200');
});

Route::get('/fun', function () {
    throw new FunException('This is a fun exception, code 433');
});

The dd($response) is never ran when raising an exception, can I change that ?

0 likes
3 replies
pmall's avatar

it is very normal the execution of the application stops when there is a uncached exception no ?

kitensei's avatar

Exception sent to the Handler ARE catched, here I'm trying to return a response when an exception is catched by the handler, but when the response is sent, no AFTER middleware is called (if I use a BEFORE middleware it gets called)

Maybe I am missing a big point here, but this don't seem obvious to me :/

What I want to achieve it's convert response to json in one single place so I can keep returning response() everywhere without taking care of the fact that I need json or not.

neomerx's avatar

@kitensei If exception is thrown it goes directly to exception handler and you can't go back. Typical problem that arises is that you want either add something to the response (e.g. headers) or modify the response itself (e.g. convert to json).

The second problem could be solved by taking out conversion logic out from middleware. The code will be used in both places your middleware and exception handler.

The first problem a bit trickier but has a good solution with Containers. You can put to container your data and use it in your conversion logic. For example

$key = YourDataClass::class;

// put to container
$data = new YourDataClass(...);
app()->instance($key, $data);

// take from container
$data = app($key);

$key actually can be any string value and $data could be just array but I personally prefer classes.

Please or to participate in this conversation.