iro/error.py
author Sandro Knauß <knauss@netzguerilla.net>
Fri, 24 Aug 2012 01:05:06 +0200
branchdevel
changeset 294 0e75bd39767d
parent 267 ef2df3f23cb1
permissions -rw-r--r--
adding LICENSE to all files

# Copyright (c) 2012 netzguerilla.net <iro@netzguerilla.net>
# 
# This file is part of Iro.
# 
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
# #Software, and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
# PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

# -*- 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