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

Bug: Improve error description #147

Open
1 task done
yanivbh1 opened this issue Apr 28, 2023 · 6 comments
Open
1 task done

Bug: Improve error description #147

yanivbh1 opened this issue Apr 28, 2023 · 6 comments
Assignees
Labels
bug Something isn't working good first issue Good for newcomers

Comments

@yanivbh1
Copy link
Contributor

Describe the bug

Hey,
I was trying to produce a protobuf dict to a station without schema attached and received the following error:

$ python3 producer-protobuf.py
memphis: Unsupported message type

That error should be something like:

$ python3 producer-protobuf.py
memphis: Unsupported message type.
Potential issues:
- Memphis requires an array of bytes to produce a binary format into the broker.
- If you want to use protobuf and other supported formats within Schemaverse, please attach a schema to the station.

Steps to reproduce

from __future__ import annotations
import asyncio
from memphis import Memphis, Headers, MemphisError, MemphisConnectError, MemphisHeaderError, MemphisSchemaError
import entity_pb2 as ENTITY

async def main():
    try:
        memphis = Memphis()
        await memphis.connect(host="localhost", username="root", password="memphis")
        
        producer = await memphis.producer(station_name="benchmark", producer_name="protobuf-producer") # you can send the message parameter as dict as well
        
        obj = ENTITY.Entity()
        obj.identifier = 1
        obj.description = "Amazing"

        for i in range(5):
            await producer.produce(obj)
        
    except (MemphisError, MemphisConnectError, MemphisHeaderError, MemphisSchemaError) as e:
        print(e)
        
    finally:
        await memphis.close()
        
if __name__ == "__main__":
    asyncio.run(main())

Affected services

Broker, SDKs

Platforms

No response

If UI - Browsers

No response

Environment

No response

Additional context

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct
@yanivbh1 yanivbh1 added bug Something isn't working good first issue Good for newcomers labels Apr 28, 2023
@ohDaddyPlease
Copy link

I think you need to create issue in https://github.com/memphisdev/memphis.py

@yanivbh1 yanivbh1 transferred this issue from superstreamlabs/memphis May 2, 2023
@Adarsh-jaiss
Copy link
Contributor

Hey @yanivbh1 , i would love to work on this issue. Can you please tell me the file in which i should make the changes to make things work, because I have looked upon the code base, but i am unable to understand where this error message has been written.

@yanivbh1
Copy link
Contributor Author

yanivbh1 commented Jul 3, 2023

Adding @shay23b to the rescue

@shay23b
Copy link
Contributor

shay23b commented Jul 4, 2023

Hi @Adarsh-jaiss ,
There are a few functions that raise "Unsupported message type" error in the producer.py file, in this case it returns from the "validate_protobuf" function

@Adarsh-jaiss
Copy link
Contributor

Adarsh-jaiss commented Jul 11, 2023

Hey @shay23b, @yanivbh1 , I tried to understand the bug and took help from an AI model and it suggested these changes in the "validate_protobuf" function :

In the updated code:

  • We added an additional elif condition to check if the message is of type dict. Previously, only bytearray and objects with SerializeToString method were supported. This will allow us to handle protobuf dictionaries as well.

  • In the else block, we raise a MemphisSchemaError with a more descriptive error message including the type of the unsupported message type.

By making these changes, the code will provide a clearer error message when encountering an unsupported message type. Additionally, it will be able to handle protobuf dictionaries as well.

Here's the updated code :

def validate_protobuf(self, message):
    proto_msg = self.connection.proto_msgs[self.internal_station_name]
    msg_to_send = ""

    try:
        if isinstance(message, bytearray):
            msg_to_send = bytes(message)
            try:
                proto_msg.ParseFromString(msg_to_send)
                proto_msg.SerializeToString()
                msg_to_send = msg_to_send.decode("utf-8")
            except Exception as e:
                if "parsing message" in str(e):
                    e = "Invalid message format, expecting protobuf"
                raise MemphisSchemaError(str(e))
            return message
        elif hasattr(message, "SerializeToString"):
            msg_to_send = message.SerializeToString()
            proto_msg.ParseFromString(msg_to_send)
            proto_msg.SerializeToString()
            try:
                proto_msg.ParseFromString(msg_to_send)
                proto_msg.SerializeToString()
            except Exception as e:
                if "parsing message" in str(e):
                    e = "Error parsing protobuf message"
                raise MemphisSchemaError(str(e))
            return msg_to_send
        elif isinstance(message, dict):
            try:
                protobuf_json_format.ParseDict(message, proto_msg)
                msg_to_send = proto_msg.SerializeToString()
                return msg_to_send
            except Exception as e:
                raise MemphisSchemaError(str(e))
        else:
            raise MemphisSchemaError("Unsupported message type: {}".format(type(message).__name__))

    except Exception as e:
        raise MemphisSchemaError("Schema validation has failed: " + str(e))

Kindly review this! @shay23b

@shay23b
Copy link
Contributor

shay23b commented Sep 13, 2023

Hi @Adarsh-jaiss,
Sorry for the delay
So regarding the first point - dict already exists as a type that can be passed so it is not needed to add
And regarding the second one - it is not enough to just say what type is not supported, it would be better to describe what the user can do in order to fix his problem - so for example add the supported types to the error

If you will be working on this issue please let us know and open a PR when you're done.
This is how to contribute:
https://github.com/memphisdev/memphis.py/blob/master/CONTRIBUTING.md

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

5 participants