162 lines
5.6 KiB
PHP
162 lines
5.6 KiB
PHP
<?php
|
||
|
||
namespace App\Exceptions;
|
||
|
||
use App\Http\Response\ResponseJson;
|
||
use Exception;
|
||
use Illuminate\Http\Response;
|
||
use Illuminate\Http\JsonResponse;
|
||
use Illuminate\Auth\AuthenticationException;
|
||
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
|
||
use App\Utils\Messenger;
|
||
use App\Mail\ErrorMessage;
|
||
use Illuminate\Support\Facades\DB;
|
||
use Illuminate\Support\Facades\Mail;
|
||
use App\Jobs\SendMail;
|
||
|
||
class Handler extends ExceptionHandler
|
||
{
|
||
use ResponseJson;
|
||
/**
|
||
* A list of the exception types that are not reported.
|
||
*
|
||
* @var array
|
||
*/
|
||
protected $dontReport = [
|
||
"\league\oauth2-server\src\Exception\OAuthServerException",
|
||
"\League\OAuth2\Server\Exception\OAuthServerException",
|
||
];
|
||
|
||
/**
|
||
* A list of the inputs that are never flashed for validation exceptions.
|
||
*
|
||
* @var array
|
||
*/
|
||
protected $dontFlash = [
|
||
'password',
|
||
'password_confirmation',
|
||
];
|
||
|
||
/**
|
||
* Report or log an exception.
|
||
*
|
||
* This is a great spot to send exceptions to Sentry, Bugsnag, etc.
|
||
*
|
||
* @param \Exception $exception
|
||
* @return void
|
||
*/
|
||
public function report(Exception $exception)
|
||
{
|
||
$error = $exception->getMessage();
|
||
$error_arr = [
|
||
'The resource owner or authorization server denied the request.',
|
||
'The given payload is invalid.'
|
||
];
|
||
if (app()->bound('sentry') && $this->shouldReport($exception) && !in_array($error, $error_arr)) {
|
||
app('sentry')->captureException($exception);
|
||
}
|
||
parent::report($exception);
|
||
// if(method_exists($exception, 'getStatusCode')){
|
||
// $status = $exception->getStatusCode();
|
||
// if ($status == 405 || $status == 404) return;
|
||
// }elseif($exception instanceof AuthenticationException){
|
||
// //403不报警
|
||
// return;
|
||
// }else{
|
||
// $status = get_class($exception);
|
||
// }
|
||
// $route = Request()->getMethod().':'.Request()->getRequestUri();
|
||
// if(\Route::getFacadeRoot()->current()){
|
||
// $action = $status.':'.\Route::getCurrentRoute()->getActionName();
|
||
// }else{
|
||
// $action = $status.':非法路由访问错误';
|
||
// }
|
||
// if (method_exists($exception, 'getPrevious')) {
|
||
// $err = $exception->getPrevious();
|
||
// if (!empty($err)) {
|
||
// $error = $err->getMessage();
|
||
// }
|
||
// }
|
||
// $files = explode('/', $exception->getFile());
|
||
// $file = $files[count($files)-1];
|
||
// $host = request()->gethost();
|
||
// //参数
|
||
// $params = json_encode(request()->all());
|
||
// //服务器ip
|
||
// $server_ip = isset($_SERVER['REMOTE_ADDR'])?$_SERVER['REMOTE_ADDR']:'';
|
||
// $client = Request()->header('client-os')?:'other';
|
||
// if ($error != 'The given payload is invalid.' && $error != 'The resource owner or authorization server denied the request.') {
|
||
// $message = '服务器异常,前端:'.$client.',ip:'.$server_ip.',域名:'.$host.',位置:'.$route.',操作:'.$action.',控制器:'.$file.',Line:'.$exception->getLine().',参数:'.$params.',用户id:'.(auth()->id()?:'无').',报错内容:'.$error;
|
||
//// SendMail::dispatch($message)->onQueue('error_email');
|
||
// }
|
||
}
|
||
|
||
/**
|
||
* Render an exception into an HTTP response.
|
||
*
|
||
* @param \Illuminate\Http\Request $request
|
||
* @param \Exception $exception
|
||
* @return \Illuminate\Http\Response
|
||
*/
|
||
public function render($request, Exception $e)
|
||
{
|
||
DB::rollBack();
|
||
if (method_exists($e, 'getStatusCode')) {
|
||
if ($e->getStatusCode() == 405) {
|
||
return $this->failure("没有对应的路由方法");
|
||
}elseif ($e->getStatusCode() == 404) {
|
||
return $this->failure("没有对应的路由");
|
||
}
|
||
}
|
||
$this->getError($e);
|
||
$msg = $e->getMessage();
|
||
if ($msg == 'Route [login] not defined.') return $this->authFail();
|
||
if (config('app.env') == 'production') return $this->failure();
|
||
return $request->expectsJson()
|
||
? $this->prepareJsonResponse($request, $e)
|
||
: $this->prepareResponse($request, $e);
|
||
}
|
||
|
||
public function prepareJsonResponse($request, Exception $e)
|
||
{
|
||
$data['status'] = $this->isHttpException($e) ? $e->getStatusCode() : 500;
|
||
if(config('app.debug')){
|
||
$data['file'] = $e->getFile();
|
||
$data['line'] = $e->getLine();
|
||
$data['traces'] = $e->getTrace();
|
||
}
|
||
|
||
$headers = $this->isHttpException($e) ? $e->getHeaders() : [];
|
||
$message = $e->getMessage();
|
||
if ($message == 'The given payload is invalid.') {
|
||
$message = '服务错误,请重试';
|
||
}
|
||
return new JsonResponse(
|
||
[ 'code'=>1,
|
||
'data'=>$data,
|
||
'message'=>$data['status'].':'.$message
|
||
], 200, $headers,
|
||
JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES
|
||
);
|
||
|
||
}
|
||
|
||
/**
|
||
* Convert an authentication exception into an unauthenticated response.
|
||
*
|
||
* @param \Illuminate\Http\Request $request
|
||
* @param \Illuminate\Auth\AuthenticationException $exception
|
||
* @return \Illuminate\Http\Response
|
||
*/
|
||
protected function unauthenticated($request, AuthenticationException $exception)
|
||
{
|
||
if ($request->expectsJson()) {
|
||
return response()->json(['code'=>2, 'message' => '请登录后访问.'], 200);
|
||
}
|
||
|
||
return redirect()->guest(route('login'));
|
||
}
|
||
|
||
|
||
}
|