pumpwood_communication.exceptions

Define PumpWood exceptions to be treated as API errors.

Define especific errors for PumpWood plataform. These errors will be treated and will not result in default 500 errors

  1"""
  2Define PumpWood exceptions to be treated as API errors.
  3
  4Define especific errors for PumpWood plataform. These errors will be treated
  5and will not result in default 500 errors
  6"""
  7
  8
  9class PumpWoodException(Exception): # NOQA
 10    """Special exception used in Pumpowod Systems.
 11
 12    It permits treatment of raises on applications serializing response
 13    using to_dict function and return status code as `status_code`
 14    attribute value.
 15    """
 16
 17    status_code: int = 400
 18    """PumpWoodException will return status 400 on Pumpwood backend."""
 19
 20    message: str
 21    """Message associated with raise."""
 22
 23    payload: dict
 24    """
 25    Dictionary payload that will be returned by to_dict funcion and format
 26    message string.
 27    """
 28
 29    def __repr__(self):
 30        """@private."""
 31        message_fmt = self.format_message()
 32        template = "{class_name}[status_code={status_code}]: " + \
 33            "{message_fmt}\nerror payload={payload}"
 34        return template.format(
 35            class_name=self.__class__.__name__,
 36            status_code=self.status_code,
 37            message_fmt=message_fmt,
 38            payload=self.payload)
 39
 40    def __str__(self):
 41        """@private."""
 42        message_fmt = self.format_message()
 43        template = "{class_name}[status_code={status_code}]: " + \
 44            "{message_fmt}\nerror payload={payload}"
 45        return template.format(
 46            class_name=self.__class__.__name__,
 47            status_code=self.status_code,
 48            message_fmt=message_fmt,
 49            payload=self.payload)
 50
 51    def __init__(self, message: str, payload: dict = {}, status_code=None):
 52        """__init__.
 53
 54        Args:
 55            message:
 56                Message that will be formated using payload
 57                information using `{key}` to replace information.
 58            payload:
 59                Payload data passed as a dictionary, it will be returned
 60                in payload at `to_dict` funcion and used to format message.
 61            status_code:
 62                Change the default status code of the exception.
 63        """
 64        Exception.__init__(self)
 65        self.message = message
 66        if status_code is not None:
 67            self.status_code = status_code
 68        self.payload = payload
 69
 70    def format_message(self) -> str:
 71        """Format exception message using payload data.
 72
 73        Substitute placeholders at exception message with payload.
 74
 75        Returns:
 76            Return a string of message with placeholders substituted with
 77            payload data.
 78        """
 79        try:
 80            return self.message.format(**self.payload)
 81        except Exception:
 82            return self.message + "\n** format error **"
 83
 84    def to_dict(self) -> dict:
 85        """Serialize Exception object to return as reponse.
 86
 87        Returns:
 88            Return a dictionary with keys:
 89            - **payload [dict]:** Payload associated with raise.
 90            - **type [str]:** Return name of the class of the Exception.
 91            - **message_not_fmt [str]:** Return msg without replacemnt of the
 92                msg with payload information.
 93            - **message [str]:** Return msg formated with payload information.
 94        """
 95        message_fmt = self.format_message()
 96        rv = {
 97            "payload": self.payload,
 98            "type": self.__class__.__name__,
 99            "message_not_fmt": self.message,
100            "message": message_fmt}
101        return rv
102
103
104class PumpWoodDataLoadingException(PumpWoodException):
105    """Problem when loading data at dataloaders and to_load models."""
106
107    pass
108
109
110class PumpWoodDatabaseError(PumpWoodException):
111    """Errors raised by Postgres and not treated by other handlers."""
112
113    pass
114
115
116class PumpWoodDataTransformationException(PumpWoodException):
117    """Problem when transforming model data."""
118
119    pass
120
121
122class PumpWoodWrongParameters(PumpWoodException):
123    """Raise for errors in object deserialization."""
124
125    pass
126
127
128class PumpWoodObjectSavingException(PumpWoodException):
129    """Raise for errors in object deserialization."""
130
131    pass
132
133
134class PumpWoodObjectDeleteException(PumpWoodException):
135    """Raise for errors in object deserialization."""
136
137    pass
138
139
140class PumpWoodActionArgsException(PumpWoodException):
141    """Missing arguments to perform action."""
142
143    pass
144
145
146class PumpWoodUnauthorized(PumpWoodException):
147    """User Unauthorized to perform action."""
148
149    status_code = 401
150
151
152class PumpWoodForbidden(PumpWoodException):
153    """Action not permited."""
154
155    status_code = 403
156
157
158class PumpWoodObjectDoesNotExist(PumpWoodException):
159    """Object not found in database."""
160
161    status_code = 404
162
163
164class PumpWoodQueryException(PumpWoodException):
165    """Problem when querying data, like wrong fields or operators."""
166
167    pass
168
169
170class PumpWoodIntegrityError(PumpWoodException):
171    """Problem when saving data due to IntegrityError."""
172
173    pass
174
175
176class PumpWoodNotImplementedError(PumpWoodException):
177    """Problem when saving data due to NotImplementedError."""
178
179    pass
180
181
182class PumpWoodMicroserviceUnavailableError(PumpWoodException):
183    """Problem when trying to use a microservice that was not deployied."""
184
185    pass
186
187
188class PumpWoodMFAError(PumpWoodException):
189    """Problem when using MFA."""
190
191    pass
192
193
194class PumpWoodOtherException(PumpWoodException):
195    """Other untreated error on server."""
196
197    status_code = 500
198
199    def __repr__(self):
200        """__repr__."""
201        template = "{class_name}[status_code={status_code}]: " + \
202            "{message}\nerror payload={payload}"
203        return template.format(
204            class_name=self.__class__.__name__,
205            status_code=self.status_code, message=self.message,
206            payload=self.payload,)
207
208    def __str__(self):
209        """__str__."""
210        template = "{class_name}[status_code={status_code}]: " + \
211            "{message}\nerror payload={payload}"
212        return template.format(
213            class_name=self.__class__.__name__,
214            status_code=self.status_code, message=self.message,
215            payload=self.payload,)
216
217    def __init__(self, message: str, payload: dict = {}, status_code=None):
218        """__init__."""
219        Exception.__init__(self)
220        # Limit size of the error
221        self.message = message[:1000]
222        if status_code is not None:
223            self.status_code = status_code
224        self.payload = payload
225
226    def to_dict(self):
227        """Serialize exception to dictionary."""
228        rv = {
229            "payload": self.payload,
230            "type": self.__class__.__name__,
231            "message": self.message}
232        return rv
233
234
235class AirflowMicroServiceException(PumpWoodException):
236    """Raises from AirflowMicroService."""
237
238    pass
239
240
241exceptions_dict = {
242    "PumpWoodException": PumpWoodException,
243    "PumpWoodDataLoadingException": PumpWoodDataLoadingException,
244    "PumpWoodDatabaseError": PumpWoodDatabaseError,
245    "PumpWoodDataTransformationException": PumpWoodDataTransformationException,
246    "PumpWoodWrongParameters": PumpWoodWrongParameters,
247    "PumpWoodObjectSavingException": PumpWoodObjectSavingException,
248    "PumpWoodObjectDeleteException": PumpWoodObjectDeleteException,
249    "PumpWoodActionArgsException": PumpWoodActionArgsException,
250    "PumpWoodUnauthorized": PumpWoodUnauthorized,
251    "PumpWoodForbidden": PumpWoodForbidden,
252    "PumpWoodObjectDoesNotExist": PumpWoodObjectDoesNotExist,
253    "PumpWoodQueryException": PumpWoodQueryException,
254    "PumpWoodIntegrityError": PumpWoodIntegrityError,
255    "PumpWoodNotImplementedError": PumpWoodNotImplementedError,
256    "PumpWoodMicroserviceUnavailableError":
257        PumpWoodMicroserviceUnavailableError,
258    "PumpWoodMFAError": PumpWoodMFAError,
259    "PumpWoodOtherException": PumpWoodOtherException,
260    "AirflowMicroServiceException": AirflowMicroServiceException,
261}
262"""
263Dictionary used by backends/microservice to treat Pumpwood exceptions and
264re-raise them exception.
265"""
class PumpWoodException(builtins.Exception):
 10class PumpWoodException(Exception): # NOQA
 11    """Special exception used in Pumpowod Systems.
 12
 13    It permits treatment of raises on applications serializing response
 14    using to_dict function and return status code as `status_code`
 15    attribute value.
 16    """
 17
 18    status_code: int = 400
 19    """PumpWoodException will return status 400 on Pumpwood backend."""
 20
 21    message: str
 22    """Message associated with raise."""
 23
 24    payload: dict
 25    """
 26    Dictionary payload that will be returned by to_dict funcion and format
 27    message string.
 28    """
 29
 30    def __repr__(self):
 31        """@private."""
 32        message_fmt = self.format_message()
 33        template = "{class_name}[status_code={status_code}]: " + \
 34            "{message_fmt}\nerror payload={payload}"
 35        return template.format(
 36            class_name=self.__class__.__name__,
 37            status_code=self.status_code,
 38            message_fmt=message_fmt,
 39            payload=self.payload)
 40
 41    def __str__(self):
 42        """@private."""
 43        message_fmt = self.format_message()
 44        template = "{class_name}[status_code={status_code}]: " + \
 45            "{message_fmt}\nerror payload={payload}"
 46        return template.format(
 47            class_name=self.__class__.__name__,
 48            status_code=self.status_code,
 49            message_fmt=message_fmt,
 50            payload=self.payload)
 51
 52    def __init__(self, message: str, payload: dict = {}, status_code=None):
 53        """__init__.
 54
 55        Args:
 56            message:
 57                Message that will be formated using payload
 58                information using `{key}` to replace information.
 59            payload:
 60                Payload data passed as a dictionary, it will be returned
 61                in payload at `to_dict` funcion and used to format message.
 62            status_code:
 63                Change the default status code of the exception.
 64        """
 65        Exception.__init__(self)
 66        self.message = message
 67        if status_code is not None:
 68            self.status_code = status_code
 69        self.payload = payload
 70
 71    def format_message(self) -> str:
 72        """Format exception message using payload data.
 73
 74        Substitute placeholders at exception message with payload.
 75
 76        Returns:
 77            Return a string of message with placeholders substituted with
 78            payload data.
 79        """
 80        try:
 81            return self.message.format(**self.payload)
 82        except Exception:
 83            return self.message + "\n** format error **"
 84
 85    def to_dict(self) -> dict:
 86        """Serialize Exception object to return as reponse.
 87
 88        Returns:
 89            Return a dictionary with keys:
 90            - **payload [dict]:** Payload associated with raise.
 91            - **type [str]:** Return name of the class of the Exception.
 92            - **message_not_fmt [str]:** Return msg without replacemnt of the
 93                msg with payload information.
 94            - **message [str]:** Return msg formated with payload information.
 95        """
 96        message_fmt = self.format_message()
 97        rv = {
 98            "payload": self.payload,
 99            "type": self.__class__.__name__,
100            "message_not_fmt": self.message,
101            "message": message_fmt}
102        return rv

