"""quixote.errors

Exception classes used by Quixote
"""

class IgnoredException (Exception):
    """Handlers can raise this exception to bail out early; Quixote will
    ignore the exception and simply perform its usual processing.
    """
    
class AccessError (Exception):
    """Raised when a user's access to a given URL is denied.
    """

    def __init__ (self, msg=None):
        self.msg = msg or "access denied"
    
    def __str__ (self):
        return self.msg

    
class TraversalError (Exception):
    """Raised when a URL attempts to traverse to a bogus or non-
    existent Python namespace.  There are a number of reasons why this
    can happen, so these exceptions should have a clear and explicit
    message explaining the problem.  However, that can reveal valuable
    information to attackers, so the '__str__()' method here will hide
    that useful information if the configuration variable SECURE_ERRORS
    is true.
    """

    # XXX there should be some interaction between the response object
    # and this exception, eg. the ability to generate a pretty HTML
    # document (with or without traceback) and an appropriate HTTP
    # response code (most likely 404).

    # XXX hiding the exact error message is kind of pointless if
    # we show tracebacks, since a line of code like
    #   raise TraversalError("here's what's really going on", url)
    # might appear at the end of the traceback.  Thus, "secure errors"
    # aren't really secure unless tracebacks are also disabled.

    def __init__ (self, msg, url):
        self.msg = msg
        self.url = url

    def __str__ (self):
        from quixote import config
        if config.SECURE_ERRORS:
            return "not found: %s" % self.url
        else:
            return self.msg
