zoukankan      html  css  js  c++  java
  • 【laravel5.6】 laravel 接口 接管 自定义异常类

    1  appexceptions 目录下 新建 Apiexception.php

    <?php 
    namespace AppExceptions; 
     
    /***
     * API 自定义异常类
     */
    use Exception;
    
    class ApiException extends Exception 
    { 
      
        //自定义异常处理
        public function SetErrorMessage($errorMsg='', $errorCode = '500'){
            $this->errorMsg = $errorMsg;
            $this->errorCode = $errorCode;
            return $this;  
    
        }
    
    } 

    2 修改  appexceptionshandler.php 文件

    /**
         * Render an exception into an HTTP response.
         *
         * @param  IlluminateHttpRequest  $request
         * @param  Exception  $exception
         * @return IlluminateHttpResponse
         */
        public function render($request, Exception $exception) 
        { 
            // 如果config配置debug为true ==>debug模式的话让laravel自行处理 
            $debug_status = config('app.debug'); // .env文件配置
            if($debug_status){ 
                return parent::render($request, $exception); 
            } 
            return $this->handle($request, $exception); 
        } 
    
         /**
          * 异常接管
          * 
          */
         public function handle($request, Exception $exception){ 
            //如果是接口请求,则抛出json
            if($request->is('api/*')) { 
                 // 只处理自定义的APIException异常 
                if($exception  instanceof   APPExceptionsApiException ) {  //此处写ApiException文件所处路径
                    $result = [ 
                        "status" => 2 , //操作状态: 1 成功 2 失败
                        "errorCode"=>$exception->errorCode,
                        "msg"    => $exception->errorMsg, 
                        "result"   => '', 
                    ]; 
                    return response()->json($result);
                } 
    
                //此处可以写多个自定义异常类
                if($exception  instanceof   APPExceptionsotherException ) { 
                    $result = [ 
                        "status" => 2 , //操作状态: 1 成功 2 失败
                        "errorCode"=>$exception->errorCode,
                        "msg"    => $exception->errorMsg, 
                        "result"   => '', 
                    ]; 
                    return response()->json($result);
                } 
    
                # 继续写其他自定义异常
                /***
                 * code  
                 * 
                 * ***/
            } 
            return parent::render($request, $exception); 
        } 
        

    3 使用

    <?php
    
    namespace AppHttpControllersTest;
    use IlluminateRoutingController;
    use AppExceptionsApiException;
    
    class IndexController extends Controller
    {
        public function index(){
            throw (new ApiException)->SetErrorMessage("错了",'500');
    
        }
    
    }

    这就ok了

    整体思路: 使用时候,先实例 自定义 异常。把错误信息传过去,  然后会回到 handler.php 里边显示

    注意事项: 

         1 测试时候。要注册一个路由在访问

         2 注意开启debug 在.env 文件里边

  • 相关阅读:
    selenium 8大元素定位方法
    selenium环境安装及简单使用
    Python json序列化和反序列化
    pytest使用allure生成测试报告
    Windows10下JDK15的安装教程
    Ansible:服务器巡检_3、Windows 平台巡检
    转载:shell中各种括号的作用详解()、(())、[]、[[]]、{}(推荐)
    Tips: Linux 中 当形参名称是 变量时,如何取出全部的实参
    Python 问题记录:XXX.whl is not a supported wheel on this platform.
    Ansible:服务器巡检_2、Linux 服务器巡检脚本
  • 原文地址:https://www.cnblogs.com/richerdyoung/p/10044760.html
Copyright © 2011-2022 走看看