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 PumpWoodUniqueDatabaseError(PumpWoodException):
117    """Unique errors raised by Postgres."""
118
119    pass
120
121
122class PumpWoodDataTransformationException(PumpWoodException):
123    """Problem when transforming model data."""
124
125    pass
126
127
128class PumpWoodWrongParameters(PumpWoodException):
129    """Raise for errors in object deserialization."""
130
131    pass
132
133
134class PumpWoodObjectSavingException(PumpWoodException):
135    """Raise for errors in object deserialization."""
136
137    pass
138
139
140class PumpWoodObjectDeleteException(PumpWoodException):
141    """Raise for errors in object deserialization."""
142
143    pass
144
145
146class PumpWoodActionArgsException(PumpWoodException):
147    """Missing arguments to perform action."""
148
149    pass
150
151
152class PumpWoodUnauthorized(PumpWoodException):
153    """User Unauthorized to perform action."""
154
155    status_code = 401
156
157
158class PumpWoodForbidden(PumpWoodException):
159    """Action not permited."""
160
161    status_code = 403
162
163
164class PumpWoodObjectDoesNotExist(PumpWoodException):
165    """Object not found in database."""
166
167    status_code = 404
168
169
170class PumpWoodQueryException(PumpWoodException):
171    """Problem when querying data, like wrong fields or operators."""
172
173    pass
174
175
176class PumpWoodIntegrityError(PumpWoodException):
177    """Problem when saving data due to IntegrityError."""
178
179    pass
180
181
182class PumpWoodNotImplementedError(PumpWoodException):
183    """Problem when saving data due to NotImplementedError."""
184
185    pass
186
187
188class PumpWoodMicroserviceUnavailableError(PumpWoodException):
189    """Problem when trying to use a microservice that was not deployied."""
190
191    pass
192
193
194class PumpWoodMFAError(PumpWoodException):
195    """Problem when using MFA."""
196
197    pass
198
199
200class PumpWoodJSONLoadError(PumpWoodException):
201    """Problem loading json data from request."""
202
203    pass
204
205
206class PumpWoodOtherException(PumpWoodException):
207    """Other untreated error on server."""
208
209    status_code = 500
210
211    def __repr__(self):
212        """__repr__."""
213        template = "{class_name}[status_code={status_code}]: " + \
214            "{message}\nerror payload={payload}"
215        return template.format(
216            class_name=self.__class__.__name__,
217            status_code=self.status_code, message=self.message,
218            payload=self.payload,)
219
220    def __str__(self):
221        """__str__."""
222        template = "{class_name}[status_code={status_code}]: " + \
223            "{message}\nerror payload={payload}"
224        return template.format(
225            class_name=self.__class__.__name__,
226            status_code=self.status_code, message=self.message,
227            payload=self.payload,)
228
229    def __init__(self, message: str, payload: dict = {}, status_code=None):
230        """__init__."""
231        Exception.__init__(self)
232        # Limit size of the error
233        self.message = message[:1000]
234        if status_code is not None:
235            self.status_code = status_code
236        self.payload = payload
237
238    def to_dict(self):
239        """Serialize exception to dictionary."""
240        rv = {
241            "payload": self.payload,
242            "type": self.__class__.__name__,
243            "message": self.message}
244        return rv
245
246
247class AirflowMicroServiceException(PumpWoodException):
248    """Raises from AirflowMicroService."""
249
250    pass
251
252
253exceptions_dict = {
254    "PumpWoodException": PumpWoodException,
255    "PumpWoodDataLoadingException": PumpWoodDataLoadingException,
256    "PumpWoodDatabaseError": PumpWoodDatabaseError,
257    "PumpWoodDataTransformationException": PumpWoodDataTransformationException,
258    "PumpWoodWrongParameters": PumpWoodWrongParameters,
259    "PumpWoodObjectSavingException": PumpWoodObjectSavingException,
260    "PumpWoodObjectDeleteException": PumpWoodObjectDeleteException,
261    "PumpWoodActionArgsException": PumpWoodActionArgsException,
262    "PumpWoodUnauthorized": PumpWoodUnauthorized,
263    "PumpWoodForbidden": PumpWoodForbidden,
264    "PumpWoodObjectDoesNotExist": PumpWoodObjectDoesNotExist,
265    "PumpWoodQueryException": PumpWoodQueryException,
266    "PumpWoodIntegrityError": PumpWoodIntegrityError,
267    "PumpWoodNotImplementedError": PumpWoodNotImplementedError,
268    "PumpWoodMicroserviceUnavailableError":
269        PumpWoodMicroserviceUnavailableError,
270    "PumpWoodMFAError": PumpWoodMFAError,
271    "PumpWoodOtherException": PumpWoodOtherException,
272    "AirflowMicroServiceException": AirflowMicroServiceException,
273    "PumpWoodUniqueDatabaseError": PumpWoodUniqueDatabaseError
274}
275"""
276Dictionary used by backends/microservice to treat Pumpwood exceptions and
277re-raise them exception.
278"""
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 PumpWoodUniqueDatabaseError(PumpWoodException):
117class PumpWoodUniqueDatabaseError(PumpWoodException):
118    """Unique errors raised by Postgres."""
119
120    pass

