As title ask, I’m working a SPA backend api project, but since Larave 10 removed MaintenanceModeException
, I wonder what’s the proper way to handle maintenance mode in Laravel 10?
My old way handle the MaintenanceModeException
is like:
/**
* Register the exception handling callbacks for the application.
*/
public function register(): void
{
$this->renderable(function (Throwable $e, $request) {
if($request->is('api/*')) {
switch(true)
{
case $e instanceof MaintenanceModeException:
return (new SystemMaintenanceResponse($e))->response()->setStatusCode(503);
}
}
});
}
And now, Laravel 10 throw HttpException
when in the maintenance mode, but HttpException
contains not just the maintenance exception, there is no document about why remove the MaintenanceModeException
and no new document about how to handle this in Laravel 10.
I need a specific response to tell the front end that I’m in maintenance mode, and I don’t know how to tell exactly if I’m in maintenance mode right now because of Laravel removed the MaintenanceModeException
.
What could I handle the maintenance mode correctly in Laravel 10?
2
Answers
After few days research, I end up with the solution below, it allowed me use the maintenance exception as before.
Create a custom exception by
php artisan make:exception MaintenanceModeException
.Middleware path will be
appHttpMiddlewarePreventRequestsDuringMaintenance
.After it done, I can handle the system maintenance just I did before.
The way can't handle system maintenance by
HttpException
is because of theThrottleRequestsException
also extend theHttpException
, the condition$e instanceof HttpException
will return true on it and also the http status code is also 503 on this exception.As I known, there's more situation like these, for the api project, it seems must have to custom in Laravel 10, the default maintenance mode feature only design for web.
In Laravel 10, the MaintenanceModeException has been removed, and instead, Laravel throws an HttpException with a status code of 503 when the application is in maintenance mode.
To avoid the issue of handling HttpException with a 503 status code, which may include other types of exceptions like TooManyRequestsHttpException, you can check if the application is in maintenance mode using the IlluminateFoundationApplication instance.
Here’s an example of how you can modify the exception handler to check for the maintenance mode using the application instance:
We’re first checking if the application is in maintenance mode using the isDownForMaintenance method of the IlluminateFoundationApplication instance. If the application is in maintenance mode, we can return a JSON response with a specific message and a 503 status code.
If the application is not in maintenance mode and the thrown exception is an instance of MaintenanceModeException, we can return a custom response using your SystemMaintenanceResponse class, as in your original code.
This approach ensures that the correct response is returned when the application is in maintenance mode, regardless of the type of exception that was thrown.