I am trying to ensure that exceptions thrown from database connection issues are handled globally.
I have added the following in my app\Exceptions\Handler.php in the render() method, however none of the exceptions are being caught:
<?php
namespace App\Exceptions;
use Exception;
use Illuminate\Database\QueryException;
use Illuminate\Validation\ValidationException;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Laravel\Lumen\Exceptions\Handler as ExceptionHandler;
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use PDOException;
class Handler extends ExceptionHandler
{
/**
* A list of the exception types that should not be reported.
*
* @var array
*/
protected $dontReport = [
AuthorizationException::class,
HttpException::class,
ModelNotFoundException::class,
ValidationException::class,
];
/**
* Report or log an exception.
*
* This is a great spot to send exceptions to Sentry, Bugsnag, etc.
*
* @param \Exception $e
* @return void
*/
public function report(Exception $e)
{
parent::report($e);
}
/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Exception $e
* @return \Illuminate\Http\Response
*/
public function render($request, Exception $e)
{
if ($e instanceof AuthorizationException) {
return response()->json((['status' => 403, 'message' => 'Insufficient privileges to perform this action']), 403);
}
if ($e instanceof MethodNotAllowedHttpException) {
return response()->json((['status' => 405, 'message' => 'Method Not Allowed']), 405);
}
if ($e instanceof NotFoundHttpException) {
return response()->json((['status' => 404, 'message' => 'The requested resource was not found']), 404);
}
if ($e instanceof QueryException) {
return response()->json((['id' => 0, 'status_billing' => 'The requested resource was not found']), 500);
}
if ($e instanceof PDOException) {
return response()->json((['id' => 0, 'status_billing' => 'The requested resource was not found']), 500);
}
return parent::render($request, $e);
}
}
This was also added to my app.php:
$app->singleton( App\Exceptions\Handler::class );
For the life of me I cant get this to work.
Any help/advice would be greatly appreciated
Thanks