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 文件里边

  • 相关阅读:
    机器学习笔记(二)---- 线性回归
    机器学习笔记(一)----基本概念
    ZZ:SDNLAB技术分享(一):ODL的SFC入门和Demo
    zz:NETCONF协议详解
    技术谈 | SDN 和 NFV 之间的爱与恨
    华为云内容审核—性能更加狂野,价格更加腼腆
    【并发技术01】传统线程技术中创建线程的两种方式
    【开发者portal在线开发插件系列五】命令的响应,即命令结果的上报(mid的使用)
    【并发技术16】线程同步工具Exchanger的使用
    什么是API文档?--斯科特·马文
  • 原文地址:https://www.cnblogs.com/richerdyoung/p/10044760.html
Copyright © 2011-2022 走看看