ldc-crm/app/Exceptions/Handler.php

185 lines
5.8 KiB
PHP
Raw Normal View History

2020-12-01 17:02:10 +01:00
<?php
namespace App\Exceptions;
2021-03-14 10:45:42 +01:00
use App\Enums\ApiError;
use App\Enums\ApiStatus;
use App\Exceptions\Api\V2\ApiErrorException;
use App\Exceptions\Api\V2\CrmException;
use App\Http\Resources\Api\V2\ApiResponse as V2ApiResponse;
2021-03-14 10:45:42 +01:00
use App\Lib\Helper;
2021-08-02 19:06:01 +02:00
use App\Mail\ExceptionAlert;
2021-03-14 10:45:42 +01:00
use Illuminate\Auth\AuthenticationException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
2020-12-01 17:02:10 +01:00
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
2021-08-04 09:33:28 +02:00
use Illuminate\Http\Exceptions\ThrottleRequestsException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
2021-03-14 10:45:42 +01:00
use Illuminate\Support\Facades\Mail;
use Illuminate\Validation\ValidationException;
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
use Throwable;
2020-12-01 17:02:10 +01:00
class Handler extends ExceptionHandler
{
2021-03-14 10:45:42 +01:00
2020-12-01 17:02:10 +01:00
/**
* A list of the exception types that are not reported.
*
* @var array
*/
protected $dontReport = [
2021-03-14 10:45:42 +01:00
ApiErrorException::class
2020-12-01 17:02:10 +01:00
];
/**
* A list of the inputs that are never flashed for validation exceptions.
*
* @var array
*/
protected $dontFlash = [
'password',
'password_confirmation',
];
/**
* Register the exception handling callbacks for the application.
*
* @return void
*/
public function register()
{
//
}
2021-03-14 10:45:42 +01:00
/**
* Report or log an exception.
*
* @param \Throwable $exception
* @return void
*
* @throws \Exception
*/
public function report(Throwable $e)
{
2021-08-02 19:06:01 +02:00
if (get_class($e) == RedirectTo::class) {
abort(redirect($e->url));
}
2021-03-14 10:45:42 +01:00
try {
if (!config('app.debug')) {
if ($this->shouldntReport($e)) {
return;
}
2021-08-02 19:06:01 +02:00
Mail::send(new ExceptionAlert($e));
2021-03-14 10:45:42 +01:00
}
} finally {
parent::report($e);
}
}
/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Throwable $e
* @return \Symfony\Component\HttpFoundation\Response
*
* @throws \Throwable
*/
public function render($request, Throwable $e)
{
2021-08-04 09:33:28 +02:00
if (Helper::isRequestForApiV2($request) and $request->isJson()) {
2021-03-14 10:45:42 +01:00
switch (get_class($e)) {
case AuthenticationException::class:
/** @var AuthenticationException $e */
return $this->getReponse(
$request,
407,
ApiError::needAuth()->value,
sprintf('%s, %s', ApiError::needAuth()->label, $e->getMessage())
);
2021-03-14 10:45:42 +01:00
case MethodNotAllowedHttpException::class:
/** @var MethodNotAllowedHttpException $e */
return $this->getReponse(
$request,
$e->getStatusCode(),
ApiError::methodNotAllowed()->value,
sprintf('%s, %s', ApiError::methodNotAllowed()->label, $e->getMessage())
);
2021-03-14 10:45:42 +01:00
case ValidationException::class:
/** @var ValidationException $e */
return $this->getReponse(
$request,
$e->status,
ApiError::validationFailed()->value,
sprintf('%s, %s', ApiError::validationFailed()->label, $e->getMessage()),
['errors' => $e->errors()]
);
2021-03-14 10:45:42 +01:00
case ModelNotFoundException::class:
/** @var ModelNotFoundException $e */
return $this->getReponse(
$request,
404,
ApiError::modelNotFound()->value,
sprintf(
2021-03-14 10:45:42 +01:00
ApiError::modelNotFound()->label,
\array_slice(explode('\\', $e->getModel()), -1)[0]
)
);
2021-03-14 10:45:42 +01:00
case ApiErrorException::class:
/** @var ApiErrorException $e */
return $this->getReponse(
$request,
$e->getStatusCode(),
$e->getCode(),
$e->getMessage()
);
2021-08-04 09:33:28 +02:00
case ThrottleRequestsException::class:
/** @var ThrottleRequestsException $e */
return $this->getReponse(
$request,
429,
ApiError::throttleRequestsException()->value,
ApiError::throttleRequestsException()->label
);
2021-03-14 10:45:42 +01:00
case CrmException::class:
default:
if (config('app.env') === 'testing') {
// phpcs:ignore Squiz.PHP.DiscouragedFunctions.Found
print_r((string)$e);
}
return $this->getReponse($request, 500, $e->getCode(), $e->getMessage());
2021-03-14 10:45:42 +01:00
}
}
return parent::render($request, $e);
}
/**
* Generate an ApiV2 json response
*
* @param Request $request
* @param integer $httpCode
* @param integer $apiCode
* @param string $message
* @return JsonResponse
*/
private static function getReponse(
Request $request,
int $httpCode,
int $apiCode,
string $message,
array $data = []
): JsonResponse {
return (new V2ApiResponse(
(object)array_merge(
$data,
[
'message' => $message,
'code' => $apiCode
]
),
ApiStatus::KO()
))->toResponse($request)->setStatusCode($httpCode);
}
2020-12-01 17:02:10 +01:00
}