exceptions.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. from Tea.exceptions import UnretryableException as TeaUnretryableException
  2. from Tea.exceptions import RequiredArgumentException as TeaRequiredArgumentException
  3. from Tea.exceptions import TeaException
  4. from darabonba.policy.retry import RetryPolicyContext
  5. from typing import Any, Optional
  6. class DaraException(TeaException):
  7. def __init__(self, dic):
  8. super().__init__(dic)
  9. self.code = dic.get("code")
  10. self.detail = dic.get("detail")
  11. self.message = dic.get("message")
  12. self.data = dic.get("data")
  13. self.description = dic.get("description")
  14. self.accessDeniedDetail = dic.get("accessDeniedDetail")
  15. if isinstance(dic.get("data"), dict) and dic.get("data").get("statusCode") is not None:
  16. self.statusCode = dic.get("data").get("statusCode")
  17. self.name = 'DaraException'
  18. def __str__(self):
  19. return f'Error: {self.code} {self.message} Response: {self.data}'
  20. class ResponseException(DaraException):
  21. def __init__(self,
  22. code: Optional[str] = None,
  23. message: Optional[str] = None,
  24. status_code: Optional[int] = None,
  25. retry_after: Optional[int] = None,
  26. data: Optional[dict] = None,
  27. access_denied_detail: Optional[dict] = None,
  28. description: Optional[str] = None,
  29. detail: Optional[str] = None,
  30. stack: Optional[str] = None):
  31. if data and status_code is not None:
  32. data['statusCode'] = status_code
  33. super().__init__({
  34. 'code': code,
  35. 'message': message,
  36. 'data': data,
  37. 'description': description,
  38. 'detail': detail,
  39. 'accessDeniedDetail': access_denied_detail
  40. })
  41. self.name = 'ResponseException'
  42. self.status_code = status_code
  43. self.retry_after = retry_after
  44. self.stack = stack
  45. class ValidateException(Exception):
  46. pass
  47. class RequiredArgumentException(TeaRequiredArgumentException):
  48. def __init__(self, arg):
  49. self.arg = arg
  50. def __str__(self):
  51. return f'"{self.arg}" is required.'
  52. class RetryError(Exception):
  53. def __init__(self, message):
  54. super().__init__({"message":message})
  55. self.message = message
  56. self.data = None
  57. self.name = 'RetryError'
  58. class UnretryableException(TeaUnretryableException):
  59. def __init__(
  60. self,
  61. _context: RetryPolicyContext
  62. ):
  63. if isinstance(_context.exception, ResponseException):
  64. raise _context.exception
  65. super().__init__(
  66. request= _context.http_request,
  67. ex= _context.exception,
  68. )
  69. self.name = 'UnretryableException'
  70. def __str__(self):
  71. return str(self.inner_exception)