Debian Codegraph @

odoorpc/error.py

raw
1 # -*- coding: utf-8 -*-
2 # Copyright 2014 Sébastien Alix
3 # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl)
4 """This module contains all exceptions raised by `OdooRPC` when an error
5 occurred.
6 """
7 import sys
8
9
10 class Error(Exception):
11 """Base class for exception."""
12
13 pass
14
15
16 class RPCError(Error):
17 """Exception raised for errors related to RPC queries.
18 Error details (like the `Odoo` server traceback) are available through the
19 `info` attribute:
20
21 .. doctest::
22 :options: +SKIP
23
24 >>> from pprint import pprint as pp
25 >>> try:
26 ... odoo.execute('res.users', 'wrong_method')
27 ... except odoorpc.error.RPCError as exc:
28 ... pp(exc.info)
29 ...
30 {'code': 200,
31 'data': {'arguments': ["type object 'res.users' has no attribute 'wrong_method'"],
32 'debug': 'Traceback (most recent call last):\\n File ...',
33 'exception_type': 'internal_error',
34 'message': "'res.users' object has no attribute 'wrong_method'",
35 'name': 'exceptions.AttributeError'}
36 'message': 'Odoo Server Error'}
37
38 .. doctest::
39 :hide:
40
41 >>> from pprint import pprint as pp
42 >>> try:
43 ... odoo.execute('res.users', 'wrong_method')
44 ... except odoorpc.error.RPCError as exc:
45 ... exc.info['code'] == 200
46 ... 'message' in exc.info
47 ... exc.info['data']['arguments'] in [
48 ... ["'res.users' object has no attribute 'wrong_method'"], # >= 8.0
49 ... ["type object 'res.users' has no attribute 'wrong_method'"], # >= 10.0
50 ... ]
51 ... exc.info['data']['debug'].startswith('Traceback (most recent call last):\\n File')
52 ... exc.info['data']['message'] in [
53 ... "'res.users' object has no attribute 'wrong_method'", # >= 8.0
54 ... "type object 'res.users' has no attribute 'wrong_method'", # >= 10.0
55 ... ]
56 ... exc.info['data']['name'] in [
57 ... 'exceptions.AttributeError',
58 ... 'builtins.AttributeError',
59 ... ]
60 ...
61 True
62 True
63 True
64 True
65 True
66 True
67 """
68
69 def __init__(self, message, info=False):
70 # Ensure that the message is in unicode,
71 # to be compatible both with Python2 and 3
72 try:
73 message = message.decode('utf-8')
74 except (UnicodeEncodeError, AttributeError):
75 pass
76 super(Error, self).__init__(message, info)
77 self.info = info
78
79 def __str__(self):
80 # args[0] should always be a unicode object (see '__init__(...)')
81 if sys.version_info[0] < 3 and self.args and self.args[0]:
82 return self.args[0].encode('utf-8')
83 return self.args and self.args[0] or ''
84
85 def __unicode__(self):
86 # args[0] should always be a unicode object (see '__init__(...)')
87 return self.args and self.args[0] or u''
88
89 def __repr__(self):
90 return "{}({})".format(self.__class__.__name__, repr(self.args[0]))
91
92
93 class InternalError(Error):
94 """Exception raised for errors occurring during an internal operation."""
95
96 pass