Special exception used in Pumpowod Systems.

It permits treatment of raises on applications serializing response using to_dict function and return status code as status_code attribute value.

PumpWoodException(message: str, payload: dict = {}, status_code=None)
52    def __init__(self, message: str, payload: dict = {}, status_code=None):
53        """__init__.
54
55        Args:
56            message:
57                Message that will be formated using payload
58                information using `{key}` to replace information.
59            payload:
60                Payload data passed as a dictionary, it will be returned
61                in payload at `to_dict` funcion and used to format message.
62            status_code:
63                Change the default status code of the exception.
64        """
65        Exception.__init__(self)
66        self.message = message
67        if status_code is not None:
68            self.status_code = status_code
69        self.payload = payload

__init__.

Arguments:
  • message: Message that will be formated using payload information using {key} to replace information.
  • payload: Payload data passed as a dictionary, it will be returned in payload at to_dict funcion and used to format message.
  • status_code: Change the default status code of the exception.
status_code: int = 400

PumpWoodException will return status 400 on Pumpwood backend.

message: str

Message associated with raise.

payload: dict

Dictionary payload that will be returned by to_dict funcion and format message string.

def format_message(self) -> str:
71    def format_message(self) -> str:
72        """Format exception message using payload data.
73
74        Substitute placeholders at exception message with payload.
75
76        Returns:
77            Return a string of message with placeholders substituted with
78            payload data.
79        """
80        try:
81            return self.message.format(**self.payload)
82        except Exception:
83            return self.message + "\n** format error **"

