cubicweb/_exceptions.py
changeset 12567 26744ad37953
parent 12508 a8c1ea390400
child 12607 3bf9002de48e
equal deleted inserted replaced
12566:6b3523f81f42 12567:26744ad37953
    19 
    19 
    20 
    20 
    21 
    21 
    22 from warnings import warn
    22 from warnings import warn
    23 
    23 
    24 from six import PY2, text_type
       
    25 
       
    26 from logilab.common.decorators import cachedproperty
    24 from logilab.common.decorators import cachedproperty
    27 
    25 
    28 from yams import ValidationError
    26 from yams import ValidationError
    29 
    27 
    30 # abstract exceptions #########################################################
    28 # abstract exceptions #########################################################
    31 
    29 
    32 class CubicWebException(Exception):
    30 class CubicWebException(Exception):
    33     """base class for cubicweb server exception"""
    31     """base class for cubicweb server exception"""
    34     msg = ""
    32     msg = ""
    35     def __unicode__(self):
    33 
       
    34     def __str__(self):
    36         if self.msg:
    35         if self.msg:
    37             if self.args:
    36             if self.args:
    38                 return self.msg % tuple(self.args)
    37                 return self.msg % tuple(self.args)
    39             else:
    38             else:
    40                 return self.msg
    39                 return self.msg
    41         else:
    40         else:
    42             return u' '.join(text_type(arg) for arg in self.args)
    41             return u' '.join(str(arg) for arg in self.args)
    43 
       
    44     def __str__(self):
       
    45         res = self.__unicode__()
       
    46         if PY2:
       
    47             res = res.encode('utf-8')
       
    48         return res
       
    49 
       
    50 
    42 
    51 class ConfigurationError(CubicWebException):
    43 class ConfigurationError(CubicWebException):
    52     """a misconfiguration error"""
    44     """a misconfiguration error"""
    53 
    45 
    54 class InternalError(CubicWebException):
    46 class InternalError(CubicWebException):
    96 
    88 
    97     @cachedproperty
    89     @cachedproperty
    98     def rtypes(self):
    90     def rtypes(self):
    99         if 'rtypes' in self.kwargs:
    91         if 'rtypes' in self.kwargs:
   100             return self.kwargs['rtypes']
    92             return self.kwargs['rtypes']
   101         cstrname = text_type(self.kwargs['cstrname'])
    93         cstrname = str(self.kwargs['cstrname'])
   102         cstr = self.session.find('CWUniqueTogetherConstraint', name=cstrname).one()
    94         cstr = self.session.find('CWUniqueTogetherConstraint', name=cstrname).one()
   103         return sorted(rtype.name for rtype in cstr.relations)
    95         return sorted(rtype.name for rtype in cstr.relations)
   104 
    96 
   105 
    97 
   106 class ViolatedConstraint(RepositoryError):
    98 class ViolatedConstraint(RepositoryError):
   117     """
   109     """
   118     msg = u'You are not allowed to perform this operation'
   110     msg = u'You are not allowed to perform this operation'
   119     msg1 = u'You are not allowed to perform %s operation on %s'
   111     msg1 = u'You are not allowed to perform %s operation on %s'
   120     var = None
   112     var = None
   121 
   113 
   122     def __unicode__(self):
   114     def __str__(self):
   123         try:
   115         try:
   124             if self.args and len(self.args) == 2:
   116             if self.args and len(self.args) == 2:
   125                 return self.msg1 % self.args
   117                 return self.msg1 % self.args
   126             if self.args:
   118             if self.args:
   127                 return u' '.join(self.args)
   119                 return u' '.join(self.args)
   128             return self.msg
   120             return self.msg
   129         except Exception as ex:
   121         except Exception as ex:
   130             return text_type(ex)
   122             return str(ex)
   131 
   123 
   132 class Forbidden(SecurityError):
   124 class Forbidden(SecurityError):
   133     """raised when a user tries to perform a forbidden action
   125     """raised when a user tries to perform a forbidden action
   134     """
   126     """
   135 
   127