exception

作者:Shine 发布于:2018-01-29 18:08:19 浏览:911次 分类:PHP

exception

public function index()
{
   throw new APIHttpException('wrong');
}
public function __construct($message = '', Exception $previous = null, array $headers = array(), $code = 0)
{
   if (!$message) $message = $this->message;

   parent::__construct($this->statusCode, $message, $previous, $headers, $code);
}
/**
* Render an exception into an HTTP response.
*
* @param  \Illuminate\Http\Request $request
* @param  \Exception $e
* @return \Illuminate\Http\Response
*/
public function render($request, Exception $e)
{
   ##如果config配置debugtrue ==>debug模式的话让laravel自行处理
   if (config('app.debug')) {
       return parent::render($request, $e);
   }
   return $this->handle($request, $e);
}


/**
* @param $request
* @param Exception $e
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Response
*/
public function handle($request, Exception $e)
{
   ##只处理自定义的APIException异常
   if ($e instanceof APIHttpException) {
       $result = [
           "msg"    => "",
           "data"   => $e->getMessage(),
           "status" => 0
       ];
       return response()->json($result);
   }
   return parent::render($request, $e);
}


标签: laravel exception
声明:文章内容由作者原创或整理,转载请标明出处!
暂留位置!--请勿随意修改