86 lines
2.0 KiB
PHP
86 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace App\Exceptions;
|
|
|
|
use Catch\Enums\Code;
|
|
use Catch\Exceptions\CatchException;
|
|
use Catch\Exceptions\FailedException;
|
|
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
use Throwable;
|
|
|
|
class Handler extends ExceptionHandler
|
|
{
|
|
/**
|
|
* A list of exception types with their corresponding custom log levels.
|
|
*
|
|
* @var array<class-string<\Throwable>, \Psr\Log\LogLevel::*>
|
|
*/
|
|
protected $levels = [
|
|
//
|
|
];
|
|
|
|
/**
|
|
* A list of the exception types that are not reported.
|
|
*
|
|
* @var array<int, class-string<\Throwable>>
|
|
*/
|
|
protected $dontReport = [
|
|
//
|
|
];
|
|
|
|
/**
|
|
* A list of the inputs that are never flashed to the session on validation exceptions.
|
|
*
|
|
* @var array<int, string>
|
|
*/
|
|
protected $dontFlash = [
|
|
'current_password',
|
|
'password',
|
|
'password_confirmation',
|
|
];
|
|
|
|
/**
|
|
* Register the exception handling callbacks for the application.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function register()
|
|
{
|
|
$this->reportable(function (Throwable $e) {
|
|
//
|
|
});
|
|
}
|
|
|
|
|
|
/**
|
|
* render
|
|
*
|
|
* @param $request
|
|
* @param Throwable $e
|
|
* @return JsonResponse|Response
|
|
* @throws Throwable
|
|
*/
|
|
public function render($request, Throwable $e): JsonResponse|Response
|
|
{
|
|
$message = $e->getMessage();
|
|
|
|
if (method_exists($e, 'getStatusCode')) {
|
|
if ($e->getStatusCode() == Response::HTTP_NOT_FOUND) {
|
|
$message = '路由未找到或未注册';
|
|
}
|
|
}
|
|
|
|
$e = new FailedException($message ?: 'Server Error', $e instanceof CatchException ? $e->getCode() : Code::FAILED);
|
|
|
|
$response = parent::render($request, $e);
|
|
|
|
$response->header('Access-Control-Allow-Origin', '*');
|
|
$response->header('Access-Control-Allow-Methods', '*');
|
|
$response->header('Access-Control-Allow-Headers', '*');
|
|
|
|
return $response;
|
|
}
|
|
}
|