Unique errors raised by Postgres.

class PumpWoodDataTransformationException(PumpWoodException):
123class PumpWoodDataTransformationException(PumpWoodException):
124    """Problem when transforming model data."""
125
126    pass

Problem when transforming model data.

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

Raise for errors in object deserialization.

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

Raise for errors in object deserialization.

class PumpWoodObjectDeleteException(PumpWoodException):
141class PumpWoodObjectDeleteException(PumpWoodException):
142    """Raise for errors in object deserialization."""
143
144    pass

Raise for errors in object deserialization.

class PumpWoodActionArgsException(PumpWoodException):
147class PumpWoodActionArgsException(PumpWoodException):
148    """Missing arguments to perform action."""
149
150    pass

Missing arguments to perform action.

class PumpWoodUnauthorized(PumpWoodException):
153class PumpWoodUnauthorized(PumpWoodException):
154    """User Unauthorized to perform action."""
155
156    status_code = 401

User Unauthorized to perform action.

status_code = 401

PumpWoodException will return status 400 on Pumpwood backend.

class PumpWoodForbidden(PumpWoodException):
159class PumpWoodForbidden(PumpWoodException):
160    """Action not permited."""
161
162    status_code = 403

Action not permited.

status_code = 403

PumpWoodException will return status 400 on Pumpwood backend.

class PumpWoodObjectDoesNotExist(PumpWoodException):
165class PumpWoodObjectDoesNotExist(PumpWoodException):
166    """Object not found in database."""
167
168    status_code = 404

Object not found in database.

status_code = 404

PumpWoodException will return status 400 on Pumpwood backend.

class PumpWoodQueryException(PumpWoodException):
171class PumpWoodQueryException(PumpWoodException):
172    """Problem when querying data, like wrong fields or operators."""
173
174    pass

Problem when querying data, like wrong fields or operators.

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

Problem when saving data due to IntegrityError.

class PumpWoodNotImplementedError(PumpWoodException):
183class PumpWoodNotImplementedError(PumpWoodException):
184    """Problem when saving data due to NotImplementedError."""
185
186    pass

Problem when saving data due to NotImplementedError.

class PumpWoodMicroserviceUnavailableError(PumpWoodException):
189class PumpWoodMicroserviceUnavailableError(PumpWoodException):
190    """Problem when trying to use a microservice that was not deployied."""
191
192    pass

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

class PumpWoodMFAError(PumpWoodException):
195class PumpWoodMFAError(PumpWoodException):
196    """Problem when using MFA."""
197
198    pass

Problem when using MFA.

class PumpWoodJSONLoadError(PumpWoodException):
201class PumpWoodJSONLoadError(PumpWoodException):
202    """Problem loading json data from request."""
203
204    pass

Problem loading json data from request.

class PumpWoodOtherException(PumpWoodException):
207class PumpWoodOtherException(PumpWoodException):
208    """Other untreated error on server."""
209
210    status_code = 500
211
212    def __repr__(self):
213        """__repr__."""
214        template = "{class_name}[status_code={status_code}]: " + \
215            "{message}\nerror payload={payload}"
216        return template.format(
217            class_name=self.__class__.__name__,
218            status_code=self.status_code, message=self.message,
219            payload=self.payload,)
220
221    def __str__(self):
222        """__str__."""
223        template = "{class_name}[status_code={status_code}]: " + \
224            "{message}\nerror payload={payload}"
225        return template.format(
226            class_name=self.__class__.__name__,
227            status_code=self.status_code, message=self.message,
228            payload=self.payload,)
229
230    def __init__(self, message: str, payload: dict = {}, status_code=None):
231        """__init__."""
232        Exception.__init__(self)
233        # Limit size of the error
234        self.message = message[:1000]
235        if status_code is not None:
236            self.status_code = status_code
237        self.payload = payload
238
239    def to_dict(self):
240        """Serialize exception to dictionary."""
241        rv = {
242            "payload": self.payload,
243            "type": self.__class__.__name__,
244            "message": self.message}
245        return rv

Other untreated error on server.

PumpWoodOtherException(message: str, payload: dict = {}, status_code=None)
230    def __init__(self, message: str, payload: dict = {}, status_code=None):
231        """__init__."""
232        Exception.__init__(self)
233        # Limit size of the error
234        self.message = message[:1000]
235        if status_code is not None:
236            self.status_code = status_code
237        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):
239    def to_dict(self):
240        """Serialize exception to dictionary."""
241        rv = {
242            "payload": self.payload,
243            "type": self.__class__.__name__,
244            "message": self.message}
245        return rv

Serialize exception to dictionary.

Inherited Members
PumpWoodException
format_message
class AirflowMicroServiceException(PumpWoodException):
248class AirflowMicroServiceException(PumpWoodException):
249    """Raises from AirflowMicroService."""
250
251    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'>, 'PumpWoodUniqueDatabaseError': <class 'PumpWoodUniqueDatabaseError'>}

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