Format exception message using payload data.

Substitute placeholders at exception message with payload.

Returns:

Return a string of message with placeholders substituted with payload data.

def to_dict(self) -> dict:
 85    def to_dict(self) -> dict:
 86        """Serialize Exception object to return as reponse.
 87
 88        Returns:
 89            Return a dictionary with keys:
 90            - **payload [dict]:** Payload associated with raise.
 91            - **type [str]:** Return name of the class of the Exception.
 92            - **message_not_fmt [str]:** Return msg without replacemnt of the
 93                msg with payload information.
 94            - **message [str]:** Return msg formated with payload information.
 95        """
 96        message_fmt = self.format_message()
 97        rv = {
 98            "payload": self.payload,
 99            "type": self.__class__.__name__,
100            "message_not_fmt": self.message,
101            "message": message_fmt}
102        return rv

Serialize Exception object to return as reponse.

Returns:

Return a dictionary with keys:

  • payload [dict]: Payload associated with raise.
  • type [str]: Return name of the class of the Exception.
  • message_not_fmt [str]: Return msg without replacemnt of the msg with payload information.
  • message [str]: Return msg formated with payload information.
class PumpWoodDataLoadingException(PumpWoodException):
105class PumpWoodDataLoadingException(PumpWoodException):
106    """Problem when loading data at dataloaders and to_load models."""
107
108    pass

