Skip to content
Filippo Romani edited this page Nov 25, 2024 · 1 revision

Using the sync version (a.k.a. WhatsApp() class)

Errors are already handled while using the synchronous version of the library. While sending a message or a button (or anything else), you can make a try-except statement to catch any eventual error:

... 

print("sending button")
try:
    app.send_button(
        {
            "header": "Header Testing", # obviously wrong button, as way more content is needed
            
        },
        dest_phone_number,
        sender=1,
    )
except Exception as e:
    print(f"send_button: {e}")

...

This will return the following text in the terminal:

send_button: {'error': "(#100) The parameter interactive['action'] is required.", 'code': 100}

The error handler is implemented by default in every library method.

Using the async version (a.k.a. AsyncWhatsApp)

Errors must be handled manually one by one while using the asynchronous version of the library as the request is going to be completed in the future. While sending a message or a button (or anything else), you can handle any error as follows:

... 

print("sending button")
v = await app.send_button(
    {
        "header": "Header Testing", # obviously wrong button, as way more content is needed
        
    },
    dest_phone_number,
    sender=1,
)
while not v.done():
    await asyncio.sleep(1) # wait for the request to finish
try: app.handle(v.result())
except Exception as error:  
    print(f"error: {error}") # catch the error
...

This will return the following text in the terminal:

send_button: {'error': "(#100) The parameter interactive['action'] is required.", 'code': 100}

The handle(data: dict) function is built to take the request json output and parse the error if it contains one, raising an Exception with the error code and message.