Skip to content

Commit

Permalink
Update: Add Exceptions for API Error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Ph.Hitachi authored and Ph.Hitachi committed Aug 13, 2023
1 parent e6bd31d commit 2b3202f
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions app/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@

namespace App\Exceptions;

use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Throwable;
use App\Support\Exceptions\OAuthException;
use Tymon\JWTAuth\Exceptions\JWTException;
use App\Support\Exceptions\GraphApiException;
use Symfony\Component\HttpFoundation\Response;
use Tymon\JWTAuth\Exceptions\TokenExpiredException;
use Tymon\JWTAuth\Exceptions\TokenInvalidException;
use Tymon\JWTAuth\Exceptions\TokenBlacklistedException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;

class Handler extends ExceptionHandler
{
Expand All @@ -23,8 +30,20 @@ class Handler extends ExceptionHandler
*/
public function register(): void
{
$this->reportable(function (Throwable $e) {
//
});
$this->reportable(
reportUsing: fn(TokenInvalidException | TokenExpiredException | TokenBlacklistedException $e) => throw new OAuthException(code: 'token_could_not_verified')
);

$this->reportable(
reportUsing: fn(JWTException $e) => throw new OAuthException(code: 'token_could_not_parse', statusCode: Response::HTTP_INTERNAL_SERVER_ERROR)
);

$this->reportable(
reportUsing: fn(Throwable $e) => //dd($e)
throw new GraphApiException(
//check if app is in production
message: config('app.env') !== 'production' ? $e->getMessage() : 'Sorry, There was something went wrong on our side, Please try again later.'
)
);
}
}

0 comments on commit 2b3202f

Please sign in to comment.