Problem when loading data at dataloaders and to_load models.

class PumpWoodDatabaseError(PumpWoodException):
111class PumpWoodDatabaseError(PumpWoodException):
112    """Errors raised by Postgres and not treated by other handlers."""
113
114    pass

Errors raised by Postgres and not treated by other handlers.

class PumpWoodDataTransformationException(PumpWoodException):
117class PumpWoodDataTransformationException(PumpWoodException):
118    """Problem when transforming model data."""
119
120    pass

Problem when transforming model data.

class PumpWoodWrongParameters(PumpWoodException):
123class PumpWoodWrongParameters(PumpWoodException):
124    """Raise for errors in object deserialization."""
125
126    pass

Raise for errors in object deserialization.

class PumpWoodObjectSavingException(PumpWoodException):
129class PumpWoodObjectSavingException(PumpWoodException):
130    """Raise for errors in object deserialization."""
131
132    pass

Raise for errors in object deserialization.

class PumpWoodObjectDeleteException(PumpWoodException):
135class PumpWoodObjectDeleteException(PumpWoodException):
136    """Raise for errors in object deserialization."""
137
138    pass

Raise for errors in object deserialization.

class PumpWoodActionArgsException(PumpWoodException):
141class PumpWoodActionArgsException(PumpWoodException):
142    """Missing arguments to perform action."""
143
144    pass

Missing arguments to perform action.

class PumpWoodUnauthorized(PumpWoodException):
147class PumpWoodUnauthorized(PumpWoodException):
148    """User Unauthorized to perform action."""
149
150    status_code = 401

User Unauthorized to perform action.

status_code = 401

PumpWoodException will return status 400 on Pumpwood backend.

class PumpWoodForbidden(PumpWoodException):
153class PumpWoodForbidden(PumpWoodException):
154    """Action not permited."""
155
156    status_code = 403

Action not permited.

status_code = 403

PumpWoodException will return status 400 on Pumpwood backend.

class PumpWoodObjectDoesNotExist(PumpWoodException):
159class PumpWoodObjectDoesNotExist(PumpWoodException):
160    """Object not found in database."""
161
162    status_code = 404

Object not found in database.

status_code = 404

PumpWoodException will return status 400 on Pumpwood backend.

class PumpWoodQueryException(PumpWoodException):
165class PumpWoodQueryException(PumpWoodException):
166    """Problem when querying data, like wrong fields or operators."""
167
168    pass

Problem when querying data, like wrong fields or operators.

class PumpWoodIntegrityError(PumpWoodException):
171class PumpWoodIntegrityError(PumpWoodException):
172    """Problem when saving data due to IntegrityError."""
173
174    pass

Problem when saving data due to IntegrityError.

class PumpWoodNotImplementedError(PumpWoodException):
177class PumpWoodNotImplementedError(PumpWoodException):
178    """Problem when saving data due to NotImplementedError."""
179
180    pass

Problem when saving data due to NotImplementedError.

class PumpWoodMicroserviceUnavailableError(PumpWoodException):
183class PumpWoodMicroserviceUnavailableError(PumpWoodException):
184    """Problem when trying to use a microservice that was not deployied."""
185
186    pass

