web/_exceptions.py
brancholdstable
changeset 8746 88c71ad83d47
parent 8312 6c2119509fac
child 9791 32047f5f08ba
child 9921 8227358aa983
equal deleted inserted replaced
8507:0c111b232927 8746:88c71ad83d47
    18 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
    18 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
    19 """exceptions used in the core of the CubicWeb web application"""
    19 """exceptions used in the core of the CubicWeb web application"""
    20 
    20 
    21 __docformat__ = "restructuredtext en"
    21 __docformat__ = "restructuredtext en"
    22 
    22 
       
    23 import httplib
       
    24 
    23 from cubicweb._exceptions import *
    25 from cubicweb._exceptions import *
    24 from cubicweb.utils import json_dumps
    26 from cubicweb.utils import json_dumps
       
    27 
       
    28 
       
    29 class DirectResponse(Exception):
       
    30     """Used to supply a twitted HTTP Response directly"""
       
    31     def __init__(self, response):
       
    32         self.response = response
       
    33 
       
    34 class InvalidSession(CubicWebException):
       
    35     """raised when a session id is found but associated session is not found or
       
    36     invalid"""
       
    37 
       
    38 # Publish related exception
    25 
    39 
    26 class PublishException(CubicWebException):
    40 class PublishException(CubicWebException):
    27     """base class for publishing related exception"""
    41     """base class for publishing related exception"""
    28 
    42 
       
    43     def __init__(self, *args, **kwargs):
       
    44         self.status = kwargs.pop('status', httplib.OK)
       
    45         super(PublishException, self).__init__(*args, **kwargs)
       
    46 
       
    47 class LogOut(PublishException):
       
    48     """raised to ask for deauthentication of a logged in user"""
       
    49     def __init__(self, url=None):
       
    50         super(LogOut, self).__init__()
       
    51         self.url = url
       
    52 
       
    53 class Redirect(PublishException):
       
    54     """raised to redirect the http request"""
       
    55     def __init__(self, location, status=httplib.SEE_OTHER):
       
    56         super(Redirect, self).__init__(status=status)
       
    57         self.location = location
       
    58 
       
    59 class StatusResponse(PublishException):
       
    60 
       
    61     def __init__(self, status, content=''):
       
    62         super(StatusResponse, self).__init__(status=status)
       
    63         self.content = content
       
    64 
       
    65     def __repr__(self):
       
    66         return '%s(%r, %r)' % (self.__class__.__name__, self.status, self.content)
       
    67         self.url = url
       
    68 
       
    69 # Publish related error
       
    70 
    29 class RequestError(PublishException):
    71 class RequestError(PublishException):
    30     """raised when a request can't be served because of a bad input"""
    72     """raised when a request can't be served because of a bad input"""
       
    73 
       
    74     def __init__(self, *args, **kwargs):
       
    75         kwargs.setdefault('status', httplib.BAD_REQUEST)
       
    76         super(RequestError, self).__init__(*args, **kwargs)
       
    77 
    31 
    78 
    32 class NothingToEdit(RequestError):
    79 class NothingToEdit(RequestError):
    33     """raised when an edit request doesn't specify any eid to edit"""
    80     """raised when an edit request doesn't specify any eid to edit"""
    34 
    81 
       
    82     def __init__(self, *args, **kwargs):
       
    83         kwargs.setdefault('status', httplib.BAD_REQUEST)
       
    84         super(NothingToEdit, self).__init__(*args, **kwargs)
       
    85 
    35 class ProcessFormError(RequestError):
    86 class ProcessFormError(RequestError):
    36     """raised when posted data can't be processed by the corresponding field
    87     """raised when posted data can't be processed by the corresponding field
    37     """
    88     """
       
    89     def __init__(self, *args, **kwargs):
       
    90         kwargs.setdefault('status', httplib.BAD_REQUEST)
       
    91         super(ProcessFormError, self).__init__(*args, **kwargs)
    38 
    92 
    39 class NotFound(RequestError):
    93 class NotFound(RequestError):
    40     """raised when a 404 error should be returned"""
    94     """raised when something was not found. In most case,
       
    95        a 404 error should be returned"""
    41 
    96 
    42 class Redirect(PublishException):
    97     def __init__(self, *args, **kwargs):
    43     """raised to redirect the http request"""
    98         kwargs.setdefault('status', httplib.NOT_FOUND)
    44     def __init__(self, location):
    99         super(NotFound, self).__init__(*args, **kwargs)
    45         self.location = location
       
    46 
       
    47 class DirectResponse(Exception):
       
    48     def __init__(self, response):
       
    49         self.response = response
       
    50 
       
    51 class StatusResponse(Exception):
       
    52     def __init__(self, status, content=''):
       
    53         self.status = int(status)
       
    54         self.content = content
       
    55 
       
    56     def __repr__(self):
       
    57         return '%s(%r, %r)' % (self.__class__.__name__, self.status, self.content)
       
    58 
       
    59 class InvalidSession(CubicWebException):
       
    60     """raised when a session id is found but associated session is not found or
       
    61     invalid
       
    62     """
       
    63 
   100 
    64 class RemoteCallFailed(RequestError):
   101 class RemoteCallFailed(RequestError):
    65     """raised when a json remote call fails
   102     """raised when a json remote call fails
    66     """
   103     """
    67     def __init__(self, reason=''):
   104     def __init__(self, reason='', status=httplib.INTERNAL_SERVER_ERROR):
    68         super(RemoteCallFailed, self).__init__()
   105         super(RemoteCallFailed, self).__init__(status=status)
    69         self.reason = reason
   106         self.reason = reason
    70 
   107 
    71     def dumps(self):
   108     def dumps(self):
    72         return json_dumps({'reason': self.reason})
   109         return json_dumps({'reason': self.reason})
    73 
       
    74 class LogOut(PublishException):
       
    75     """raised to ask for deauthentication of a logged in user"""
       
    76     def __init__(self, url):
       
    77         super(LogOut, self).__init__()
       
    78         self.url = url