| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- <?php
- namespace App\Exceptions;
- use Exception;
- use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
- use Symfony\Component\ErrorHandler\Exception\FlattenException;
- 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 = [
- 'password',
- 'password_confirmation',
- ];
- /**
- * Report or log an exception.
- *
- * @param \Exception $exception
- * @return void
- *
- * @throws \Exception
- */
- public function report(Exception $exception)
- {
- parent::report($exception);
- }
- /**
- * Render an exception into an HTTP response.
- *
- * @param \Illuminate\Http\Request $request
- * @param \Exception $exception
- * @return \Symfony\Component\HttpFoundation\Response
- *
- * @throws \Exception
- */
- public function render($request, Exception $exception)
- {
- if ($this->shouldntReport($exception)) {
- // 因为laravel-admin利用flush返回错误,必须先这样排除.
- return parent::render($request, $exception);
- }
- //匹配获取新的抛出实例
- $e = FlattenException::create($exception);
- $statusCode = $e->getStatusCode();
- $isDebug = config('app.debug');
- $code = 500; //输出的错误代码
- $msg404 = array('code' => $statusCode, 'message' => 'Nothing to do!', 'data' => [], 'handshake' => '');
- if ($isDebug === false) { //生产环境下
- if ($request->wantsJson() || $request->isJson() || $request->ajax()) {
- return response()->json($msg404, $statusCode);
- } else {
- if (view()->exists('errors.' . $statusCode)) {
- return response()->view('errors.' . $statusCode, [], $statusCode);
- }
- }
- return response()->view('errors.404', [], $statusCode);
- }
- return parent::render($request, $exception);
- }
- }
|