douqiang5163 2017-04-24 06:41
浏览 123
已采纳

如何拦截laravel http错误响应

I am trying to handle all errors thrown by a Laravel project

I overrode some of the code in the file app\Exception\Handler.php

public function render($request, Exception $e)
{
    $err = New Response;
    $err->Response=0;
    $err->Message = $e;
    return json_encode($err);
    //return parent::render($request, $e);
}

I commented the render function and tried to return an instance of my custom error class

However this did not work, because i am returning a different type (response does not extend ExceptionHandler

Is there a way to do what i want?

  • 写回答

1条回答 默认 最新

  • douou9786 2017-04-24 06:46
    关注

    try this code. does this help!

       public function render($request, Exception $e)
            {
                   return \Response::json([
                            'success' => false,
                            'message' => $e->getMessage(),
                      ], $e->getCode());
                   } 
             }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?