"""
$URL: svn+ssh://sting.mems-exchange.org/repos/trunk/DurusWorks/durus/error.py $
$Id: error.py 34059 2011-07-01 02:21:01Z dbinger $
"""
from durus.utils import str_to_int8

class DurusError (Exception):
    """Durus error."""

class DurusKeyError (KeyError, DurusError):
    """Key not found in database."""

    def __str__(self):
        first_durus_id = self.durus_ids[0]
        return str(first_durus_id and str_to_int8(first_durus_id))


class ConflictError (DurusError):
    """
    There has been some kind of conflict involving the named durus_ids.
    """
    def __init__(self, durus_ids=None):
        self.durus_ids = durus_ids

    def __str__(self):
        if self.durus_ids is None:
            return "conflicting durus_ids not available"
        else:
            if len(self.durus_ids) > 1:
                s = "durus_ids=[%s ...]"
            else:
                s = "durus_ids=[%s]"
            first_durus_id = self.durus_ids[0]
            return s % (first_durus_id and str_to_int8(first_durus_id))


class WriteConflictError (ConflictError):
    """
    Two transactions tried to modify the same object at once.
    """

class ReadConflictError (ConflictError):
    """
    Conflict detected when object was loaded.
    An attempt was made to read an object that has changed in another
    transaction (eg. another process).
    """

class ProtocolError(DurusError):
    """
    An error occurred during communication between the storage server
    and the client.
    """
