exceptions.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. # -*- coding: utf-8 -*-
  2. # !/usr/bin/env python
  3. from apps.web.constant import ErrorCode
  4. class ApiException(Exception):
  5. def __init__(self, errcode, errmsg, payload = {}):
  6. self.errcode = errcode
  7. self.payload = payload
  8. self.errmsg = errmsg
  9. def __repr__(self):
  10. return 'api exception. errcode = {}, errmsg = {}, payload = {}'.format(
  11. self.errcode, self.errmsg, str(self.payload))
  12. def __str__(self):
  13. return self.__repr__()
  14. def to_response(self):
  15. from apps.web.api.utils import api_error_response
  16. return api_error_response(errcode = self.errcode, errmsg = self.errmsg, payload = self.payload)
  17. class ApiParameterError(ApiException):
  18. def __init__(self, errmsg):
  19. super(ApiParameterError, self).__init__(
  20. errcode = ErrorCode.API_PARAMETER_ERROR, errmsg = errmsg)
  21. class ApiAuthException(ApiException):
  22. def __init__(self, errmsg):
  23. super(ApiAuthException, self).__init__(
  24. errcode = ErrorCode.API_AUTH_ERROR, errmsg = errmsg, payload = {'auth_result': 1})
  25. class ApiAuthDeviceException(ApiException):
  26. def __init__(self):
  27. super(ApiAuthDeviceException, self).__init__(
  28. errcode = ErrorCode.API_AUTH_DEVICE_ERROR, errmsg = u'无权操作设备', payload = {'auth_result': 3})
  29. class ApiNoDeviceException(ApiException):
  30. def __init__(self):
  31. super(ApiNoDeviceException, self).__init__(
  32. errcode = ErrorCode.API_NO_DEVICE, errmsg = u'设备不存在')
  33. class ApiDeviceModeException(ApiException):
  34. def __init__(self):
  35. super(ApiDeviceModeException, self).__init__(
  36. errcode = ErrorCode.API_NO_DEVICE, errmsg = u'当前设备不在API模式, 请切换API模式(经销商后台->API接口管理->API设备管理)')
  37. class ApiDeviceTypeError(ApiException):
  38. def __init__(self, errmsg):
  39. super(ApiDeviceTypeError, self).__init__(
  40. errcode = ErrorCode.API_ERROR_DEVICE_TYPE, errmsg = errmsg)