iro/error.py
author Sandro Knauß <knauss@netzguerilla.net>
Fri, 30 Mar 2012 11:23:22 +0200
branchdevel
changeset 267 ef2df3f23cb1
parent 225 70dafbaf21af
child 294 0e75bd39767d
permissions -rw-r--r--
adding docstring: iro

# -*- coding: utf-8 -*-
class InterfaceException(Exception):
    """Exception, that should be reported to external client
    
    999 -- unknown error
    """
    def __init__(self, code=999, msg="Unknown error."):
        """
        :param integer code: the error code
        :param string msg: the error message
        """
        self.code=code
        self.msg=msg

    def dict(self):
        """dict representation of the error"""
        return {"code":self.code,
                "msg":self.msg,
                }

    def __str__(self):
        return "%i: %s"%(self.code,self.msg)

class UserNotFound(InterfaceException):
    """ 901 -- apikey is unknown -- user not found""" 
    def __init__(self):
        InterfaceException.__init__(self, 901, "Apikey is unknown.")

class JobNotFound(InterfaceException):
    """902 -- jobid is unknown"""
    def __init__(self):
        InterfaceException.__init__(self, 902, "Jobid is unknown.")

class ExternalException(InterfaceException):
    """950 -- error in external api"""
    def __init__(self):
        InterfaceException.__init__(self, 950, "Error in external API.")

class ValidateException(Exception):
    """700 -- validation failed."""
    def __init__(self, code=700, field=None, msg=None):
        """
        :param integer code: the error code
        :param string field: the field, that is not valid 
        :param string msg: the error message
        """
        self.code=code
        self.field=field
        self.msg = msg
        if not msg:
            self.msg='Validation failed.'
        if field and not msg:
            self.msg="Validation of '%s' failed."%field

    def dict(self):
        """dict representation of the error"""
        return {"code":self.code,
                "msg":self.msg,
                "field":self.field,
                }
    def __str__(self):
        return "%i: %s"%(self.code,self.msg)

class InvalidTel(ValidateException):
    """701 -- invalid telnumber"""
    def __init__(self, number,field=None):
        self.number = number
        msg = "No valid telnumber: '%s'"%(number)
        ValidateException.__init__(self, 701, field, msg)
    
class InvalidMail(ValidateException):
    """702 -- invalid mailaddress"""
    def __init__(self, number,field=None):
        self.number = number
        msg = "No valid email: '%s'"%(number)
        ValidateException.__init__(self, 702, field, msg)

class OfferException(Exception):
    """an Exception in Offer handling"""
    def __init__(self, name):
        self.name = name

class NoRoute(OfferException):
    """no valid route found"""
    def __str__(self):
        return "Not a valid route: %s"%self.name

class NoProvider(OfferException):
    """no provider found"""
    def __str__(self):
        return "Not a valid provider: %s"%self.name

class NoTyp(OfferException):
    """no typ found."""
    def __str__(self):
        return "Not a valid Typ: %s"%self.name

class RejectRecipient(Exception):
    """can't handle the recipient in a route"""
    def __init__(self,recipient, status=None):
        self.recipient = recipient
        self.status = status

    def __str__(self):
        return "Reject recipient(%s): %s"%(str(self.recipient),str(self.status))

class ConfigException(Exception):
    """Exception while loading configuration."""
    def __init__(self,section, name):
        self.section = section
        self.name = name

class UnknownOption(ConfigException):
    """Option is unknown"""
    def __str__(self):
        return "Unknown option '%s' in section '%s'."%(self.name, self.section)

class NeededOption(ConfigException):
    """Option is missing, but needed."""
    def __str__(self):
        return "Option '%s' in section '%s' is missing."%(self.name, self.section)

class NoRouteForTask(Exception):
    """Can't send message to recipient with given offers"""
    pass