laravel 接口異常錯(cuò)誤返回json數(shù)據(jù)

在laravel中默認(rèn)返回的異常是html的形式展現(xiàn)在頁(yè)面上面渔伯,當(dāng)我們寫api接口時(shí)候希望返回json給前端希望能夠不會(huì)出現(xiàn)神奇的bug迅诬。

找到文件Exceptions\Handler.php重寫render方法

重寫代碼

 /**
     * 重寫render
     * @param $request
     * @param Throwable $e
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Response|\Symfony\Component\HttpFoundation\Response|void
     * @throws Throwable
     */
    public function render($request, Throwable $e)
    {
        if (!$request->is('api/*')) {
            return parent::render($request, $e);
        }


        $error = $this->convertExceptionToResponse($e);
        $api_e = FlattenException::create($e);
        $debug['line'] =  $api_e->getLine();
        $debug['file'] =  $api_e->getFile();
        $debug['class'] = $api_e->getClass();
        $debug['trace'] = $api_e->getTrace();
        return response()->json([
            'status' => false,
            'code'=>$error->getStatusCode(),
            'message'=>$api_e->getMessage(),
            'data'=>$debug,
        ], $error->getStatusCode());
    }

返回內(nèi)容

{
    "status": false,
    "code": 404,
    "message": "",
    "data": {
        "line": 43,
        "file": "/project_path/vendor/laravel/framework/src/Illuminate/Routing/AbstractRouteCollection.php",
        "class": "Symfony\\Component\\HttpKernel\\Exception\\NotFoundHttpException",
        "trace": [
            {
                "namespace": "",
                "short_class": "",
                "class": "",
                "type": "",
                "function": "",
                "file": "/project_path/vendor/laravel/framework/src/Illuminate/Routing/AbstractRouteCollection.php",
                "line": 43,
                "args": []
            },
            {
                "namespace": "Illuminate\\Routing",
                "short_class": "AbstractRouteCollection",
                "class": "Illuminate\\Routing\\AbstractRouteCollection",
                "type": "->",
                "function": "handleMatchedRoute",
                "file": "/project_path/vendor/laravel/framework/src/Illuminate/Routing/RouteCollection.php",
                "line": 162,
                "args": [
                    [
                        "object",
                        "Illuminate\\Http\\Request"
                    ],
                    [
                        "null",
                        null
                    ]
                ]
            },
            {
                "namespace": "Illuminate\\Routing",
                "short_class": "RouteCollection",
                "class": "Illuminate\\Routing\\RouteCollection",
                "type": "->",
                "function": "match",
                "file": "/project_path/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
                "line": 673,
                "args": [
                    [
                        "object",
                        "Illuminate\\Http\\Request"
                    ]
                ]
            },
            {
                "namespace": "Illuminate\\Routing",
                "short_class": "Router",
                "class": "Illuminate\\Routing\\Router",
                "type": "->",
                "function": "findRoute",
                "file": "/project_path/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
                "line": 662,
                "args": [
                    [
                        "object",
                        "Illuminate\\Http\\Request"
                    ]
                ]
            },
            {
                "namespace": "Illuminate\\Routing",
                "short_class": "Router",
                "class": "Illuminate\\Routing\\Router",
                "type": "->",
                "function": "dispatchToRoute",
                "file": "/project_path/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
                "line": 651,
                "args": [
                    [
                        "object",
                        "Illuminate\\Http\\Request"
                    ]
                ]
            },
            {
                "namespace": "Illuminate\\Routing",
                "short_class": "Router",
                "class": "Illuminate\\Routing\\Router",
                "type": "->",
                "function": "dispatch",
                "file": "/project_path/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php",
                "line": 167,
                "args": [
                    [
                        "object",
                        "Illuminate\\Http\\Request"
                    ]
                ]
            },
            {
                "namespace": "Illuminate\\Foundation\\Http",
                "short_class": "Kernel",
                "class": "Illuminate\\Foundation\\Http\\Kernel",
                "type": "->",
                "function": "Illuminate\\Foundation\\Http\\{closure}",
                "file": "/project_path/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
                "line": 128,
                "args": [
                    [
                        "object",
                        "Illuminate\\Http\\Request"
                    ]
                ]
            },
            {
                "namespace": "Illuminate\\Pipeline",
                "short_class": "Pipeline",
                "class": "Illuminate\\Pipeline\\Pipeline",
                "type": "->",
                "function": "Illuminate\\Pipeline\\{closure}",
                "file": "/project_path/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php",
                "line": 21,
                "args": [
                    [
                        "object",
                        "Illuminate\\Http\\Request"
                    ]
                ]
            },
            {
                "namespace": "Illuminate\\Foundation\\Http\\Middleware",
                "short_class": "TransformsRequest",
                "class": "Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest",
                "type": "->",
                "function": "handle",
                "file": "/project_path/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ConvertEmptyStringsToNull.php",
                "line": 31,
                "args": [
                    [
                        "object",
                        "Illuminate\\Http\\Request"
                    ],
                    [
                        "object",
                        "Closure"
                    ]
                ]
            },
            {
                "namespace": "Illuminate\\Foundation\\Http\\Middleware",
                "short_class": "ConvertEmptyStringsToNull",
                "class": "Illuminate\\Foundation\\Http\\Middleware\\ConvertEmptyStringsToNull",
                "type": "->",
                "function": "handle",
                "file": "/project_path/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
                "line": 167,
                "args": [
                    [
                        "object",
                        "Illuminate\\Http\\Request"
                    ],
                    [
                        "object",
                        "Closure"
                    ]
                ]
            },
            {
                "namespace": "Illuminate\\Pipeline",
                "short_class": "Pipeline",
                "class": "Illuminate\\Pipeline\\Pipeline",
                "type": "->",
                "function": "Illuminate\\Pipeline\\{closure}",
                "file": "/project_path/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php",
                "line": 21,
                "args": [
                    [
                        "object",
                        "Illuminate\\Http\\Request"
                    ]
                ]
            },
            {
                "namespace": "Illuminate\\Foundation\\Http\\Middleware",
                "short_class": "TransformsRequest",
                "class": "Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest",
                "type": "->",
                "function": "handle",
                "file": "/project_path/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TrimStrings.php",
                "line": 40,
                "args": [
                    [
                        "object",
                        "Illuminate\\Http\\Request"
                    ],
                    [
                        "object",
                        "Closure"
                    ]
                ]
            },
            {
                "namespace": "Illuminate\\Foundation\\Http\\Middleware",
                "short_class": "TrimStrings",
                "class": "Illuminate\\Foundation\\Http\\Middleware\\TrimStrings",
                "type": "->",
                "function": "handle",
                "file": "/project_path/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
                "line": 167,
                "args": [
                    [
                        "object",
                        "Illuminate\\Http\\Request"
                    ],
                    [
                        "object",
                        "Closure"
                    ]
                ]
            },
            {
                "namespace": "Illuminate\\Pipeline",
                "short_class": "Pipeline",
                "class": "Illuminate\\Pipeline\\Pipeline",
                "type": "->",
                "function": "Illuminate\\Pipeline\\{closure}",
                "file": "/project_path/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php",
                "line": 27,
                "args": [
                    [
                        "object",
                        "Illuminate\\Http\\Request"
                    ]
                ]
            },
            {
                "namespace": "Illuminate\\Foundation\\Http\\Middleware",
                "short_class": "ValidatePostSize",
                "class": "Illuminate\\Foundation\\Http\\Middleware\\ValidatePostSize",
                "type": "->",
                "function": "handle",
                "file": "/project_path/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
                "line": 167,
                "args": [
                    [
                        "object",
                        "Illuminate\\Http\\Request"
                    ],
                    [
                        "object",
                        "Closure"
                    ]
                ]
            },
            {
                "namespace": "Illuminate\\Pipeline",
                "short_class": "Pipeline",
                "class": "Illuminate\\Pipeline\\Pipeline",
                "type": "->",
                "function": "Illuminate\\Pipeline\\{closure}",
                "file": "/project_path/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/PreventRequestsDuringMaintenance.php",
                "line": 86,
                "args": [
                    [
                        "object",
                        "Illuminate\\Http\\Request"
                    ]
                ]
            },
            {
                "namespace": "Illuminate\\Foundation\\Http\\Middleware",
                "short_class": "PreventRequestsDuringMaintenance",
                "class": "Illuminate\\Foundation\\Http\\Middleware\\PreventRequestsDuringMaintenance",
                "type": "->",
                "function": "handle",
                "file": "/project_path/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
                "line": 167,
                "args": [
                    [
                        "object",
                        "Illuminate\\Http\\Request"
                    ],
                    [
                        "object",
                        "Closure"
                    ]
                ]
            },
            {
                "namespace": "Illuminate\\Pipeline",
                "short_class": "Pipeline",
                "class": "Illuminate\\Pipeline\\Pipeline",
                "type": "->",
                "function": "Illuminate\\Pipeline\\{closure}",
                "file": "/project_path/vendor/fruitcake/laravel-cors/src/HandleCors.php",
                "line": 52,
                "args": [
                    [
                        "object",
                        "Illuminate\\Http\\Request"
                    ]
                ]
            },
            {
                "namespace": "Fruitcake\\Cors",
                "short_class": "HandleCors",
                "class": "Fruitcake\\Cors\\HandleCors",
                "type": "->",
                "function": "handle",
                "file": "/project_path/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
                "line": 167,
                "args": [
                    [
                        "object",
                        "Illuminate\\Http\\Request"
                    ],
                    [
                        "object",
                        "Closure"
                    ]
                ]
            },
            {
                "namespace": "Illuminate\\Pipeline",
                "short_class": "Pipeline",
                "class": "Illuminate\\Pipeline\\Pipeline",
                "type": "->",
                "function": "Illuminate\\Pipeline\\{closure}",
                "file": "/project_path/vendor/laravel/framework/src/Illuminate/Http/Middleware/TrustProxies.php",
                "line": 39,
                "args": [
                    [
                        "object",
                        "Illuminate\\Http\\Request"
                    ]
                ]
            },
            {
                "namespace": "Illuminate\\Http\\Middleware",
                "short_class": "TrustProxies",
                "class": "Illuminate\\Http\\Middleware\\TrustProxies",
                "type": "->",
                "function": "handle",
                "file": "/project_path/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
                "line": 167,
                "args": [
                    [
                        "object",
                        "Illuminate\\Http\\Request"
                    ],
                    [
                        "object",
                        "Closure"
                    ]
                ]
            },
            {
                "namespace": "Illuminate\\Pipeline",
                "short_class": "Pipeline",
                "class": "Illuminate\\Pipeline\\Pipeline",
                "type": "->",
                "function": "Illuminate\\Pipeline\\{closure}",
                "file": "/project_path/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
                "line": 103,
                "args": [
                    [
                        "object",
                        "Illuminate\\Http\\Request"
                    ]
                ]
            },
            {
                "namespace": "Illuminate\\Pipeline",
                "short_class": "Pipeline",
                "class": "Illuminate\\Pipeline\\Pipeline",
                "type": "->",
                "function": "then",
                "file": "/project_path/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php",
                "line": 142,
                "args": [
                    [
                        "object",
                        "Closure"
                    ]
                ]
            },
            {
                "namespace": "Illuminate\\Foundation\\Http",
                "short_class": "Kernel",
                "class": "Illuminate\\Foundation\\Http\\Kernel",
                "type": "->",
                "function": "sendRequestThroughRouter",
                "file": "/project_path/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php",
                "line": 111,
                "args": [
                    [
                        "object",
                        "Illuminate\\Http\\Request"
                    ]
                ]
            },
            {
                "namespace": "Illuminate\\Foundation\\Http",
                "short_class": "Kernel",
                "class": "Illuminate\\Foundation\\Http\\Kernel",
                "type": "->",
                "function": "handle",
                "file": "/project_path/public/index.php",
                "line": 52,
                "args": [
                    [
                        "object",
                        "Illuminate\\Http\\Request"
                    ]
                ]
            },
            {
                "namespace": "",
                "short_class": "",
                "class": "",
                "type": "",
                "function": "require_once",
                "file": "/project_path/server.php",
                "line": 21,
                "args": [
                    [
                        "string",
                        "/project_path/public/index.php"
                    ]
                ]
            }
        ]
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末因惭,一起剝皮案震驚了整個(gè)濱河市蒿褂,隨后出現(xiàn)的幾起案子倦淀,更是在濱河造成了極大的恐慌蒋畜,老刑警劉巖,帶你破解...
    沈念sama閱讀 212,080評(píng)論 6 493
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件撞叽,死亡現(xiàn)場(chǎng)離奇詭異姻成,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)愿棋,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,422評(píng)論 3 385
  • 文/潘曉璐 我一進(jìn)店門科展,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人糠雨,你說(shuō)我怎么就攤上這事辛润。” “怎么了?”我有些...
    開封第一講書人閱讀 157,630評(píng)論 0 348
  • 文/不壞的土叔 我叫張陵砂竖,是天一觀的道長(zhǎng)真椿。 經(jīng)常有香客問(wèn)我,道長(zhǎng)乎澄,這世上最難降的妖魔是什么突硝? 我笑而不...
    開封第一講書人閱讀 56,554評(píng)論 1 284
  • 正文 為了忘掉前任,我火速辦了婚禮置济,結(jié)果婚禮上解恰,老公的妹妹穿的比我還像新娘。我一直安慰自己浙于,他們只是感情好护盈,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,662評(píng)論 6 386
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著羞酗,像睡著了一般腐宋。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上檀轨,一...
    開封第一講書人閱讀 49,856評(píng)論 1 290
  • 那天胸竞,我揣著相機(jī)與錄音,去河邊找鬼参萄。 笑死卫枝,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的讹挎。 我是一名探鬼主播校赤,決...
    沈念sama閱讀 39,014評(píng)論 3 408
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼筒溃!你這毒婦竟也來(lái)了痒谴?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,752評(píng)論 0 268
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤铡羡,失蹤者是張志新(化名)和其女友劉穎积蔚,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體烦周,經(jīng)...
    沈念sama閱讀 44,212評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡尽爆,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,541評(píng)論 2 327
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了读慎。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片漱贱。...
    茶點(diǎn)故事閱讀 38,687評(píng)論 1 341
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖夭委,靈堂內(nèi)的尸體忽然破棺而出幅狮,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 34,347評(píng)論 4 331
  • 正文 年R本政府宣布崇摄,位于F島的核電站擎值,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏逐抑。R本人自食惡果不足惜鸠儿,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,973評(píng)論 3 315
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望厕氨。 院中可真熱鬧进每,春花似錦、人聲如沸命斧。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,777評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)国葬。三九已至贤徒,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間胃惜,已是汗流浹背泞莉。 一陣腳步聲響...
    開封第一講書人閱讀 32,006評(píng)論 1 266
  • 我被黑心中介騙來(lái)泰國(guó)打工哪雕, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留船殉,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 46,406評(píng)論 2 360
  • 正文 我出身青樓斯嚎,卻偏偏與公主長(zhǎng)得像利虫,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子堡僻,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,576評(píng)論 2 349

推薦閱讀更多精彩內(nèi)容