Problem when trying to use a microservice that was not deployied.

class PumpWoodMFAError(PumpWoodException):
189class PumpWoodMFAError(PumpWoodException):
190    """Problem when using MFA."""
191
192    pass

Problem when using MFA.

class PumpWoodOtherException(PumpWoodException):
195class PumpWoodOtherException(PumpWoodException):
196    """Other untreated error on server."""
197
198    status_code = 500
199
200    def __repr__(self):
201        """__repr__."""
202        template = "{class_name}[status_code={status_code}]: " + \
203            "{message}\nerror payload={payload}"
204        return template.format(
205            class_name=self.__class__.__name__,
206            status_code=self.status_code, message=self.message,
207            payload=self.payload,)
208
209    def __str__(self):
210        """__str__."""
211        template = "{class_name}[status_code={status_code}]: " + \
212            "{message}\nerror payload={payload}"
213        return template.format(
214            class_name=self.__class__.__name__,
215            status_code=self.status_code, message=self.message,
216            payload=self.payload,)
217
218    def __init__(self, message: str, payload: dict = {}, status_code=None):
219        """__init__."""
220        Exception.__init__(self)
221        # Limit size of the error
222        self.message = message[:1000]
223        if status_code is not None:
224            self.status_code = status_code
225        self.payload = payload
226
227    def to_dict(self):
228        """Serialize exception to dictionary."""
229        rv = {
230            "payload": self.payload,
231            "type": self.__class__.__name__,
232            "message": self.message}
233        return rv

Other untreated error on server.

PumpWoodOtherException(message: str, payload: dict = {}, status_code=None)
218    def __init__(self, message: str, payload: dict = {}, status_code=None):
219        """__init__."""
220        Exception.__init__(self)
221        # Limit size of the error
222        self.message = message[:1000]
223        if status_code is not None:
224            self.status_code = status_code
225        self.payload = payload

__init__.

status_code = 500

PumpWoodException will return status 400 on Pumpwood backend.

message

Message associated with raise.

payload

Dictionary payload that will be returned by to_dict funcion and format message string.

def to_dict(self):
227    def to_dict(self):
228        """Serialize exception to dictionary."""
229        rv = {
230            "payload": self.payload,
231            "type": self.__class__.__name__,
232            "message": self.message}
233        return rv

Serialize exception to dictionary.

Inherited Members
PumpWoodException
format_message
class AirflowMicroServiceException(PumpWoodException):
236class AirflowMicroServiceException(PumpWoodException):
237    """Raises from AirflowMicroService."""
238
239    pass

Raises from AirflowMicroService.

exceptions_dict = {'PumpWoodException': <class 'PumpWoodException'>, 'PumpWoodDataLoadingException': <class 'PumpWoodDataLoadingException'>, 'PumpWoodDatabaseError': <class 'PumpWoodDatabaseError'>, 'PumpWoodDataTransformationException': <class 'PumpWoodDataTransformationException'>, 'PumpWoodWrongParameters': <class 'PumpWoodWrongParameters'>, 'PumpWoodObjectSavingException': <class 'PumpWoodObjectSavingException'>, 'PumpWoodObjectDeleteException': <class 'PumpWoodObjectDeleteException'>, 'PumpWoodActionArgsException': <class 'PumpWoodActionArgsException'>, 'PumpWoodUnauthorized': <class 'PumpWoodUnauthorized'>, 'PumpWoodForbidden': <class 'PumpWoodForbidden'>, 'PumpWoodObjectDoesNotExist': <class 'PumpWoodObjectDoesNotExist'>, 'PumpWoodQueryException': <class 'PumpWoodQueryException'>, 'PumpWoodIntegrityError': <class 'PumpWoodIntegrityError'>, 'PumpWoodNotImplementedError': <class 'PumpWoodNotImplementedError'>, 'PumpWoodMicroserviceUnavailableError': <class 'PumpWoodMicroserviceUnavailableError'>, 'PumpWoodMFAError': <class 'PumpWoodMFAError'>, 'PumpWoodOtherException': <class 'PumpWoodOtherException'>, 'AirflowMicroServiceException': <class 'AirflowMicroServiceException'>}

Dictionary used by backends/microservice to treat Pumpwood exceptions and re-raise them exception.