Skip to content

Teams webhook message too large exception#2164

Merged
michael-myaskovsky merged 3 commits intomasterfrom
teams-webhook-message-too-large-exception
Mar 22, 2026
Merged

Teams webhook message too large exception#2164
michael-myaskovsky merged 3 commits intomasterfrom
teams-webhook-message-too-large-exception

Conversation

@GuyEshdat
Copy link
Copy Markdown
Contributor

@GuyEshdat GuyEshdat commented Mar 21, 2026

null

Summary by CodeRabbit

  • New Features

    • Teams webhook integration now surfaces a specific "payload too large" error for oversized adaptive card payloads.
  • Bug Fixes

    • Removed automatic/truncated sending of oversized Teams adaptive cards; when payloads exceed limits, users receive an explicit error instead of a silently truncated message.

…1-truncate-teams-webhook-payload"

This reverts commit ed170b7, reversing
changes made to 31e7528.
…teams, either in the status code or inside the response text
@github-actions
Copy link
Copy Markdown
Contributor

👋 @GuyEshdat
Thank you for raising your pull request.
Please make sure to add tests and document all user-facing changes.
You can do this by editing the docs files in this pull request.

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 21, 2026

📝 Walkthrough

Walkthrough

Removed Teams adaptive-card payload truncation and helper functions. send_adaptive_card now posts the provided card payload unchanged. Added TeamsWebhookPayloadTooLargeError; send_message detects Teams webhook "HTTP error 413" responses and raises the new typed exception instead of the generic error.

Changes

Cohort / File(s) Summary
Teams Webhook Payload Handling
elementary/messages/messaging_integrations/teams_webhook.py
Deleted payload-size limit constant, truncation helper, and minimal-card logic; simplified send_adaptive_card to always post given card JSON; added TeamsWebhookPayloadTooLargeError and updated send_message to compute payload_json and map Teams "HTTP error 413" responses to the new exception.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 I stitched the card without a snip or clip,
It hops to Teams in one smooth zip.
If it grows too big and bumps a wall,
A gentle 413 will let us call.
A little hop, a careful cheer—payloads clear! 🥕✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title directly matches the main change: adding a new TeamsWebhookPayloadTooLargeError exception and refactoring Teams webhook error handling for oversized payloads.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch teams-webhook-message-too-large-exception

Comment @coderabbitai help to get the list of available commands and usage tips.

Tip

You can disable poems in the walkthrough.

Disable the reviews.poem setting to disable the poems in the walkthrough.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
elementary/messages/messaging_integrations/teams_webhook.py (1)

101-103: Consider documenting the string-based error detection.

The substring check for "HTTP error 413" is a pragmatic workaround for the old Teams webhook behavior (as noted in the comment above). A brief inline comment explaining this would help future maintainers understand why string matching is used instead of checking response.status_code.

Suggested comment
             if response.status_code not in (HTTPStatus.OK, HTTPStatus.ACCEPTED) or (
                 response.status_code == HTTPStatus.OK and len(response.text) > 1
             ):
