sarga/packages/Webkul/Core/src/Exceptions/Handler.php

114 lines
3.2 KiB
PHP
Raw Normal View History

2018-10-17 07:40:08 +00:00
<?php
namespace Webkul\Core\Exceptions;
2018-10-17 07:40:08 +00:00
2021-06-02 09:40:47 +00:00
use App\Exceptions\Handler as AppExceptionHandler;
2021-06-29 08:32:46 +00:00
use Illuminate\Auth\AuthenticationException;
2018-12-01 10:03:32 +00:00
use Illuminate\Database\Eloquent\ModelNotFoundException;
2021-06-29 08:32:46 +00:00
use PDOException;
2018-12-01 10:03:32 +00:00
use Symfony\Component\HttpKernel\Exception\HttpException;
2021-06-29 08:32:46 +00:00
use Throwable;
2018-10-17 07:40:08 +00:00
2020-05-09 10:29:34 +00:00
class Handler extends AppExceptionHandler
2018-10-17 07:40:08 +00:00
{
2021-06-02 09:40:47 +00:00
/**
* Json errors.
*
* @var array
*/
2019-03-13 11:17:52 +00:00
protected $jsonErrorMessages = [
2021-06-02 09:40:47 +00:00
404 => 'Resource Not Found',
403 => '403 Forbidden Error',
2019-03-13 11:17:52 +00:00
401 => 'Unauthenticated',
500 => '500 Internal Server Error',
];
2018-10-17 07:40:08 +00:00
/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
2021-06-02 09:40:47 +00:00
* @param \Throwable $exception
2018-10-17 07:40:08 +00:00
* @return \Illuminate\Http\Response
*/
2020-06-09 07:24:46 +00:00
public function render($request, Throwable $exception)
2018-10-17 07:40:08 +00:00
{
2021-06-29 08:32:46 +00:00
if (! config('app.debug')) {
return $this->renderCustomResponse($request, $exception);
2018-10-17 07:40:08 +00:00
}
return parent::render($request, $exception);
}
2018-12-01 10:03:32 +00:00
2019-03-13 11:17:52 +00:00
/**
* Convert an authentication exception into a response.
*
2020-03-05 13:37:08 +00:00
* @param \Illuminate\Http\Request $request
* @param \Illuminate\Auth\AuthenticationException $exception
2019-03-13 11:17:52 +00:00
* @return \Illuminate\Http\Response
*/
protected function unauthenticated($request, AuthenticationException $exception)
{
if ($request->expectsJson()) {
return response()->json(['error' => $this->jsonErrorMessages[401]], 401);
}
2020-10-05 14:03:57 +00:00
return redirect()->guest(route('customer.session.index'));
2019-03-13 11:17:52 +00:00
}
2021-06-02 09:40:47 +00:00
/**
* Is admin uri.
*
* @return boolean
*/
2018-12-01 10:03:32 +00:00
private function isAdminUri()
{
return strpos(\Illuminate\Support\Facades\Request::path(), 'admin') !== false ? true : false;
2018-12-01 10:03:32 +00:00
}
2021-06-29 08:32:46 +00:00
/**
* Render custom HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Throwable $exception
* @return \Illuminate\Http\Response|null
*/
private function renderCustomResponse($request, Throwable $exception)
{
$path = $this->isAdminUri() ? 'admin' : 'shop';
if ($exception instanceof HttpException) {
$statusCode = in_array($exception->getStatusCode(), [401, 403, 404, 503])
? $exception->getStatusCode()
: 500;
return $this->response($path, $statusCode);
} elseif ($exception instanceof ModelNotFoundException) {
return $this->response($path, 404);
} elseif ($exception instanceof PDOException) {
return $this->response($path, 500);
} else {
return parent::render($request, $exception);
}
}
2021-06-02 09:40:47 +00:00
/**
* Response.
*
* @param string $path
* @param int $statusCode
* @return \Illuminate\Http\Response
*/
2018-12-01 10:03:32 +00:00
private function response($path, $statusCode)
{
2019-03-13 11:17:52 +00:00
if (request()->expectsJson()) {
return response()->json([
2020-02-27 08:03:03 +00:00
'error' => isset($this->jsonErrorMessages[$statusCode])
2021-06-02 09:40:47 +00:00
? $this->jsonErrorMessages[$statusCode]
: 'Something went wrong, please try again later.'
2020-02-27 08:03:03 +00:00
], $statusCode);
2019-03-13 11:17:52 +00:00
}
2018-12-01 10:03:32 +00:00
return response()->view("{$path}::errors.{$statusCode}", [], $statusCode);
}
2021-06-02 09:40:47 +00:00
}