PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/trytond/error.py

https://bitbucket.org/sharoonthomas/trytond
Python | 108 lines | 103 code | 3 blank | 2 comment | 0 complexity | d237d668f5d8439c8789fbc6154424dd MD5 | raw file
Possible License(s): GPL-3.0, AGPL-1.0
  1. #This file is part of Tryton. The COPYRIGHT file at the top level of
  2. #this repository contains the full copyright notices and license terms.
  3. from trytond.transaction import Transaction
  4. class WarningErrorMixin(object):
  5. def raise_user_error(self, error, error_args=None,
  6. error_description='', error_description_args=None,
  7. raise_exception=True):
  8. '''
  9. Raise an exception that will be displayed as an error message
  10. in the client.
  11. :param error: the key of the dictionary _error_messages used
  12. for error message
  13. :param error_args: the arguments that will be used
  14. for "%"-based substitution
  15. :param error_description: the key of the dictionary
  16. _error_messages used for error description
  17. :param error_description_args: the arguments that will be used
  18. for "%"-based substitution
  19. :param raise_exception: if set to False return the error string
  20. (or tuple if error_description is not empty) instead of raising an
  21. exception.
  22. '''
  23. translation_obj = self.pool.get('ir.translation')
  24. error = self._error_messages.get(error, error)
  25. language = Transaction().context.get('language') or 'en_US'
  26. res = translation_obj._get_source(self._name, 'error', language, error)
  27. if not res:
  28. res = translation_obj._get_source(error, 'error', language)
  29. if not res:
  30. res = translation_obj._get_source(error, 'error', 'en_US')
  31. if res:
  32. error = res
  33. if error_args:
  34. try:
  35. error = error % error_args
  36. except TypeError:
  37. pass
  38. if error_description:
  39. error_description = self._error_messages.get(error_description,
  40. error_description)
  41. res = translation_obj._get_source(self._name, 'error', language,
  42. error_description)
  43. if not res:
  44. res = translation_obj._get_source(error_description, 'error',
  45. language)
  46. if not res:
  47. res = translation_obj._get_source(error_description, 'error',
  48. 'en_US')
  49. if res:
  50. error_description = res
  51. if error_description_args:
  52. try:
  53. error_description = error_description % \
  54. error_description_args
  55. except TypeError:
  56. pass
  57. if raise_exception:
  58. raise Exception('UserError', error, error_description)
  59. else:
  60. return (error, error_description)
  61. if raise_exception:
  62. raise Exception('UserError', error)
  63. else:
  64. return error
  65. def raise_user_warning(self, warning_name, warning,
  66. warning_args=None, warning_description='',
  67. warning_description_args=None):
  68. '''
  69. Raise an exception that will be displayed as a warning message
  70. in the client, if the user has not yet bypassed it.
  71. :param warning_name: the unique warning name
  72. :param warning: the key of the dictionary _error_messages used
  73. for warning message
  74. :param warning_args: the arguments that will be used for
  75. "%"-based substitution
  76. :param warning_description: the key of the dictionary
  77. _error_messages used for warning description
  78. :param warning_description_args: the arguments that will be used
  79. for "%"-based substitution
  80. '''
  81. warning_obj = self.pool.get('res.user.warning')
  82. if warning_obj.check(warning_name):
  83. if warning_description:
  84. warning, warning_description = self.raise_user_error(warning,
  85. error_args=warning_args,
  86. error_description=warning_description,
  87. error_description_args=warning_description_args,
  88. raise_exception=False)
  89. raise Exception('UserWarning', warning_name, warning,
  90. warning_description)
  91. else:
  92. warning = self.raise_user_error(warning,
  93. error_args=warning_args, raise_exception=False)
  94. raise Exception('UserWarning', warning_name, warning)