zoukankan      html  css  js  c++  java
  • ThinkPHP5 自定义异常

    1.配置config.php

    自定义异常路径:

    // 默认AJAX 数据返回格式,可选json xml ...
    'default_ajax_return' => 'json',
    'exception_handle'       => '\app\exceptions\ExceptionHandle',

    2.

    --------------------------------------------------------

    <?php

    namespace appexceptions;

    use thinkexceptionHandle;

    /**
    * Class ExceptionHandle
    * 全局异常处理
    * @package appexceptions
    */
    class ExceptionHandle extends Handle
    {
    /**
    * 渲染方式
    * @param Exception $e
    * @return hinkResponse
    */
    public function render(Exception $e)
    {
    if ($e instanceof appexceptionsException) {
    return json([
    'code' => $e->getCode(),
    'message' => $e->getMessage()
    ], $e->getCode());
    }
    return parent::render($e);
    }
    }

    
    

    --------------------------------------------------------

     3.

    ---------------------------------------------------------

    <?php

    namespace appexceptions;

    use Throwable;

    /**
    * Class Exception
    * 异常基类
    * @package appexceptions
    */
    class Exception extends Exception
    {
    public function __construct(string $message = "", int $code = 0, Throwable $previous = null)
    {
    parent::__construct($message, $code, $previous);
    }
    }

    4.

     ------------------------------------

    <?php
    /**
    * Created by PhpStorm.
    * User: Administrator
    * Date: 2018/6/19
    * Time: 11:29
    */

    namespace app inty9apiexception;

    use appexceptionsException;

    class UserException extends Exception
    {
    // HTTP 状态码 404,200
    public $code = 400;

    // 错误具体信息
    public $msg = '参数错误';

    // 自定义的错误码
    public $errorCode = 10000;

    /**
    * UserException constructor.
    * @param $message
    * @param int $code
    * @param null $throwable
    */
    public function __construct($message, $code=0, $throwable=null)
    {
    if ( is_array($message)) {
    if (array_key_exists('code',$message)) {
    $this->code = $message['code'];
    }

    if (array_key_exists('msg',$message)) {
    $this->msg = $message['msg'];
    }

    if (array_key_exists('errorCode',$message)) {
    $this->errorCode = $message['errorCode'];
    }
    }

    parent::__construct($message, $code, $throwable);
    }

    }
    -------------------------------------------------

    5.

    ------------------------------------------------

    <?php
    /**
    * Created by PhpStorm.
    * User: Administrator
    * Date: 2018/6/20
    * Time: 10:37
    */

    namespace app inty9apiexception;

    use Throwable;

    class ParameterException extends UserException
    {
    public $code = 400;
    public $msg = '参数错误';
    public $errorCode = 10000;

    public function __construct(string $message = "", int $code = 0, Throwable $previous = null)
    {
    parent::__construct($message, $code, $previous);
    }

    /**
    * @param int $code
    */
    public function setCode(int $code)
    {
    $this->code = $code;
    }

    /**
    * @param string $msg
    */
    public function setMsg(string $msg)
    {
    $this->msg = $msg;
    }

    /**
    * @param int $errorCode
    */
    public function setErrorCode(int $errorCode)
    {
    $this->errorCode = $errorCode;
    }
    }
    -------------------------------

    
    
    

     ------------------------------------------------

  • 相关阅读:
    简单的实现UIpicker上面的取消确定按钮
    ios 笔记
    KVO 简单使用
    iOS 返回到根目录实现
    ios 实现简单的断点续传下载 nsurlconnection
    cocos2d 安装mac
    iOS 自定义自动锁屏时间
    PHP面向对象——单例模式
    PHP面向对象——构造函数、析构函数
    PHP面向对象——多态
  • 原文地址:https://www.cnblogs.com/vip-deng-vip/p/10136673.html
Copyright © 2011-2022 走看看