lcly-data-admin/app/Exceptions/Handler.php

160 lines
4.5 KiB
PHP

<?php
namespace App\Exceptions;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Http\Exceptions\ThrottleRequestsException;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Throwable;
class Handler extends ExceptionHandler
{
/**
* A list of the exception types that are not reported.
*
* @var string[]
*/
protected $dontReport = [
//
];
/**
* A list of the inputs that are never flashed for validation exceptions.
*
* @var string[]
*/
protected $dontFlash = [
'current_password',
'password',
'password_confirmation',
];
/**
* Get the default context variables for logging.
*
* @return array
*/
protected function context()
{
try {
return array_merge(parent::context(), [
'url' => request()->url(),
'params' => request()->except($this->dontFlash),
'accept' => request()->header('accept'),
'client_app' => request()->header('client-app'),
'client_version' => request()->header('client-version'),
'client_platform' => request()->header('client-platform'),
'client_os' => request()->header('client-os'),
]);
} catch (Throwable $e) {
return [];
}
}
/**
* Register the exception handling callbacks for the application.
*
* @return void
*/
public function register()
{
$this->map(ModelNotFoundException::class, function ($e) {
$resource = __($key = 'models.'.$e->getModel());
if ($key === $resource) {
$resource = str_ireplace(['App\\Models\\Admin\\', 'App\\Models\\'], '', $e->getModel());
}
return new HttpException(404, __(':resource not found', ['resource' => $resource]), $e);
});
$this->renderable(function (BizException $e, $request) {
if ($this->shouldReturnJson($request, $e)) {
return response()->json($this->convertExceptionToArray($e), $e->status);
}
if (! config('app.debug')) {
$e = new HttpException($e->status, $e->getMessage(), $e);
}
return $this->prepareResponse($request, $e);
});
$this->renderable(function (ThrottleRequestsException $e, Request $request) {
if ($request->acceptsJson()) {
return response()->json(['code' => 429, 'message' => $e->getMessage()]);
}
return $this->prepareResponse($request, $e);
});
}
/**
* {@inheritdoc}
*/
protected function unauthenticated($request, AuthenticationException $exception)
{
return $this->shouldReturnJson($request, $exception)
? response()->json([
'code' => 401,
'message' => $exception->getMessage(),
], 401)
: redirect()->guest($exception->redirectTo() ?? route('login'));
}
/**
* {@inheritdoc}
*/
protected function invalidJson($request, ValidationException $exception)
{
return response()->json([
'code' => 422,
'message' => $exception->getMessage(),
'errors' => $exception->errors(),
], 200);
}
/**
* {@inheritdoc}
*/
protected function convertExceptionToArray(Throwable $e)
{
$data = [
'code' => 500,
'message' => config('app.debug') ? $e->getMessage() : '服务器繁忙,请稍后再试',
];
if ($this->isBizException($e)) {
$data = [
'code' => $e->getCode(),
'message' => $e->getMessage(),
'data' => null,
];
return $data;
} elseif ($this->isHttpException($e)) {
$data = [
'code' => $e->getStatusCode(),
'message' => $e->getMessage(),
'data' => null,
];
}
return array_merge(parent::convertExceptionToArray($e), $data);
}
/**
* 确认给定的异常是否是自定义业务异常
*
* @param Throwable $e
* @return bool
*/
protected function isBizException(Throwable $e): bool
{
return $e instanceof BizException;
}
}