+                # Old Teams webhook returns HTTP 200 with error message in body
+                # instead of proper HTTP status codes
                 if "HTTP error 413" in response.text:
                     raise TeamsWebhookPayloadTooLargeError(response)
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@elementary/messages/messaging_integrations/teams_webhook.py` around lines 101
- 103, Add a brief inline comment above the string check that explains why the
code inspects response.text for "HTTP error 413" rather than using
response.status_code: note that this is a pragmatic workaround for old Microsoft
Teams webhook responses that embed the 413 error in the body instead of
returning the proper status code, so the substring match is intentional to
detect payload-too-large and raise TeamsWebhookPayloadTooLargeError; place this
comment next to the existing check that examines response.text and references
TeamsWebhookPayloadTooLargeError for clarity to future maintainers.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@elementary/messages/messaging_integrations/teams_webhook.py`:
- Around line 43-45: The exception message currently uses len(response.text)
(server response size) which is wrong; update the exception constructor (the
class __init__ where super().__init__ is called) to accept a payload_size
parameter (or use an existing request_payload variable) and build the message
with that payload_size (e.g. f"Teams webhook payload size ({payload_size} bytes)
exceeds limit") or omit the size if the caller cannot provide it; then update
call sites that raise this exception to pass the actual request payload size.

---

Nitpick comments:
In `@elementary/messages/messaging_integrations/teams_webhook.py`:
- Around line 101-103: Add a brief inline comment above the string check that
explains why the code inspects response.text for "HTTP error 413" rather than
using response.status_code: note that this is a pragmatic workaround for old
Microsoft Teams webhook responses that embed the 413 error in the body instead
of returning the proper status code, so the substring match is intentional to
detect payload-too-large and raise TeamsWebhookPayloadTooLargeError; place this
comment next to the existing check that examines response.text and references
TeamsWebhookPayloadTooLargeError for clarity to future maintainers.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 0290272b-09fa-412e-b815-cf0557516a24

📥 Commits

Reviewing files that changed from the base of the PR and between ed170b7 and 520be22.

📒 Files selected for processing (1)
  • elementary/messages/messaging_integrations/teams_webhook.py

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
elementary/messages/messaging_integrations/teams_webhook.py (1)

48-65: ⚠️ Potential issue | 🟠 Major

Handle direct HTTP 413 responses in the HTTPError exception path.

send_adaptive_card() calls response.raise_for_status() at line 65, so an actual HTTP 413 response is raised as requests.HTTPError and caught at line 115, where it is re-raised as TeamsWebhookHttpError instead of the new typed TeamsWebhookPayloadTooLargeError. The new exception only fires on the legacy path (lines 102–105) when a 200 status with "HTTP error 413" in the response body is received.

Add 413 handling to the HTTPError exception block:

Suggested fix
         except requests.HTTPError as e:
+            if (
+                e.response is not None
+                and e.response.status_code == HTTPStatus.REQUEST_ENTITY_TOO_LARGE
+            ):
+                raise TeamsWebhookPayloadTooLargeError(
+                    e.response, payload_size=len(payload_json)
+                ) from e
             raise TeamsWebhookHttpError(e.response) from e

Note: The payload size reported (line 104) also excludes the outer Teams message envelope, so it undercounts the actual request body size.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@elementary/messages/messaging_integrations/teams_webhook.py` around lines 48
- 65, send_adaptive_card currently calls response.raise_for_status() so a real
HTTP 413 surfaces as requests.HTTPError but is re-raised as
TeamsWebhookHttpError; update the except requests.HTTPError handler (the block
that currently raises TeamsWebhookHttpError) to detect e.response and if
e.response.status_code == 413 raise TeamsWebhookPayloadTooLargeError instead
(include the reported payload size and note that you must compute the full
request body size including the outer Teams message envelope when populating the
size field). Ensure other HTTP errors still raise TeamsWebhookHttpError and
preserve the original error details.
♻️ Duplicate comments (1)
elementary/messages/messaging_integrations/teams_webhook.py (1)

48-58: ⚠️ Potential issue | 🟡 Minor

Report the size of the full webhook payload, not just the card body.

payload_json only serializes card, but send_adaptive_card() wraps it in the Teams message envelope before posting. The byte count in TeamsWebhookPayloadTooLargeError will therefore be lower than the actual request size. Either compute the size from the final payload or omit the byte count.

Also applies to: 90-90, 103-105

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@elementary/messages/messaging_integrations/teams_webhook.py` around lines 48
- 58, The code currently measures size using only the card JSON, causing
TeamsWebhookPayloadTooLargeError to report a smaller byte count than the real
request; in send_adaptive_card (and the other webhook-sending helpers that raise
TeamsWebhookPayloadTooLargeError) serialize the full message envelope (the
payload dict that includes "type" and "attachments") to bytes (e.g.,
json.dumps(payload).encode("utf-8")) and use len(...) for the byte count, then
use that value in the exception or log; alternatively remove the byte count from
TeamsWebhookPayloadTooLargeError and its raise sites—update references in
send_adaptive_card and the other functions that build the final payload to
compute/report the size from the final payload variable rather than from the
card only.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Outside diff comments:
In `@elementary/messages/messaging_integrations/teams_webhook.py`:
- Around line 48-65: send_adaptive_card currently calls
response.raise_for_status() so a real HTTP 413 surfaces as requests.HTTPError
but is re-raised as TeamsWebhookHttpError; update the except requests.HTTPError
handler (the block that currently raises TeamsWebhookHttpError) to detect
e.response and if e.response.status_code == 413 raise
TeamsWebhookPayloadTooLargeError instead (include the reported payload size and
note that you must compute the full request body size including the outer Teams
message envelope when populating the size field). Ensure other HTTP errors still
raise TeamsWebhookHttpError and preserve the original error details.

---

Duplicate comments:
In `@elementary/messages/messaging_integrations/teams_webhook.py`:
- Around line 48-58: The code currently measures size using only the card JSON,
causing TeamsWebhookPayloadTooLargeError to report a smaller byte count than the
real request; in send_adaptive_card (and the other webhook-sending helpers that
raise TeamsWebhookPayloadTooLargeError) serialize the full message envelope (the
payload dict that includes "type" and "attachments") to bytes (e.g.,
json.dumps(payload).encode("utf-8")) and use len(...) for the byte count, then
use that value in the exception or log; alternatively remove the byte count from
TeamsWebhookPayloadTooLargeError and its raise sites—update references in
send_adaptive_card and the other functions that build the final payload to
compute/report the size from the final payload variable rather than from the
card only.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 740de3f4-8ef9-4969-8531-7fae3f83e42a

📥 Commits

Reviewing files that changed from the base of the PR and between 520be22 and 69e6e4d.

📒 Files selected for processing (1)
  • elementary/messages/messaging_integrations/teams_webhook.py

@michael-myaskovsky michael-myaskovsky merged commit a91d876 into master Mar 22, 2026
23 checks passed
@michael-myaskovsky michael-myaskovsky deleted the teams-webhook-message-too-large-exception branch March 22, 2026 09:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants