177 lines
6.0 KiB
PHP
177 lines
6.0 KiB
PHP
<?php
|
|
|
|
namespace App\Exceptions;
|
|
|
|
use App\Traits\ApiResponser;
|
|
use ErrorException;
|
|
use GuzzleHttp\Exception\ClientException;
|
|
use GuzzleHttp\Exception\ConnectException;
|
|
use GuzzleHttp\Exception\ServerException;
|
|
use Illuminate\Auth\Access\AuthorizationException;
|
|
use Illuminate\Auth\AuthenticationException;
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
|
use Illuminate\Database\QueryException;
|
|
use Illuminate\Http\Response;
|
|
use Illuminate\Support\Facades\Lang;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Validation\ValidationException;
|
|
use Laravel\Lumen\Exceptions\Handler as ExceptionHandler;
|
|
use Symfony\Component\HttpKernel\Exception\HttpException;
|
|
use Throwable;
|
|
|
|
class Handler extends ExceptionHandler
|
|
{
|
|
use ApiResponser;
|
|
|
|
/**
|
|
* A list of the exception types that should not be reported.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $dontReport = [
|
|
AuthorizationException::class,
|
|
HttpException::class,
|
|
ModelNotFoundException::class,
|
|
ValidationException::class,
|
|
\Stripe\Exception\ApiErrorException::class,
|
|
];
|
|
|
|
/**
|
|
* Report or log an exception.
|
|
*
|
|
* This is a great spot to send exceptions to Sentry, Bugsnag, etc.
|
|
*
|
|
* @param \Throwable $exception
|
|
* @return void
|
|
*
|
|
* @throws \Exception
|
|
*/
|
|
public function report(Throwable $exception)
|
|
{
|
|
parent::report($exception);
|
|
}
|
|
|
|
/**
|
|
* Render an exception into an HTTP response.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @param \Throwable $exception
|
|
* @return \Illuminate\Http\Response|\Illuminate\Http\JsonResponse
|
|
*
|
|
* @throws \Throwable
|
|
*/
|
|
public function render($request, Throwable $exception)
|
|
{
|
|
// return parent::render($request, $exception);
|
|
if ($exception instanceof HttpException) {
|
|
$code = $exception->getStatusCode();
|
|
$message = Response::$statusTexts[$code];
|
|
|
|
return $this->errorResponse($message, $code);
|
|
}
|
|
|
|
if ($exception instanceof ModelNotFoundException) {
|
|
$model = strtolower(class_basename($exception->getModel()));
|
|
|
|
return $this->errorResponse(trans('errors.model_not_found', ['model' => $model]),
|
|
Response::HTTP_NOT_FOUND);
|
|
}
|
|
|
|
if ($exception instanceof AuthorizationException) {
|
|
return $this->errorResponse($exception->getMessage(), Response::HTTP_UNAUTHORIZED);
|
|
}
|
|
|
|
if ($exception instanceof ValidationException) {
|
|
$errors = $exception->validator->errors()->getMessages();
|
|
$message = '';
|
|
foreach ($errors as $val) {
|
|
foreach ($val as $validation) {
|
|
$message .= $validation . "\n";
|
|
}
|
|
$message .= "\n";
|
|
}
|
|
return $this->errorResponse($message, Response::HTTP_UNPROCESSABLE_ENTITY);
|
|
}
|
|
|
|
|
|
if ($exception instanceof AuthenticationException) {
|
|
return $this->errorResponse($exception->getMessage(), Response::HTTP_UNAUTHORIZED);
|
|
}
|
|
|
|
if ($exception instanceof QueryException) {
|
|
return $this->errorResponse($exception->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR);
|
|
}
|
|
|
|
if ($exception instanceof ServerException) {
|
|
return $this->errorResponse($exception->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR);
|
|
}
|
|
|
|
if ($exception instanceof ErrorException) {
|
|
return $this->errorResponse($exception->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR);
|
|
}
|
|
|
|
if ($exception instanceof ClientException) {
|
|
$response = json_decode($exception->getResponse()->getBody()->getContents());
|
|
$code = $exception->getCode();
|
|
|
|
return $this->errorResponse($response->message ?? $response , $code);
|
|
}
|
|
|
|
if($exception instanceof ConnectException){
|
|
return $this->errorResponse(trans('errors.timeout'), Response::HTTP_REQUEST_TIMEOUT);
|
|
}
|
|
|
|
//Stripe Exceptions
|
|
//https://stripe.com/docs/api/errors/handling?lang=php
|
|
// if($exception instanceof \Stripe\Exception\CardException){
|
|
// // Since it's a decline, \Stripe\Exception\CardException will be caught
|
|
// echo 'Status is:' . $exception->getHttpStatus() . '\n';
|
|
// echo 'Type is:' . $exception->getError()->type . '\n';
|
|
// echo 'Code is:' . $exception->getError()->code . '\n';
|
|
// // param is '' in this case
|
|
// echo 'Param is:' . $exception->getError()->param . '\n';
|
|
// echo 'Message is:' . $exception->getError()->message . '\n';
|
|
//
|
|
// }
|
|
//
|
|
// if($exception instanceof \Stripe\Exception\RateLimitException){
|
|
// }
|
|
//
|
|
// if($exception instanceof \Stripe\Exception\InvalidRequestException){
|
|
// }
|
|
//
|
|
// if($exception instanceof \Stripe\Exception\AuthenticationException){
|
|
// }
|
|
//
|
|
// if($exception instanceof \Stripe\Exception\ApiConnectionException){
|
|
// }
|
|
|
|
if($exception instanceof \Stripe\Exception\ApiErrorException){
|
|
$errorMessage = $exception->getMessage();
|
|
Log::error("Error Stripe API");
|
|
|
|
//https://stripe.com/docs/error-codes
|
|
//https://stripe.com/docs/declines/codes
|
|
$code = $exception->getError()->decline_code ?? $exception->getError()->code;
|
|
if(Lang::has('stripe.'.$code)){
|
|
Log::error($code);
|
|
$errorMessage = __('stripe.'.$code);
|
|
}
|
|
|
|
Log::error($errorMessage);
|
|
return $this->errorResponse($errorMessage ?? __('errors.unexpected_error'));
|
|
}
|
|
|
|
// if ($exception instanceof AppException) {
|
|
// return $this->errorResponse($exception->getMessage(), $exception->getCode());
|
|
// }
|
|
|
|
if (!config('variables.app_debug')) {
|
|
return parent::render($request, $exception);
|
|
}
|
|
|
|
return $this->errorResponse(trans('errors.unexpected_error'),
|
|
Response::HTTP_INTERNAL_SERVER_ERROR);
|
|
}
|
|
}
|