File: /var/www/javago-portal-updates/app/Exceptions/Handler.php
<?php
namespace App\Exceptions;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Throwable;
class Handler extends ExceptionHandler
{
/**
* A list of exception types with their corresponding custom log levels.
*
* @var array<class-string<\Throwable>, \Psr\Log\LogLevel::*>
*/
protected $levels = [
//
];
/**
* A list of the exception types that are not reported.
*
* @var array<int, class-string<\Throwable>>
*/
protected $dontReport = [
//
];
/**
* A list of the inputs that are never flashed to the session on validation exceptions.
*
* @var array<int, string>
*/
protected $dontFlash = [
'current_password',
'password',
'password_confirmation',
];
/**
* Register the exception handling callbacks for the application.
*
* @return void
*/
public function register()
{
$this->reportable(function (Throwable $e) {
//
});
// This single handler will catch all exceptions for API routes
$this->renderable(function (Throwable $e, $request) {
// Check if this is an API request
if ($request->is('cafe-api/*') || $request->wantsJson()) {
// Get appropriate status code
$statusCode = 500;
if (method_exists($e, 'getStatusCode')) {
$statusCode = $e->getStatusCode();
} elseif ($e instanceof \Illuminate\Auth\AuthenticationException) {
$statusCode = 401;
} elseif ($e instanceof \Illuminate\Auth\Access\AuthorizationException) {
$statusCode = 403;
} elseif ($e instanceof \Symfony\Component\HttpKernel\Exception\NotFoundHttpException) {
$statusCode = 404;
} elseif ($e instanceof \Illuminate\Validation\ValidationException) {
$statusCode = 422;
return response()->json([
'status' => false,
'message' => 'Validation error',
'errors' => $e->errors(),
'status_code' => $statusCode
], $statusCode);
}
// Return a simple JSON response
return response()->json([
'status' => false,
'message' => $e->getMessage() ?: 'An error occurred',
'status_code' => $statusCode
], $statusCode);
}
// For non-API requests, let Laravel handle the response normally
return null;
});
}
}