<?php

namespace App;

use Exception;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Foundation\Exceptions\Handler;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

class ExceptionHandler extends Handler
{
    /**
     * A list of the exception types that should not be reported.
     *
     * @var array
     */
    protected $dontReport = [
        \Symfony\Component\HttpKernel\Exception\HttpException::class,
        \Illuminate\Database\Eloquent\ModelNotFoundException::class,
    ];

    /**
     * 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)
    {
        parent::report($exception);
    }

    /**
     * Render an exception into an HTTP response.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  \Exception               $exception
     * @return \Illuminate\Http\Response
     */
    public function render($request, Exception $exception)
    {
        if (($exception instanceof ModelNotFoundException || $exception instanceof NotFoundHttpException) && $request->expectsJson()) {
            $result         = [];
            $result['msg']  = "URL未正常识别！请确认路由配置是否正确。";
            $result['code'] = "9999";
            return response()->json($result, 200, [], JSON_UNESCAPED_UNICODE);
        }

        if ($request->expectsJson()) {
            $msg            = $exception->getMessage();
            $result         = [];
            $result['msg']  = empty($msg) ? '系统错误请联系管理员' : $msg;
            $result['code'] = "9999";
            return response()->json($result, 200, [], JSON_UNESCAPED_UNICODE);
        }
        return parent::render($request, $exception);
    }
}
