Files
nexusphp/app/Exceptions/Handler.php

117 lines
3.2 KiB
PHP
Raw Normal View History

2021-04-02 19:48:41 +08:00
<?php
namespace App\Exceptions;
use Illuminate\Auth\AuthenticationException;
2021-04-28 01:22:25 +08:00
use Illuminate\Database\Eloquent\ModelNotFoundException;
2021-04-02 19:48:41 +08:00
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Arr;
2021-04-28 01:22:25 +08:00
use Illuminate\Validation\UnauthorizedException;
2021-04-02 19:48:41 +08:00
use Illuminate\Validation\ValidationException;
2021-04-23 01:28:41 +08:00
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
2021-04-02 19:48:41 +08:00
use Throwable;
class Handler extends ExceptionHandler
{
/**
* A list of the exception types that are not reported.
*
* @var array
*/
protected $dontReport = [
//
];
/**
* A list of the inputs that are never flashed for validation exceptions.
*
* @var array
*/
protected $dontFlash = [
'current_password',
'password',
'password_confirmation',
];
/**
* Register the exception handling callbacks for the application.
*
* @return void
*/
public function register()
{
2022-06-03 03:42:53 +08:00
if (!request()->expectsJson()) {
return;
}
2021-04-02 19:48:41 +08:00
$this->reportable(function (Throwable $e) {
//
});
$this->renderable(function (AuthenticationException $e) {
return response()->json(fail($e->getMessage(), $e->guards()), 401);
});
2021-04-28 01:22:25 +08:00
$this->renderable(function (UnauthorizedException $e) {
return response()->json(fail($e->getMessage(), request()->all()), 403);
});
2021-04-02 19:48:41 +08:00
$this->renderable(function (ValidationException $exception) {
$errors = $exception->errors();
$msg = Arr::first(Arr::first($errors));
return response()->json(fail($msg, $errors));
});
2021-04-23 01:28:41 +08:00
$this->renderable(function (NotFoundHttpException $e) {
2021-04-28 01:22:25 +08:00
if ($e->getPrevious() && $e->getPrevious() instanceof ModelNotFoundException) {
2022-03-30 15:37:11 +08:00
$exception = $e->getPrevious();
return response()->json(fail($exception->getMessage(), request()->all()), 404);
2021-04-28 01:22:25 +08:00
}
2021-04-23 01:28:41 +08:00
});
2021-04-02 19:48:41 +08:00
}
/**
* Prepare a JSON response for the given exception.
*
* @param \Illuminate\Http\Request $request
* @param \Throwable $e
* @return \Illuminate\Http\JsonResponse
*/
protected function prepareJsonResponse($request, Throwable $e)
{
2021-04-23 01:28:41 +08:00
$data = $request->all();
2021-05-20 23:30:34 +08:00
$httpStatusCode = $this->getHttpStatusCode($e);
2022-03-04 16:16:56 +08:00
$msg = $e->getMessage();
$trace = $e->getTraceAsString();
2021-05-20 23:30:34 +08:00
if (config('app.debug')) {
$data['trace'] = $trace;
2021-05-20 23:30:34 +08:00
}
2021-04-02 19:48:41 +08:00
return new JsonResponse(
2021-04-23 01:28:41 +08:00
fail($msg, $data),
2021-05-20 23:30:34 +08:00
$httpStatusCode,
2021-04-02 19:48:41 +08:00
$this->isHttpException($e) ? $e->getHeaders() : [],
JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES
);
}
2021-04-28 01:22:25 +08:00
protected function getHttpStatusCode(Throwable $e)
{
2022-03-30 15:37:11 +08:00
if (
$e instanceof NexusException
|| $e instanceof \InvalidArgumentException
|| $e instanceof \LogicException
|| $e instanceof \RuntimeException
) {
2021-04-28 01:22:25 +08:00
return 200;
}
if ($this->isHttpException($e)) {
return $e->getStatusCode();
}
return 500;
}
2021-04-02 19:48:41 +08:00
}