rveachkc / pymsteams

Format messages and post to Microsoft Teams.
https://pypi.org/project/pymsteams/
Apache License 2.0
418 stars 77 forks source link

Handling MS Teams Rate Limit Request Failures #146

Open Joooooooosh88 opened 1 year ago

Joooooooosh88 commented 1 year ago

Describe the bug MS Teams rate limiting is not handled well and does not raise an exception. MS unfortunately chose to respond to rate limited requests with a 200 status code, only including the 429 error in the response body. It would be fantastic if this library would raise a TeamsWebhookException if a 429 error is found in the response body, help avoid the trap I'm sure a lot of people fall into, assuming MS would pick sensible response codes.

To Reproduce Steps to reproduce the behaviour:

  1. Trigger MS Teams webhook rate limit (default 4 requests/sec)
  2. Receive a 200 OK response but with a 429 error in the response body
  3. Standard request code raise_for_status or HTTP troubleshooting example, does not detect rate limiting, as it relies on status code
  4. Watch your application log 200's and leave you non-the-wiser

Expected behavior MS choosing to respond with a 200 is unfortunate and non-sensical but something I'd hope a library like this would account for.

zabojpetr commented 8 months ago

It's not only Rate Limit, I got more errors for example: Status: True Response: <Response [200]> Content: b'Webhook message delivery failed with error: Microsoft Teams endpoint returned HTTP error 500 with ContextId...'

Status: True Response: <Response [200]> Content: b'System.Net.Http.HttpRequestException: Error while copying content to a stream.'