¨4.0.1¨
This commit is contained in:
@@ -5,8 +5,12 @@ namespace FleetCart\Exceptions;
|
||||
use Throwable;
|
||||
use Illuminate\Http\Request;
|
||||
use Swift_TransportException;
|
||||
use Illuminate\Http\Response;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Modules\Sms\Exceptions\SmsException;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Modules\Checkout\Http\Exceptions\CheckoutException;
|
||||
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
||||
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
@@ -32,62 +36,60 @@ class Handler extends ExceptionHandler
|
||||
'password_confirmation',
|
||||
];
|
||||
|
||||
|
||||
/**
|
||||
* Report or log an exception.
|
||||
*
|
||||
* This is a great spot to send exceptions to Sentry, Bugsnag, etc.
|
||||
*
|
||||
* @param \Throwable $e
|
||||
* @param Throwable $e
|
||||
*
|
||||
* @return void
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function report(Throwable $e)
|
||||
public function report(Throwable $e): void
|
||||
{
|
||||
parent::report($e);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Render an exception into an HTTP response.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param \Throwable $e
|
||||
* @return \Illuminate\Http\Response
|
||||
* @param Request $request
|
||||
* @param Throwable $e
|
||||
*
|
||||
* @return Response|JsonResponse|RedirectResponse
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function render($request, Throwable $e)
|
||||
{
|
||||
if ($e instanceof Swift_TransportException) {
|
||||
return $this->handleSwiftException($request, $e);
|
||||
}
|
||||
|
||||
if ($e instanceof SmsException) {
|
||||
return $this->handleSmsException($request, $e);
|
||||
}
|
||||
|
||||
if ($e instanceof ValidationException && $request->ajax()) {
|
||||
return response()->json([
|
||||
return match (true) {
|
||||
$e instanceof Swift_TransportException => $this->handleSwiftException($request, $e),
|
||||
$e instanceof SmsException => $this->handleSmsException($request, $e),
|
||||
$e instanceof ValidationException && $request->ajax() => response()->json([
|
||||
'message' => trans('core::messages.the_given_data_was_invalid'),
|
||||
'errors' => $e->validator->getMessageBag(),
|
||||
], 422);
|
||||
}
|
||||
|
||||
if ($this->shouldRedirectToAdminDashboard($e)) {
|
||||
return redirect()->route('admin.dashboard.index');
|
||||
}
|
||||
|
||||
if ($this->shouldShowNotFoundPage($e)) {
|
||||
return response()->view('errors.404');
|
||||
}
|
||||
|
||||
return parent::render($request, $e);
|
||||
], 422),
|
||||
$e instanceof CheckoutException => response()->json([
|
||||
'message' => $e->getMessage(),
|
||||
], Response::HTTP_FORBIDDEN),
|
||||
$this->shouldRedirectToAdminDashboard($e) => redirect()->route('admin.dashboard.index'),
|
||||
$this->shouldShowNotFoundPage($e) => response()->view('errors.404'),
|
||||
default => parent::render($request, $e),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Handle swift transport exception.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param \Swift_TransportException $e
|
||||
* @param Request $request
|
||||
* @param Swift_TransportException $e
|
||||
*
|
||||
* @return mixed
|
||||
*
|
||||
* @throws \Swift_TransportException
|
||||
* @throws Swift_TransportException
|
||||
*/
|
||||
private function handleSwiftException(Request $request, Swift_TransportException $e)
|
||||
{
|
||||
@@ -103,14 +105,16 @@ class Handler extends ExceptionHandler
|
||||
->with('error', trans('core::messages.mail_is_not_configured'));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Handle sms exception.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param \Modules\Sms\Exceptions\SmsException $e
|
||||
* @return mixed
|
||||
* @param Request $request
|
||||
* @param SmsException $e
|
||||
*
|
||||
* @throws \Modules\Sms\Exceptions\SmsException
|
||||
* @return RedirectResponse
|
||||
*
|
||||
* @throws SmsException
|
||||
*/
|
||||
private function handleSmsException(Request $request, SmsException $e)
|
||||
{
|
||||
@@ -125,28 +129,43 @@ class Handler extends ExceptionHandler
|
||||
return back()->withInput()->with('error', $e->getMessage());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Determine whether response should redirect to the admin dashboard.
|
||||
*
|
||||
* @param \Throwable $e
|
||||
* @param Throwable $e
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function shouldRedirectToAdminDashboard(Throwable $e)
|
||||
private function shouldRedirectToAdminDashboard(Throwable $e): bool
|
||||
{
|
||||
if (config('app.debug') || ! $this->inAdminPanel()) {
|
||||
if (config('app.debug') || !$this->inAdminPanel()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $e instanceof NotFoundHttpException || $e instanceof ModelNotFoundException;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Determine if the request is from admin panel.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function inAdminPanel(): bool
|
||||
{
|
||||
return $this->container->has('inAdminPanel') && $this->container['inAdminPanel'];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Determine if the response should show not found page.
|
||||
*
|
||||
* @param \Throwable $e
|
||||
* @param Throwable $e
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function shouldShowNotFoundPage(Throwable $e)
|
||||
private function shouldShowNotFoundPage(Throwable $e): bool
|
||||
{
|
||||
if ($this->inAdminPanel()) {
|
||||
return false;
|
||||
@@ -154,14 +173,4 @@ class Handler extends ExceptionHandler
|
||||
|
||||
return $e instanceof NotFoundHttpException || $e instanceof ModelNotFoundException;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the request is from admin panel.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function inAdminPanel()
|
||||
{
|
||||
return $this->container->has('inAdminPanel') && $this->container['inAdminPanel'];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user