Exception.class.php
2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
/**
* Exception.class.php
* 异常
*
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
* @copyright Copyright (c) 2014 - ? VcySDK (http://www.vchangyi.com/)
* @author zhuxun37
* @version 1.0.0
*/
namespace VcySDK;
class Exception extends \Exception
{
/**
* 请求ID
*
* @type string
*/
protected $_request_id = '';
/**
* SDK 错误码
*
* @type string
*/
protected $_sdk_code = '';
/**
* SDK response data
*
* @type string
*/
protected $_sdk_data = '';
/**
* 异常构造方法
*
* @param array|string $message 异常详情
* @param string $code 异常错误号
* @param array $data 异常错误数据
* @param string $previous 前一个异常
*/
public function __construct($message = null, $code = null, $data = [], $previous = null)
{
if (is_array($message)) {
$this->_request_id = $message['requestId'];
$code = $message['code'];
$message = $message['msg'];
$data = $message['data'];
}
// 记录日志
Logger::write(sprintf('message: %s | code: %s | requestId: %s', $message, $code, $this->_request_id));
// 判断是否有错误编号
if (preg_match("/^(\s*\d+\s*):/", $message)) {
$pos = stripos($message, ':');
$ncode = substr($message, 0, $pos);
$message = substr($message, $pos + 1);
// 如果错误号为空, 则取详情中得编号
if (empty($code)) {
$code = $ncode;
}
}
$iCode = (int)$code;
parent::__construct($message, $iCode, $previous);
$this->_sdk_code = $code;
$this->_sdk_data = $data;
}
/**
* 获取请求ID
*
* @return string
*/
public function getRequestID()
{
return $this->_request_id;
}
/**
* 获取SDK错误码
* @return string
*/
public function getSdkCode()
{
return $this->_sdk_code;
}
/**
* 获取SDK Data
* @return string
*/
public function getSdkData()
{
return $this->_sdk_data;
}
}