alm0ra / mockafka-py

Mockafka-py is a Python library designed for in-memory mocking of Kafka.[aiokafka - confluence-kafka-python]
https://mockafka-py.readthedocs.io
MIT License
44 stars 11 forks source link

Error handling in CustomDict #69

Closed arash-d closed 4 months ago

arash-d commented 5 months ago

The CustomDict class overrides the __getitem__ method to suppress KeyError exceptions and return None if a key is not found. While this may be intentional, it could potentially mask errors in the code. It's essential to handle errors appropriately and provide meaningful feedback to users.

class CustomDict(dict):
    def __getitem__(self, key):
        try:
            return super().__getitem__(key)
        except KeyError:
            return

https://github.com/alm0ra/mockafka-py/blob/2eb4b515775882133e029a7c959f3c1c94103037/mockafka/cluster_metadata.py#L47

alm0ra commented 5 months ago

Hey @arash-d,

Thanks for highlighting this issue. Do you have any suggestions on how to address this error? Also, I'm unsure about how Kafka handles this scenario. Your contribution would be greatly appreciated.

arash-d commented 5 months ago

Hey @alm0ra! Sorry for the late reply!

Actually you have 3 options:

  1. Avoid try/except and using a default value return super().__getitem__(key, None)
  2. In the case of exception raise a CustomException
  3. Add logging to your except block and return None, something like logging.warning(f"Key '{key}' not found in CustomDict")

Based on your explanation about Kafka handling in this case and my little knowledge about your project, it seems the third option is more suitable.

Cheers!

alm0ra commented 5 months ago

Hey @arash-d

Thanks for suggesting solutions.

I'd be happy if you contribute by opening pull request if you have time. :) i prefer third way too.

alm0ra commented 4 months ago

fixed inv0.1.56