-
Notifications
You must be signed in to change notification settings - Fork 0
/
exception.py
169 lines (119 loc) · 5.32 KB
/
exception.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# exception.py
"""Defines Exceptions that can be raised by the blpapi library.
This file defines various exceptions that blpapi can raise.
"""
from builtins import Exception as _StandardException
from typing import Optional, Type
from . import internals
# pylint: disable=redefined-builtin
class Exception(_StandardException):
"""This class defines a base exception for blpapi operations.
Objects of this class contain the error description for the exception.
"""
def __init__(self, description: str, errorCode: Optional[int]) -> None:
"""Create a blpapi exception
Args:
description: Description of the error
errorCode: Code corresponding to the error
"""
_StandardException.__init__(self, description, errorCode)
def __str__(self) -> str:
args_arr = list(self.args)
return f"{args_arr[0]} ({args_arr[1]:#010x})"
class DuplicateCorrelationIdException(Exception):
"""Duplicate CorrelationId exception.
The class defines an exception for non unique :class:`CorrelationId`.
"""
class InvalidStateException(Exception):
"""Invalid state exception.
This class defines an exception for calling methods on an object that is
not in a valid state.
"""
class InvalidArgumentException(Exception):
"""Invalid argument exception.
This class defines an exception for invalid arguments on method
invocations.
"""
class InvalidConversionException(Exception):
"""Invalid conversion exception.
This class defines an exception for invalid conversion of data.
"""
class IndexOutOfRangeException(Exception):
"""Index out of range exception.
This class defines an exception to capture the error when an invalid index
is used for an operation that needs index.
"""
class NotFoundException(Exception):
"""Not found exception.
This class defines an exception to capture the error when an item is not
found for an operation.
"""
class FieldNotFoundException(Exception):
"""Field not found exception.
This class defines an exception to capture the error when an invalid field
is used for operation.
**DEPRECATED**
"""
class UnsupportedOperationException(Exception):
"""Unsupported operation exception.
This class defines an exception for unsupported operations.
"""
class UnknownErrorException(Exception):
"""Unknown error exception.
This class defines an exception for errors that do not fall in any
predefined category.
"""
class _ExceptionUtil:
"""Internal exception generating class."""
__errorClasses = {
internals.INVALIDSTATE_CLASS: InvalidStateException, # type: ignore
internals.INVALIDARG_CLASS: InvalidArgumentException, # type: ignore
internals.CNVERROR_CLASS: InvalidConversionException, # type: ignore
internals.BOUNDSERROR_CLASS: IndexOutOfRangeException, # type: ignore
internals.NOTFOUND_CLASS: NotFoundException, # type: ignore
internals.FLDNOTFOUND_CLASS: FieldNotFoundException, # type: ignore
internals.UNSUPPORTED_CLASS: UnsupportedOperationException, # type: ignore
}
@staticmethod
def __getErrorClass(errorCode: int) -> Type:
"""returns proper error class for the code"""
if errorCode == internals.ERROR_DUPLICATE_CORRELATIONID:
return DuplicateCorrelationIdException
errorClass = errorCode & 0xFF0000
return _ExceptionUtil.__errorClasses.get(
errorClass, UnknownErrorException
)
@staticmethod
def raiseException(errorCode: int, description: str = None) -> None:
"""Throw the appropriate exception for the specified 'errorCode'."""
if description is None:
description = internals.blpapi_getLastErrorDescription(errorCode)
if not description:
description = "Unknown"
errorClass = _ExceptionUtil.__getErrorClass(errorCode)
raise errorClass(description, errorCode)
@staticmethod
def raiseOnError(errorCode: int, description: str = None) -> None:
"""Throw the appropriate exception for the specified 'errorCode' if the
'errorCode != 0'.
"""
if errorCode:
_ExceptionUtil.raiseException(errorCode, description)
__copyright__ = """
Copyright 2012. Bloomberg Finance L.P.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions: The above
copyright notice and this permission notice shall be included in all copies
or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
"""