Stupid mistake
authorMarek Marecki <triviuss@gmail.com>
Tue, 16 Apr 2013 19:04:04 +0000 (21:04 +0200)
committerMarek Marecki <triviuss@gmail.com>
Tue, 16 Apr 2013 19:04:04 +0000 (21:04 +0200)
diaspy/errors.py [new file with mode: 0644]

diff --git a/diaspy/errors.py b/diaspy/errors.py
new file mode 100644 (file)
index 0000000..e5a6167
--- /dev/null
@@ -0,0 +1,63 @@
+#!/usr/bin/env python3
+
+
+class DiaspyError(Exception):
+    """Base exception for all errors
+    raised by diaspy.
+    """
+    pass
+
+
+class LoginError(DiaspyError):
+    """Exception raised when something
+    bad happens while performing actions
+    related to logging in.
+    """
+    pass
+
+
+def react(r, message='', accepted=[200, 201, 202, 203, 204, 205, 206], exception=DiaspyError):
+    """This method tries to decides how to react
+    to a response code passed to it. If it's an
+    error code it will raise an exception (it will
+    call `throw()` method.
+
+    If response code is not accepted AND cannot
+    be matched to any exception, generic exception
+    (DiaspyError) is raised (provided that `exception`
+    param was left untouched).
+
+    By default `accepted` param contains all HTTP
+    success codes.
+
+    User can force type of exception to raise by passing
+    `exception` param.
+
+    :param r: response code
+    :type r: int
+    :param message: message for the exception
+    :type message: str
+    :param accepted: list of accepted error codes
+    :type accepted: list
+    :param exception: preferred exception to raise
+    :type exception: valid exception type (default: DiaspyError)
+    """
+    if r in accepted: e = None
+    else: e = DiaspyError
+
+    if e is not None: e = exception
+    throw(e, message=message)
+
+
+def throw(e, message=''):
+    """This function throws an error with given message.
+    If None is passed as `e` throw() will not raise
+    anything.
+
+    :param e: exception to throw
+    :type e: any valid exception type or None
+    :param message: message for exception
+    :type message: str
+    """
+    if e is None: pass
+    else: raise e(message)