Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

305 detailed exception handling inside different filip methods #335

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions filip/clients/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"""
Module for client specific exceptions
"""
import requests.models


class BaseHttpClientException(Exception):
"""
Base exception class for all HTTP clients. The response of a request will be available in the exception.

Args:
message (str): Error message
response (Response): Response object
"""
def __init__(self, message: str, response: requests.models.Response):
super().__init__(message)
self.response = response
94 changes: 33 additions & 61 deletions filip/clients/ngsi_v2/cb.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
from filip.models.ngsi_v2.base import AttrsFormat
from filip.models.ngsi_v2.subscriptions import Subscription, Message
from filip.models.ngsi_v2.registrations import Registration
from filip.clients.exceptions import BaseHttpClientException

if TYPE_CHECKING:
from filip.clients.ngsi_v2.iota import IoTAClient
Expand Down Expand Up @@ -157,7 +158,7 @@ def get_version(self) -> Dict:
res.raise_for_status()
except requests.RequestException as err:
self.logger.error(err)
raise
raise BaseHttpClientException(message=err.response.text, response=err.response) from err

def get_resources(self) -> Dict:
"""
Expand All @@ -174,7 +175,7 @@ def get_resources(self) -> Dict:
res.raise_for_status()
except requests.RequestException as err:
self.logger.error(err)
raise
raise BaseHttpClientException(message=err.response.text, response=err.response) from err

# STATISTICS API
def get_statistics(self) -> Dict:
Expand All @@ -191,7 +192,7 @@ def get_statistics(self) -> Dict:
res.raise_for_status()
except requests.RequestException as err:
self.logger.error(err)
raise
raise BaseHttpClientException(message=err.response.text, response=err.response) from err

# CONTEXT MANAGEMENT API ENDPOINTS
# Entity Operations
Expand Down Expand Up @@ -266,8 +267,7 @@ def post_entity(
else:
return self.update_entity_key_values(entity=entity)
msg = f"Could not post entity {entity.id}"
self.log_error(err=err, msg=msg)
raise
raise BaseHttpClientException(message=msg, response=err.response) from err

def get_entity_list(
self,
Expand Down Expand Up @@ -412,8 +412,7 @@ def get_entity_list(

except requests.RequestException as err:
msg = "Could not load entities"
self.log_error(err=err, msg=msg)
raise
raise BaseHttpClientException(message=msg, response=err.response) from err

def get_entity(
self,
Expand Down Expand Up @@ -475,8 +474,7 @@ def get_entity(
res.raise_for_status()
except requests.RequestException as err:
msg = f"Could not load entity {entity_id}"
self.log_error(err=err, msg=msg)
raise
raise BaseHttpClientException(message=msg, response=err.response) from err

def get_entity_attributes(
self,
Expand Down Expand Up @@ -538,8 +536,7 @@ def get_entity_attributes(
res.raise_for_status()
except requests.RequestException as err:
msg = f"Could not load attributes from entity {entity_id} !"
self.log_error(err=err, msg=msg)
raise
raise BaseHttpClientException(message=msg, response=err.response) from err

def update_entity(self, entity: ContextEntity, append_strict: bool = False):
"""
Expand Down Expand Up @@ -677,8 +674,7 @@ def delete_entity(
res.raise_for_status()
except requests.RequestException as err:
msg = f"Could not delete entity {entity_id} !"
self.log_error(err=err, msg=msg)
raise
raise BaseHttpClientException(message=msg, response=err.response) from err

if delete_devices:
from filip.clients.ngsi_v2 import IoTAClient
Expand Down Expand Up @@ -825,8 +821,7 @@ def update_or_append_entity_attributes(
res.raise_for_status()
except requests.RequestException as err:
msg = f"Could not update or append attributes of entity" f" {entity.id} !"
self.log_error(err=err, msg=msg)
raise
raise BaseHttpClientException(message=msg, response=err.response) from err

def update_entity_key_values(self,
entity: Union[ContextEntityKeyValues, dict],):
Expand Down Expand Up @@ -862,8 +857,7 @@ def update_entity_key_values(self,
except requests.RequestException as err:
msg = f"Could not update attributes of entity" \
f" {entity.id} !"
self.log_error(err=err, msg=msg)
raise
raise BaseHttpClientException(message=msg, response=err.response) from err

def update_entity_attributes_key_values(self,
entity_id: str,
Expand Down Expand Up @@ -968,8 +962,7 @@ def update_existing_entity_attributes(
res.raise_for_status()
except requests.RequestException as err:
msg = f"Could not update attributes of entity" f" {entity.id} !"
self.log_error(err=err, msg=msg)
raise
raise BaseHttpClientException(message=msg, response=err.response) from err

def override_entity(self,
entity: Union[ContextEntity, ContextEntityKeyValues],
Expand Down Expand Up @@ -1064,8 +1057,7 @@ def replace_entity_attributes(
res.raise_for_status()
except requests.RequestException as err:
msg = f"Could not replace attribute of entity {entity_id} !"
self.log_error(err=err, msg=msg)
raise
raise BaseHttpClientException(message=msg, response=err.response) from err

# Attribute operations
def get_attribute(
Expand Down Expand Up @@ -1111,8 +1103,7 @@ def get_attribute(
msg = (
f"Could not load attribute '{attr_name}' from entity" f"'{entity_id}' "
)
self.log_error(err=err, msg=msg)
raise
raise BaseHttpClientException(message=msg, response=err.response) from err

def update_entity_attribute(self,
entity_id: str,
Expand Down Expand Up @@ -1197,8 +1188,7 @@ def update_entity_attribute(self,
msg = (
f"Could not update attribute '{attr_name}' of entity" f"'{entity_id}' "
)
self.log_error(err=err, msg=msg)
raise
raise BaseHttpClientException(message=msg, response=err.response) from err

def delete_entity_attribute(
self, entity_id: str, attr_name: str, entity_type: str = None
Expand Down Expand Up @@ -1234,8 +1224,7 @@ def delete_entity_attribute(
msg = (
f"Could not delete attribute '{attr_name}' of entity '{entity_id}'"
)
self.log_error(err=err, msg=msg)
raise
raise BaseHttpClientException(message=msg, response=err.response) from err

# Attribute value operations
def get_attribute_value(
Expand Down Expand Up @@ -1271,8 +1260,7 @@ def get_attribute_value(
f"Could not load value of attribute '{attr_name}' from "
f"entity'{entity_id}' "
)
self.log_error(err=err, msg=msg)
raise
raise BaseHttpClientException(message=msg, response=err.response) from err

def update_attribute_value(self, *,
entity_id: str,
Expand Down Expand Up @@ -1329,8 +1317,7 @@ def update_attribute_value(self, *,
f"Could not update value of attribute '{attr_name}' from "
f"entity '{entity_id}' "
)
self.log_error(err=err, msg=msg)
raise
raise BaseHttpClientException(message=msg, response=err.response) from err

# Types Operations
def get_entity_types(
Expand Down Expand Up @@ -1363,8 +1350,7 @@ def get_entity_types(
res.raise_for_status()
except requests.RequestException as err:
msg = "Could not load entity types!"
self.log_error(err=err, msg=msg)
raise
raise BaseHttpClientException(message=msg, response=err.response) from err

def get_entity_type(self, entity_type: str) -> Dict[str, Any]:
"""
Expand All @@ -1386,8 +1372,7 @@ def get_entity_type(self, entity_type: str) -> Dict[str, Any]:
res.raise_for_status()
except requests.RequestException as err:
msg = f"Could not load entities of type" f"'{entity_type}' "
self.log_error(err=err, msg=msg)
raise
raise BaseHttpClientException(message=msg, response=err.response) from err

# SUBSCRIPTION API ENDPOINTS
def get_subscription_list(self, limit: PositiveInt = inf) -> List[Subscription]:
Expand All @@ -1413,8 +1398,7 @@ def get_subscription_list(self, limit: PositiveInt = inf) -> List[Subscription]:
return adapter.validate_python(items)
except requests.RequestException as err:
msg = "Could not load subscriptions!"
self.log_error(err=err, msg=msg)
raise
raise BaseHttpClientException(message=msg, response=err.response) from err

def post_subscription(
self,
Expand Down Expand Up @@ -1498,8 +1482,7 @@ def post_subscription(
res.raise_for_status()
except requests.RequestException as err:
msg = "Could not send subscription!"
self.log_error(err=err, msg=msg)
raise
raise BaseHttpClientException(message=msg, response=err.response) from err

def get_subscription(self, subscription_id: str) -> Subscription:
"""
Expand All @@ -1520,8 +1503,7 @@ def get_subscription(self, subscription_id: str) -> Subscription:
res.raise_for_status()
except requests.RequestException as err:
msg = f"Could not load subscription {subscription_id}"
self.log_error(err=err, msg=msg)
raise
raise BaseHttpClientException(message=msg, response=err.response) from err

def update_subscription(
self, subscription: Subscription, skip_initial_notification: bool = False
Expand Down Expand Up @@ -1574,8 +1556,7 @@ def update_subscription(
res.raise_for_status()
except requests.RequestException as err:
msg = f"Could not update subscription {subscription.id}"
self.log_error(err=err, msg=msg)
raise
raise BaseHttpClientException(message=msg, response=err.response) from err

def delete_subscription(self, subscription_id: str) -> None:
"""
Expand All @@ -1595,8 +1576,7 @@ def delete_subscription(self, subscription_id: str) -> None:
res.raise_for_status()
except requests.RequestException as err:
msg = f"Could not delete subscription {subscription_id}"
self.log_error(err=err, msg=msg)
raise
raise BaseHttpClientException(message=msg, response=err.response) from err

# Registration API
def get_registration_list(self, *, limit: PositiveInt = None) -> List[Registration]:
Expand All @@ -1623,8 +1603,7 @@ def get_registration_list(self, *, limit: PositiveInt = None) -> List[Registrati
return adapter.validate_python(items)
except requests.RequestException as err:
msg = "Could not load registrations!"
self.log_error(err=err, msg=msg)
raise
raise BaseHttpClientException(message=msg, response=err.response) from err

def post_registration(self, registration: Registration):
"""
Expand Down Expand Up @@ -1653,8 +1632,7 @@ def post_registration(self, registration: Registration):
res.raise_for_status()
except requests.RequestException as err:
msg = f"Could not send registration {registration.id}!"
self.log_error(err=err, msg=msg)
raise
raise BaseHttpClientException(message=msg, response=err.response) from err

def get_registration(self, registration_id: str) -> Registration:
"""
Expand All @@ -1676,8 +1654,7 @@ def get_registration(self, registration_id: str) -> Registration:
res.raise_for_status()
except requests.RequestException as err:
msg = f"Could not load registration {registration_id} !"
self.log_error(err=err, msg=msg)
raise
raise BaseHttpClientException(message=msg, response=err.response) from err

def update_registration(self, registration: Registration):
"""
Expand Down Expand Up @@ -1706,8 +1683,7 @@ def update_registration(self, registration: Registration):
res.raise_for_status()
except requests.RequestException as err:
msg = f"Could not update registration {registration.id} !"
self.log_error(err=err, msg=msg)
raise
raise BaseHttpClientException(message=msg, response=err.response) from err

def delete_registration(self, registration_id: str) -> None:
"""
Expand All @@ -1726,8 +1702,7 @@ def delete_registration(self, registration_id: str) -> None:
res.raise_for_status()
except requests.RequestException as err:
msg = f"Could not delete registration {registration_id} !"
self.log_error(err=err, msg=msg)
raise
raise BaseHttpClientException(message=msg, response=err.response) from err

# Batch operation API
def update(self,
Expand Down Expand Up @@ -1810,8 +1785,7 @@ def update(self,
res.raise_for_status()
except requests.RequestException as err:
msg = f"Update operation '{action_type}' failed!"
self.log_error(err=err, msg=msg)
raise
raise BaseHttpClientException(message=msg, response=err.response) from err

def query(
self,
Expand Down Expand Up @@ -1861,8 +1835,7 @@ def query(
return items
except requests.RequestException as err:
msg = "Query operation failed!"
self.log_error(err=err, msg=msg)
raise
raise BaseHttpClientException(message=msg, response=err.response) from err

def notify(self, message: Message) -> None:
"""
Expand Down Expand Up @@ -1900,8 +1873,7 @@ def notify(self, message: Message) -> None:
f"Sending notifcation message failed! \n "
f"{message.model_dump_json(indent=2)}"
)
self.log_error(err=err, msg=msg)
raise
raise BaseHttpClientException(message=msg, response=err.response) from err

def post_command(
self,
Expand Down
Loading