12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- <?php namespace App\Exceptions\Api;
- use Exception;
- use Throwable;
- /**
- * 接口异常
- */
- class ApiException extends Exception
- {
- // 错误返回码
- protected $errCode = 'error';
- // 错误返回数据
- protected $errData = '';
- // 本地化前缀
- protected $langkey = 'message';
-
- /**
- * 抛出错误信息
- *
- * @param String $message — [optional] The Exception message to throw.
- * @param Mixed $errData — 错误返回数据
- * @param Int $code — [optional] The Exception code.
- * @param Throwable $previous — [optional] The previous throwable used for the exception chaining.
- * @return Mixed
- *
- */
- public function __construct( $message="",$errData='',$code=0,?Throwable $previous=null )
- {
- // 如果本地化前缀存在则本地化处理
- if( $this->langkey ) {
- // 消息key
- $msgKey = $this->langkey.'.'.$message;
- // 提示信息本地化
- $trans = trans($msgKey);
- // 如果没有找到对应的提示,使用原提示信息
- $message = ($trans == $msgKey) ? $message : $trans;
- }
- // 传递数据到父级
- parent::__construct($message, $code, $previous);
- // 错误信息说
- if( $errData ) $this->errData = $errData;
- }
- /**
- * 获取错误返回码
- *
- */
- public function getErrCode()
- {
- return $this->errCode;
- }
- /**
- * 获取返回数据
- *
- */
- public function getErrData()
- {
- return $this->errData;
- }
- }
|