walletservice/app/Exceptions/Handler.php

148 lines
4.9 KiB
PHP
Raw Normal View History

2020-04-15 23:08:09 +00:00
<?php
namespace App\Exceptions;
use App\Traits\ApiResponser;
use ErrorException;
2020-05-01 19:14:33 +00:00
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\ServerException;
2020-04-15 23:08:09 +00:00
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Database\QueryException;
2020-04-15 23:08:09 +00:00
use Illuminate\Http\Response;
2020-12-01 13:47:33 +00:00
use Illuminate\Support\Facades\Log;
2020-04-15 23:08:09 +00:00
use Illuminate\Validation\ValidationException;
use Laravel\Lumen\Exceptions\Handler as ExceptionHandler;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Throwable;
class Handler extends ExceptionHandler
{
use ApiResponser;
2020-12-01 13:47:33 +00:00
2020-04-15 23:08:09 +00:00
/**
* A list of the exception types that should not be reported.
*
* @var array
*/
protected $dontReport = [
AuthorizationException::class,
HttpException::class,
ModelNotFoundException::class,
ValidationException::class,
];
/**
* Report or log an exception.
*
* This is a great spot to send exceptions to Sentry, Bugsnag, etc.
*
2020-12-01 13:47:33 +00:00
* @param \Throwable $exception
2020-04-15 23:08:09 +00:00
* @return void
*
* @throws \Exception
*/
public function report(Throwable $exception)
{
parent::report($exception);
}
/**
* Render an exception into an HTTP response.
*
2020-12-01 13:47:33 +00:00
* @param \Illuminate\Http\Request $request
* @param \Throwable $exception
2020-04-15 23:08:09 +00:00
* @return \Symfony\Component\HttpFoundation\Response
*
* @throws \Throwable
*/
public function render($request, Throwable $exception)
{
// return parent::render($request, $exception);
2020-12-01 13:47:33 +00:00
if ($exception instanceof HttpException) {
2020-04-15 23:08:09 +00:00
$code = $exception->getStatusCode();
$message = Response::$statusTexts[$code];
2020-12-01 13:47:33 +00:00
return $this->errorResponse($message, $code);
2020-04-15 23:08:09 +00:00
}
2020-12-01 13:47:33 +00:00
if ($exception instanceof ModelNotFoundException) {
2020-04-15 23:08:09 +00:00
$model = strtolower(class_basename($exception->getModel()));
2020-12-01 13:47:33 +00:00
return $this->errorResponse(trans('errors.model_not_found', ['model' => $model]),
2020-04-15 23:08:09 +00:00
Response::HTTP_NOT_FOUND);
}
2020-12-01 13:47:33 +00:00
if ($exception instanceof AuthorizationException) {
return $this->errorResponse($exception->getMessage(), Response::HTTP_UNAUTHORIZED);
2020-04-15 23:08:09 +00:00
}
2020-12-01 13:47:33 +00:00
if ($exception instanceof ValidationException) {
2020-04-15 23:08:09 +00:00
$errors = $exception->validator->errors()->getMessages();
2020-11-02 09:34:35 +00:00
$message = '';
2021-12-22 09:42:27 +00:00
foreach ($errors as $val) {
2020-11-02 09:34:35 +00:00
foreach ($val as $validation) {
2021-12-22 09:42:27 +00:00
$message .= $validation . "\n";
2020-11-02 09:34:35 +00:00
}
$message .= "\n";
}
return $this->errorResponse($message, Response::HTTP_UNPROCESSABLE_ENTITY);
2020-04-15 23:08:09 +00:00
}
2020-12-01 13:47:33 +00:00
if ($exception instanceof AuthenticationException) {
return $this->errorResponse($exception->getMessage(), Response::HTTP_UNAUTHORIZED);
2020-04-15 23:08:09 +00:00
}
2020-12-01 13:47:33 +00:00
if ($exception instanceof QueryException) {
return $this->errorResponse($exception->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR);
}
2020-12-01 13:47:33 +00:00
if ($exception instanceof ServerException) {
return $this->errorResponse($exception->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR);
}
2020-04-15 23:08:09 +00:00
if ($exception instanceof ErrorException) {
2020-12-01 13:47:33 +00:00
return $this->errorResponse($exception->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR);
2020-06-05 17:00:16 +00:00
}
2020-12-01 13:47:33 +00:00
if ($exception instanceof ClientException) {
2020-05-01 19:14:33 +00:00
$message = $exception->getResponse()->getBody()->getContents();
2020-12-01 13:47:33 +00:00
$error = json_decode($message);
2020-05-01 19:14:33 +00:00
$code = $exception->getCode();
2020-12-01 13:47:33 +00:00
if ($error) {
2020-08-09 22:13:21 +00:00
if (isset($error->message)) {
2020-06-05 17:00:16 +00:00
$message = json_decode($error->message);
2020-08-09 22:13:21 +00:00
if (isset($message->errorMessage))
return $this->errorResponse($message->errorMessage, $code);
return $this->errorResponse($error->message, $code);
2020-06-05 17:00:16 +00:00
}
if (isset($error->error)) {
2020-12-01 13:47:33 +00:00
try {
$message = json_decode($error->error);
if (isset($message->message))
return $this->errorResponse($message->message, $code);
} catch (\Exception $e) {
Log::error($e->getMessage());
}
return $this->errorResponse(json_encode($error->error), $code);
}
2020-12-01 13:47:33 +00:00
}
return $this->errorResponse($message, $code);
2020-05-01 19:14:33 +00:00
}
2022-04-15 18:09:52 +00:00
if ($exception instanceof AppException) {
return $this->errorResponse($exception->getMessage(), $exception->getCode());
}
2020-12-01 13:47:33 +00:00
if (env('APP_DEBUG', false)) {
return parent::render($request, $exception);
2020-04-15 23:08:09 +00:00
}
2020-04-28 16:12:05 +00:00
return $this->errorResponse(trans('errors.unexpected_error'),
2020-04-15 23:08:09 +00:00
Response::HTTP_INTERNAL_SERVER_ERROR);
}
}