Skip to content

Commit

Permalink
Enhance exception handling in middleware (#523)
Browse files Browse the repository at this point in the history
* Enhance exception handling in middleware and response traits for improved error logging and JSON responses

* Refactor error handling in UserDefaultDatabaseConnectionMiddleware for clarity
  • Loading branch information
beesaferoot authored Mar 3, 2025
1 parent 03e1c56 commit 3efe4ae
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
4 changes: 3 additions & 1 deletion src/backend/app/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ public function report(\Throwable $exception) {
*/
public function render($request, \Exception|\Throwable $exception) {
// api request outputs are json
if (preg_match('/.*\.local\/api.*/', $request->url()) || preg_match('/.*\.com\/api.*/', $request->url())) {
if ($request->expectsJson()
|| preg_match('/.*\.local\/api.*/', $request->url())
|| preg_match('/.*\.com\/api.*/', $request->url())) {
return $this->getJsonResponseForException($request, $exception);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

namespace App\Http\Middleware;

use App\Exceptions\Handler;
use App\Exceptions\ValidationException;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
use MPM\DatabaseProxy\DatabaseProxyManagerService;
use MPM\Sharding\ApiCompanyResolverService;
Expand All @@ -23,10 +25,21 @@ public function __construct(
) {}

public function handle($request, \Closure $next) {
if ($request instanceof Request) {
return $this->handleApiRequest($request, $next);
try {
if ($request instanceof Request) {
return $this->handleApiRequest($request, $next);
}
throw new ValidationException('was not able to handle the request');
} catch (\Exception $e) {
Log::error('Middleware Exception: '.$e->getMessage(), [
'exception' => $e,
]);
// Either handle directly or ensure it propagates to your handler
if ($request->expectsJson() || strpos($request->url(), '/api') !== false) {
return app(Handler::class)->render($request, $e);
}
throw $e;
}
throw new ValidationException('was not able to handle the request');
}

private function handleApiRequest(Request $request, \Closure $next) {
Expand Down
4 changes: 2 additions & 2 deletions src/backend/app/Traits/RestExceptionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace App\Traits;

use App\Exceptions\ValidationException;
use Exception;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
use Tymon\JWTAuth\Exceptions\JWTException;
use Tymon\JWTAuth\Exceptions\TokenExpiredException;
use Tymon\JWTAuth\Exceptions\TokenInvalidException;
Expand All @@ -27,7 +27,7 @@ protected function getJsonResponseForException(Request $request, \Exception|\Thr
case $e instanceof TokenInvalidException:
return response()->json(['error' => 'Token is invalid'], 401);
case $e instanceof JWTException:
return response()->json(['error' => 'There was an issue with the token'], 401);
return response()->json(['error' => 'Unauthorized. '.$e->getMessage().' Make sure you are logged in.'], 401);
case $this->isModelNotFoundException($e):
$response = $this->modelNotFound(['model not found '.implode(' ', $e->getIds()), $e->getMessage(), $e->getTrace()]);
break;
Expand Down

0 comments on commit 3efe4ae

Please sign in to comment.