美文网首页
[PHP高可用后端]②②--不可预知的内部异常API数据输出解决

[PHP高可用后端]②②--不可预知的内部异常API数据输出解决

作者: 子木同 | 来源:发表于2017-11-15 15:14 被阅读29次
    image.png

    报错的内容应该以API形式显示

    image.png image.png

    ->必须覆盖render方法

    image.png

    ApiHandleException.php

    <?php
    /**
     * Created by PhpStorm.
     * User: tong
     * Date: 2017/11/15
     * Time: 14:13
     */
    
    namespace app\common\lib\exception;
    
    use Exception;
    use think\exception\Handle;
    
    class ApiHandleException extends Handle
    {
        /**
         * http状态码
         * @var int 500 内部错误
         */
        public $httpCode = 500;
    
        public function render(Exception $e)
        {
            return show(0, $e->getMessage(), [], $this->httpCode);
        }
    }
    

    Test.php

    /**
         * post 新增
         * @return mixed
         */
        public function save()
        {
            if(ids){
                echo "test";
            }
    //        try{
    //            model('asasas');
    //        }catch(\Exception $e){
    //            return show(0,$e->getMessage(),400);
    //        }
    
            //获取到提交数据 插入库
            //给客户端APP =》 接口数据
    
            //201 创建成功
            return show(1, 'OK', input('post.'), 201);
        }
    

    config.php

      'exception_handle' => '\app\common\lib\exception\ApiHandleException',
    
    image.png

    Test.php

    public function save()
        {
            $data = input('post.');
            if($data['mt']!=1){
                exception('您提交的数据不合法',403);
            }
            //获取到提交数据 插入库
            //给客户端APP =》 接口数据
    
            //201 创建成功
            return show(1, 'OK', input('post.'), 201);
        }
    
    image.png

    状态码仍然是500

    =>

    ApiHandleException

    <?php
    /**
     * Created by PhpStorm.
     * User: tong
     * Date: 2017/11/15
     * Time: 14:13
     */
    
    namespace app\common\lib\exception;
    
    use Exception;
    use think\exception\Handle;
    
    class ApiHandleException extends Handle
    {
        /**
         * http状态码
         * @var int 500 内部错误
         */
        public $httpCode = 500;
    
        public function render(Exception $e)
        {
            //给服务端看的
            if (config('app_debug') == true) {
                return parent::render($e);
            }
            if ($e instanceof ApiException) {
                $this->httpCode = $e->httpCode;
            }
            return show(0, $e->getMessage(), [], $this->httpCode);
        }
    }
    

    ApiException

    <?php
    /**
     * Created by PhpStorm.
     * User: tong
     * Date: 2017/11/15
     * Time: 14:55
     */
    
    namespace app\common\lib\exception;
    
    use think\Exception;
    
    class ApiException extends Exception
    {
        public $message = '';
    
        public $httpCode = 500;
    
        public $code = 0;
    
    
        /**
         * ApiException constructor.
         * @param string $message
         * @param int $httpCode
         * @param int $code
         */
        public function __construct($message = "", $httpCode = 0, $code = 0)
        {
            $this->httpCode = $httpCode;
            $this->message = $message;
            $this->code = $code;
    
        }
    }
    

    Test.php

    <?php
    /**
     * Created by PhpStorm.
     * User: tong
     * Date: 2017/11/15
     * Time: 10:23
     */
    
    namespace app\api\controller;
    
    use app\common\lib\exception\ApiException;
    use think\Controller;
    
    class Test extends Controller
    {
    
        public function index()
        {
            return [
                'sgsg',
                'sgsgs',
            ];
        }
    
        public function update($id = 0)
        {
    //        return $id;//http://singwa.com/test/100
    
            halt(input('put.'));//body提交
    
        }
    
        /**
         * post 新增
         * @return mixed
         */
        public function save()
        {
            $data = input('post.');
            if($data['mt']!=1){
                //exception('您提交的数据不合法',403);
                throw new ApiException('您提交的数据不合法~~~',403);
            }
            //获取到提交数据 插入库
            //给客户端APP =》 接口数据
    
            //201 创建成功
            return show(1, 'OK', input('post.'), 201);
        }
    }
    

    app_debug==true

    image.png

    app_debug==false

    image.png

    相关文章

      网友评论

          本文标题:[PHP高可用后端]②②--不可预知的内部异常API数据输出解决

          本文链接:https://www.haomeiwen.com/subject/faicvxtx.html