-
Notifications
You must be signed in to change notification settings - Fork 2
/
exceptions.py
executable file
·161 lines (126 loc) · 4.58 KB
/
exceptions.py
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# -*- coding: utf-8 -*-
"""
wechatpy_tornado.exceptions
~~~~~~~~~~~~~~~~~~~~
Basic exceptions definition.
:copyright: (c) 2014 by messense.
:license: MIT, see LICENSE for more details.
"""
from __future__ import absolute_import, unicode_literals
import six
from wechatpy_tornado.utils import to_binary, to_text
class WeChatException(Exception):
"""Base exception for wechatpy_tornado"""
def __init__(self, errcode, errmsg):
"""
:param errcode: Error code
:param errmsg: Error message
"""
self.errcode = errcode
self.errmsg = errmsg
def __str__(self):
_repr = 'Error code: {code}, message: {msg}'.format(
code=self.errcode,
msg=self.errmsg
)
if six.PY2:
return to_binary(_repr)
else:
return to_text(_repr)
def __repr__(self):
_repr = '{klass}({code}, {msg})'.format(
klass=self.__class__.__name__,
code=self.errcode,
msg=self.errmsg
)
if six.PY2:
return to_binary(_repr)
else:
return to_text(_repr)
class WeChatClientException(WeChatException):
"""WeChat API client exception class"""
def __init__(self, errcode, errmsg, client=None,
request=None, response=None):
super(WeChatClientException, self).__init__(errcode, errmsg)
self.client = client
self.request = request
self.response = response
class InvalidSignatureException(WeChatException):
"""Invalid signature exception class"""
def __init__(self, errcode=-40001, errmsg='Invalid signature'):
super(InvalidSignatureException, self).__init__(errcode, errmsg)
class APILimitedException(WeChatClientException):
"""WeChat API call limited exception class"""
pass
class InvalidAppIdException(WeChatException):
"""Invalid app_id exception class"""
def __init__(self, errcode=-40005, errmsg='Invalid AppId'):
super(InvalidAppIdException, self).__init__(errcode, errmsg)
class InvalidMchIdException(WeChatException):
"""Invalid mch_id exception class"""
def __init__(self, errcode=-40006, errmsg='Invalid MchId'):
super(InvalidMchIdException, self).__init__(errcode, errmsg)
class WeChatOAuthException(WeChatClientException):
"""WeChat OAuth API exception class"""
pass
class WeChatComponentOAuthException(WeChatClientException):
"""WeChat Component OAuth API exception class"""
pass
class WeChatPayException(WeChatClientException):
"""WeChat Pay API exception class"""
def __init__(self, return_code, result_code=None, return_msg=None,
errcode=None, errmsg=None, client=None,
request=None, response=None):
"""
:param return_code: 返回状态码
:param result_code: 业务结果
:param return_msg: 返回信息
:param errcode: 错误代码
:param errmsg: 错误代码描述
"""
super(WeChatPayException, self).__init__(
errcode,
errmsg,
client,
request,
response
)
self.return_code = return_code
self.result_code = result_code
self.return_msg = return_msg
def __str__(self):
_str = 'Error code: {code}, message: {msg}. Pay Error code: {pay_code}, message: {pay_msg}'.format(
code=self.return_code,
msg=self.return_msg,
pay_code=self.errcode,
pay_msg=self.errmsg
)
if six.PY2:
return to_binary(_str)
else:
return to_text(_str)
def __repr__(self):
_repr = '{klass}({code}, {msg}). Pay({pay_code}, {pay_msg})'.format(
klass=self.__class__.__name__,
code=self.return_code,
msg=self.return_msg,
pay_code=self.errcode,
pay_msg=self.errmsg
)
if six.PY2:
return to_binary(_repr)
else:
return to_text(_repr)
class RequestException(IOError):
"""There was an ambiguous exception that occurred while handling your
request.
"""
def __init__(self, *args, **kwargs):
"""Initialize RequestException with `request` and `response` objects."""
response = kwargs.pop('response', None)
self.response = response
self.request = kwargs.pop('request', None)
if (response is not None and not self.request and
hasattr(response, 'request')):
self.request = self.response.request
super(RequestException, self).__init__(*args, **kwargs)