iro/error.py
changeset 302 3f4bdea2abbf
parent 294 0e75bd39767d
equal deleted inserted replaced
90:eb04ac3a8327 302:3f4bdea2abbf
       
     1 # Copyright (c) 2012 netzguerilla.net <iro@netzguerilla.net>
       
     2 # 
       
     3 # This file is part of Iro.
       
     4 # 
       
     5 # Permission is hereby granted, free of charge, to any person obtaining a copy of
       
     6 # this software and associated documentation files (the "Software"), to deal in
       
     7 # the Software without restriction, including without limitation the rights to use,
       
     8 # copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
       
     9 # #Software, and to permit persons to whom the Software is furnished to do so,
       
    10 # subject to the following conditions:
       
    11 # 
       
    12 # The above copyright notice and this permission notice shall be included in
       
    13 # all copies or substantial portions of the Software.
       
    14 # 
       
    15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
       
    16 # INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
       
    17 # PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
       
    18 # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
       
    19 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
       
    20 # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
       
    21 
       
    22 # -*- coding: utf-8 -*-
       
    23 class InterfaceException(Exception):
       
    24     """Exception, that should be reported to external client
       
    25     
       
    26     999 -- unknown error
       
    27     """
       
    28     def __init__(self, code=999, msg="Unknown error."):
       
    29         """
       
    30         :param integer code: the error code
       
    31         :param string msg: the error message
       
    32         """
       
    33         self.code=code
       
    34         self.msg=msg
       
    35 
       
    36     def dict(self):
       
    37         """dict representation of the error"""
       
    38         return {"code":self.code,
       
    39                 "msg":self.msg,
       
    40                 }
       
    41 
       
    42     def __str__(self):
       
    43         return "%i: %s"%(self.code,self.msg)
       
    44 
       
    45 class UserNotFound(InterfaceException):
       
    46     """ 901 -- apikey is unknown -- user not found""" 
       
    47     def __init__(self):
       
    48         InterfaceException.__init__(self, 901, "Apikey is unknown.")
       
    49 
       
    50 class JobNotFound(InterfaceException):
       
    51     """902 -- jobid is unknown"""
       
    52     def __init__(self):
       
    53         InterfaceException.__init__(self, 902, "Jobid is unknown.")
       
    54 
       
    55 class ExternalException(InterfaceException):
       
    56     """950 -- error in external api"""
       
    57     def __init__(self):
       
    58         InterfaceException.__init__(self, 950, "Error in external API.")
       
    59 
       
    60 class ValidateException(Exception):
       
    61     """700 -- validation failed."""
       
    62     def __init__(self, code=700, field=None, msg=None):
       
    63         """
       
    64         :param integer code: the error code
       
    65         :param string field: the field, that is not valid 
       
    66         :param string msg: the error message
       
    67         """
       
    68         self.code=code
       
    69         self.field=field
       
    70         self.msg = msg
       
    71         if not msg:
       
    72             self.msg='Validation failed.'
       
    73         if field and not msg:
       
    74             self.msg="Validation of '%s' failed."%field
       
    75 
       
    76     def dict(self):
       
    77         """dict representation of the error"""
       
    78         return {"code":self.code,
       
    79                 "msg":self.msg,
       
    80                 "field":self.field,
       
    81                 }
       
    82     def __str__(self):
       
    83         return "%i: %s"%(self.code,self.msg)
       
    84 
       
    85 class InvalidTel(ValidateException):
       
    86     """701 -- invalid telnumber"""
       
    87     def __init__(self, number,field=None):
       
    88         self.number = number
       
    89         msg = "No valid telnumber: '%s'"%(number)
       
    90         ValidateException.__init__(self, 701, field, msg)
       
    91     
       
    92 class InvalidMail(ValidateException):
       
    93     """702 -- invalid mailaddress"""
       
    94     def __init__(self, number,field=None):
       
    95         self.number = number
       
    96         msg = "No valid email: '%s'"%(number)
       
    97         ValidateException.__init__(self, 702, field, msg)
       
    98 
       
    99 class OfferException(Exception):
       
   100     """an Exception in Offer handling"""
       
   101     def __init__(self, name):
       
   102         self.name = name
       
   103 
       
   104 class NoRoute(OfferException):
       
   105     """no valid route found"""
       
   106     def __str__(self):
       
   107         return "Not a valid route: %s"%self.name
       
   108 
       
   109 class NoProvider(OfferException):
       
   110     """no provider found"""
       
   111     def __str__(self):
       
   112         return "Not a valid provider: %s"%self.name
       
   113 
       
   114 class NoTyp(OfferException):
       
   115     """no typ found."""
       
   116     def __str__(self):
       
   117         return "Not a valid Typ: %s"%self.name
       
   118 
       
   119 class RejectRecipient(Exception):
       
   120     """can't handle the recipient in a route"""
       
   121     def __init__(self,recipient, status=None):
       
   122         self.recipient = recipient
       
   123         self.status = status
       
   124 
       
   125     def __str__(self):
       
   126         return "Reject recipient(%s): %s"%(str(self.recipient),str(self.status))
       
   127 
       
   128 class ConfigException(Exception):
       
   129     """Exception while loading configuration."""
       
   130     def __init__(self,section, name):
       
   131         self.section = section
       
   132         self.name = name
       
   133 
       
   134 class UnknownOption(ConfigException):
       
   135     """Option is unknown"""
       
   136     def __str__(self):
       
   137         return "Unknown option '%s' in section '%s'."%(self.name, self.section)
       
   138 
       
   139 class NeededOption(ConfigException):
       
   140     """Option is missing, but needed."""
       
   141     def __str__(self):
       
   142         return "Option '%s' in section '%s' is missing."%(self.name, self.section)
       
   143 
       
   144 class NoRouteForTask(Exception):
       
   145     """Can't send message to recipient with given offers"""
       
   146     pass