134 lines
4.4 KiB
PHP
134 lines
4.4 KiB
PHP
<?php
|
|
|
|
namespace App\Exceptions;
|
|
|
|
use App\Enums\ApiError;
|
|
use App\Exceptions\Api\v2\ApiErrorException;
|
|
use App\Exceptions\Api\v2\CrmException;
|
|
use App\Lib\Helper;
|
|
use Illuminate\Auth\AuthenticationException;
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
|
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
|
|
use Illuminate\Support\Facades\Mail;
|
|
use Illuminate\Validation\ValidationException;
|
|
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
|
|
use Throwable;
|
|
|
|
class Handler extends ExceptionHandler
|
|
{
|
|
|
|
/**
|
|
* A list of the exception types that are not reported.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $dontReport = [
|
|
ApiErrorException::class
|
|
];
|
|
|
|
/**
|
|
* 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()
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Report or log an exception.
|
|
*
|
|
* @param \Throwable $exception
|
|
* @return void
|
|
*
|
|
* @throws \Exception
|
|
*/
|
|
public function report(Throwable $e)
|
|
{
|
|
try {
|
|
if (!config('app.debug')) {
|
|
if ($this->shouldntReport($e)) {
|
|
return;
|
|
}
|
|
Mail::send('mails.exception', ['exception' => $e], function ($m) {
|
|
$m->to(
|
|
config('mail.contactsList.serviceweb'),
|
|
sprintf('%s Exception Handler', config('app.name'))
|
|
)->subject('Need help !, an error occured in your application, please check Logs ASAP');
|
|
});
|
|
}
|
|
} 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)
|
|
{
|
|
|
|
if (Helper::isRequestForApiV2($request)) {
|
|
switch (get_class($e)) {
|
|
case AuthenticationException::class:
|
|
/** @var AuthenticationException $e */
|
|
return response()->json([
|
|
'message' => sprintf('%s, %s', ApiError::needAuth()->label, $e->getMessage()),
|
|
'code' => ApiError::needAuth()->value,
|
|
], 407);
|
|
case MethodNotAllowedHttpException::class:
|
|
/** @var MethodNotAllowedHttpException $e */
|
|
return response()->json([
|
|
'message' => sprintf('%s, %s', ApiError::methodNotAllowed()->label, $e->getMessage()),
|
|
'code' => ApiError::methodNotAllowed()->value,
|
|
], $e->getStatusCode());
|
|
case ValidationException::class:
|
|
/** @var ValidationException $e */
|
|
return response()->json([
|
|
'message' => sprintf('%s, %s', ApiError::validationFailed()->label, $e->getMessage()),
|
|
'code' => ApiError::validationFailed()->value,
|
|
'errors' => $e->errors()
|
|
], $e->status);
|
|
case ModelNotFoundException::class:
|
|
/** @var ModelNotFoundException $e */
|
|
return response()->json([
|
|
'message' => sprintf(
|
|
ApiError::modelNotFound()->label,
|
|
\array_slice(explode('\\', $e->getModel()), -1)[0]
|
|
),
|
|
'code' => ApiError::modelNotFound()->value
|
|
], 404);
|
|
case ApiErrorException::class:
|
|
/** @var ApiErrorException $e */
|
|
return response()->json([
|
|
'message' => $e->getMessage(),
|
|
'code' => $e->getCode()
|
|
], $e->getStatusCode());
|
|
case CrmException::class:
|
|
default:
|
|
return response()->json([
|
|
'message' => $e->getMessage(),
|
|
'code' => $e->getCode()
|
|
], 500);
|
|
}
|
|
}
|
|
return parent::render($request, $e);
|
|
}
|
|
}
|