exceptions.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. # -*- coding: utf-8 -*-
  2. """
  3. wechatbase.exceptions
  4. ~~~~~~~~~~~~~~~~~~~~
  5. Basic exceptions definition.
  6. :copyright: (c) 2014 by messense.
  7. :license: MIT, see LICENSE for more details.
  8. """
  9. from __future__ import absolute_import, unicode_literals
  10. import six
  11. from typing import Optional
  12. from library import to_text, to_binary
  13. class WeChatException(Exception):
  14. def __init__(self, errCode, errMsg, client = None,
  15. request = None, response = None):
  16. super(WeChatException, self).__init__(errMsg)
  17. self.errCode = errCode
  18. self.errMsg = errMsg
  19. self.client = client
  20. self.request = request
  21. self.response = response
  22. def __str__(self):
  23. if self.client:
  24. _repr = '{kclass}(client: {client}, errCode: {errCode}, errMsg: {errMsg})'.format(
  25. kclass = self.__class__.__name__,
  26. client = repr(self.client),
  27. errCode = self.errCode,
  28. errMsg = self.errMsg)
  29. else:
  30. _repr = '{kclass}(errCode: {errCode}, errMsg: {errMsg})'.format(
  31. kclass = self.__class__.__name__,
  32. errCode = self.errCode,
  33. errMsg = self.errMsg)
  34. if six.PY2:
  35. return to_binary(_repr)
  36. else:
  37. return to_text(_repr)
  38. def __repr__(self):
  39. return str(self)
  40. @property
  41. def tip(self):
  42. return '{}({})'.format(self.errMsg, self.errCode)
  43. class InvalidSignatureException(WeChatException):
  44. """Invalid signature exception class"""
  45. def __init__(self, errCode = -40001, errMsg = 'Invalid signature', client = None):
  46. super(InvalidSignatureException, self).__init__(
  47. errCode = errCode,
  48. errMsg = errMsg,
  49. client = client)
  50. class APILimitedException(WeChatException):
  51. """WeChat API call limited exception class"""
  52. pass
  53. class InvalidAppIdException(WeChatException):
  54. """Invalid app_id exception class"""
  55. def __init__(self, errCode = -40005, errMsg = 'Invalid AppId', client = None):
  56. super(InvalidAppIdException, self).__init__(
  57. errCode = errCode,
  58. errMsg = errMsg,
  59. client = client)
  60. class WeChatOAuthException(WeChatException):
  61. """WeChat OAuth API exception class"""
  62. pass
  63. class WechatOAuthDummyException(WeChatException):
  64. def __init__(self, errCode = -40006, errMsg = 'Dummy OAuth', client = None):
  65. super(WechatOAuthDummyException, self).__init__(
  66. errCode = errCode,
  67. errMsg = errMsg,
  68. client = client)
  69. class WeChatComponentOAuthException(WeChatException):
  70. """WeChat Component OAuth API exception class"""
  71. pass
  72. class WechatValidationError(WeChatException):
  73. def __init__(self, errmsg, lvalue, rvalue, client = None):
  74. # type: (basestring, basestring, basestring, Optional[object])->None
  75. _errMsg = '{}({} != {})'.format(errmsg, lvalue, rvalue)
  76. super(WechatValidationError, self).__init__(errCode = -102,
  77. errMsg = _errMsg,
  78. client = client)
  79. class WechatNetworkException(WeChatException):
  80. pass
  81. class WeChatPayException(WeChatException):
  82. """WeChat Pay API exception class"""
  83. pass