Refactored `Aspect()` to use `repr()` on `Connection()`, refactored it
[diaspy.git] / diaspy / errors.py
CommitLineData
22422846
MM
1#!/usr/bin/env python3
2
3
4class DiaspyError(Exception):
5 """Base exception for all errors
6 raised by diaspy.
7 """
8 pass
9
10
11class LoginError(DiaspyError):
12 """Exception raised when something
13 bad happens while performing actions
14 related to logging in.
15 """
16 pass
17
18
65b1f099
MM
19class UserError(DiaspyError):
20 """Exception raised when something related to users goes wrong.
21 """
22 pass
23
24
7ebc8b0d
MM
25class ConversationError(DiaspyError):
26 """Exception raised when something related to conversations goes wrong.
27 """
28 pass
29
30
9896b779
MM
31class AspectError(DiaspyError):
32 """Exception raised when something related to aspects goes wrong.
33 """
34 pass
35
36
22422846
MM
37def react(r, message='', accepted=[200, 201, 202, 203, 204, 205, 206], exception=DiaspyError):
38 """This method tries to decides how to react
39 to a response code passed to it. If it's an
40 error code it will raise an exception (it will
41 call `throw()` method.
42
43 If response code is not accepted AND cannot
44 be matched to any exception, generic exception
45 (DiaspyError) is raised (provided that `exception`
46 param was left untouched).
47
48 By default `accepted` param contains all HTTP
49 success codes.
50
51 User can force type of exception to raise by passing
52 `exception` param.
53
54 :param r: response code
55 :type r: int
56 :param message: message for the exception
57 :type message: str
58 :param accepted: list of accepted error codes
59 :type accepted: list
60 :param exception: preferred exception to raise
61 :type exception: valid exception type (default: DiaspyError)
62 """
63 if r in accepted: e = None
64 else: e = DiaspyError
65
66 if e is not None: e = exception
67 throw(e, message=message)
68
69
70def throw(e, message=''):
71 """This function throws an error with given message.
72 If None is passed as `e` throw() will not raise
73 anything.
74
75 :param e: exception to throw
76 :type e: any valid exception type or None
77 :param message: message for exception
78 :type message: str
79 """
80 if e is None: pass
81 else: raise e(message)