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

78 lines
2.5 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
use Exception;
use Illuminate\Auth\AuthenticationException;
use Doctrine\DBAL\Driver\PDOException;
2018-12-01 10:03:32 +00:00
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Symfony\Component\HttpKernel\Exception\HttpException;
2020-05-09 10:29:34 +00:00
use App\Exceptions\Handler as AppExceptionHandler;
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
{
2019-03-13 11:17:52 +00:00
protected $jsonErrorMessages = [
404 => 'Resource not found',
403 => '403 forbidden Error',
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
2020-03-05 13:37:08 +00:00
* @param \Exception $exception
2018-10-17 07:40:08 +00:00
* @return \Illuminate\Http\Response
*/
public function render($request, Exception $exception)
{
2019-11-05 13:32:07 +00:00
$path = $this->isAdminUri() ? 'admin' : 'shop';
2018-11-29 12:09:39 +00:00
2018-10-17 07:40:08 +00:00
if ($exception instanceof HttpException) {
2019-03-13 11:17:52 +00:00
$statusCode = in_array($exception->getStatusCode(), [401, 403, 404, 503]) ? $exception->getStatusCode() : 500;
2018-12-01 10:03:32 +00:00
return $this->response($path, $statusCode);
2020-02-24 12:19:12 +00:00
} elseif ($exception instanceof ModelNotFoundException) {
2018-12-01 10:03:32 +00:00
return $this->response($path, 404);
2020-02-24 12:19:12 +00:00
} elseif ($exception instanceof PDOException) {
2018-12-01 10:03:32 +00:00
return $this->response($path, 500);
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);
}
return redirect()->guest(route('auth.login'));
}
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
}
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])
2020-02-28 10:25:53 +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);
}
2018-10-17 07:40:08 +00:00
}