Commit a6bc2858 authored by Georgios D. Tsoukalas's avatar Georgios D. Tsoukalas
Browse files

fix CallError.from_dict() / auto-subclassing

parent 4f5d5e10
# Import general commission framework
from .exception import CallError, CorruptedError, InvalidDataError
from .exception import (CallError, CorruptedError, InvalidDataError,
register_exception, register_exceptions)
from .callpoint import Callpoint, get_callpoint, mkcallargs
......
class CallError(Exception):
exceptions = {}
def __new__(cls, *args, **kw):
call_error = kw.get('call_error', None)
......@@ -7,7 +8,7 @@ class CallError(Exception):
call_error = cls.__name__
else:
call_error = str(call_error)
cls = globals().get(call_error, cls)
cls = CallError.exceptions.get(call_error, cls)
self = Exception.__new__(cls, *args)
return self
......@@ -16,7 +17,7 @@ class CallError(Exception):
self.args = args
def __str__(self):
return '\n--------\n'.join((self.call_error + ':',) + self.args)
return '\n--------\n'.join(self.args)
def __repr__(self):
return '%s(%s)' % (self.__class__.__name__, ','.join(self.args))
......@@ -55,8 +56,24 @@ class CallError(Exception):
self = cls(*args, call_error=call_error)
return self
def register_exceptions(*exceptions):
for exception in exceptions:
if not issubclass(exception, CallError):
m = "Registering '%s': is not a CallError subclass" % (exception,)
raise ValueError(m)
CallError.exceptions[exception.__name__] = exception
def register_exception(exc):
register_exceptions(exc)
return exc
@register_exception
class CorruptedError(CallError):
pass
@register_exception
class InvalidDataError(CallError):
pass
register_exceptions(CorruptedError, InvalidDataError)
from commissioning import CallError
from commissioning import CallError, register_exception
@register_exception
class CommissionException(CallError):
pass
@register_exception
class InvalidKeyError(CommissionException):
pass
@register_exception
class NoEntityError(CommissionException):
pass
@register_exception
class NoQuantityError(CommissionException):
pass
@register_exception
class NoCapacityError(CommissionException):
pass
@register_exception
class ExportLimitError(CommissionException):
pass
@register_exception
class ImportLimitError(CommissionException):
pass
......@@ -18,6 +18,9 @@ except ImportError:
import unittest
from quotaholder.clients.kamaki import quotaholder_client
from quotaholder.api import (InvalidKeyError, NoEntityError,
NoQuantityError, NoCapacityError,
ExportLimitError, ImportLimitError)
import random
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment