欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

laravel接管Dingo-api和默認(rèn)的錯誤處理方式

 更新時間:2019年10月25日 09:18:30   作者:zhangatle  
今天小編就為大家分享一篇laravel接管Dingo-api和默認(rèn)的錯誤處理方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

接管Dingo-api的錯誤

如上圖所示,AppServiceProvider.php中的register()方法中添加如下代碼

\API::error(function (\Illuminate\Validation\ValidationException $exception){
  $data =$exception->validator->getMessageBag();
   $msg = collect($data)->first();
   if(is_array($msg)){
     $msg = $msg[0];
   }
   return response()->json(['message'=>$msg,'status_code'=>400], 200);
 });
 \API::error(function (\Dingo\Api\Exception\ValidationHttpException $exception){
   $errors = $exception->getErrors();
   return response()->json(['message'=>$errors->first(),'status_code'=>400], 200);
 });

接管laravel的錯誤

在Exceptions的Handler.php的render中寫入以下代碼

public function render($request, Exception $exception)
  {
    if($exception instanceof \Illuminate\Validation\ValidationException){
      $data = $exception->validator->getMessageBag();
      $msg = collect($data)->first();
      if(is_array($msg)){
        $msg = $msg[0];
      }
      return response()->json(['message'=>$msg],200);
    }

    if (in_array('api',$exception->guards())){
      if($exception instanceof AuthenticationException){
        return response()->json(['message'=>'token錯誤'],200);
      }
      if($exception instanceof ModelNotFoundException){
        return response()->json(['message'=>'該模型未找到'],200);
      }

    }

    return parent::render($request, $exception);
  }

以上這篇laravel接管Dingo-api和默認(rèn)的錯誤處理方式就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論