Skip to content
Snippets Groups Projects
Commit a04ccb79 authored by Enrico Zini's avatar Enrico Zini
Browse files

Added a file of common exceptions usable also in xmlrpc

parent 7b7d75ac
No related merge requests found
__all__ = ["HomeDirectoryExistsError"]
class HomeDirectoryExistsError(FileExistsError):
pass
......@@ -3,6 +3,7 @@ from __future__ import (absolute_import, print_function, division, unicode_liter
from json import dumps, loads
from six.moves.xmlrpc_client import ServerProxy
import octofuss
import octofuss.exceptions
class UnknownException(Exception):
def __init__(self, msg):
......@@ -16,13 +17,20 @@ class APIKeyException(Exception):
class ExceptionSerializer:
EXCEPTION_WHITELIST = [
Exception, UnknownException, LookupError, AttributeError, ValueError, KeyError, APIKeyException
Exception, UnknownException, LookupError, AttributeError, ValueError, KeyError, APIKeyException,
]
def __init__(self):
self.by_class = { c: c.__module__ + "." + c.__name__ for c in self.EXCEPTION_WHITELIST }
self.by_name = { c.__module__ + "." + c.__name__: c for c in self.EXCEPTION_WHITELIST }
# Add the exceptions from octofuss.exceptions
for name in octofuss.exceptions.__all__:
exc = getattr(octofuss.exceptions, name)
name = "octofuss.exceptions." + name
self.by_class[exc] = name
self.by_name[name] = exc
def exception_name(self, exc):
name = self.by_class.get(exc)
if name is not None:
......
import unittest
from octofuss.xmlrpc import ExceptionSerializer, APIKeyException, UnknownException
import octofuss.exceptions
class TestSerialize(unittest.TestCase):
def setUp(self):
......@@ -19,6 +20,9 @@ class TestSerialize(unittest.TestCase):
d = self.ser.to_dict(APIKeyException("test"))
self.assertEquals(d, {"name": "octofuss.xmlrpc.APIKeyException", "msg": "test"})
d = self.ser.to_dict(octofuss.exceptions.HomeDirectoryExistsError("test"))
self.assertEquals(d, {"name": "octofuss.exceptions.HomeDirectoryExistsError", "msg": "test"})
def test_unknown(self):
d = self.ser.to_dict(42)
self.assertEquals(d, {"name": "octofuss.xmlrpc.UnknownException", "msg": "42"})
......@@ -37,6 +41,10 @@ class TestDeserialize(unittest.TestCase):
self.assertEquals(e.__class__, APIKeyException)
self.assertEquals(e.args[0], "test")
e = self.ser.from_dict({"name": "octofuss.exceptions.HomeDirectoryExistsError", "msg": "test"})
self.assertEquals(e.__class__, octofuss.exceptions.HomeDirectoryExistsError)
self.assertEquals(e.args[0], "test")
def test_unknown(self):
e = self.ser.from_dict({"name": "octofuss.xmlrpc.UnknownException", "msg": "42"})
self.assertEquals(e.__class__, UnknownException)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment