Skip to content

fix(http): reject malformed JSON in webhook adapter#872

Merged
KnorpelSenf merged 2 commits intogrammyjs:mainfrom
huntharo:fix-http-uncatchable-exception
Feb 26, 2026
Merged

fix(http): reject malformed JSON in webhook adapter#872
KnorpelSenf merged 2 commits intogrammyjs:mainfrom
huntharo:fix-http-uncatchable-exception

Conversation

@huntharo
Copy link
Contributor

Fix uncaught JSON parse exception in http webhook adapter

Fixes #871

Summary

The Node http/https webhook adapter could throw an uncaught exception when the request body contains malformed JSON.
This PR converts that failure mode into a rejected update promise and adds a regression test.

Problem

  • Source location: src/convenience/frameworks.ts (http adapter update getter), around src/convenience/frameworks.ts#L452-L466.
  • Previously, JSON.parse(raw) could throw inside the "end" event callback, escaping normal promise rejection flow.

Changes

  1. Wrap JSON.parse(raw) in try/catch and call reject(err) on parse failures.
  2. Add regression test http should reject malformed JSON bodies in test/convenience/webhook.test.ts#L163-L200.

Validation

  1. Reproduced failure with reverted fix:
    • deno test --seed=123456 --parallel --allow-import ./test/ test/convenience/webhook.test.ts
    • Failing step: framework adapters ... http should reject malformed JSON bodies
  2. Re-ran with fix applied:
    • deno test --seed=123456 --parallel --allow-import ./test/ test/convenience/webhook.test.ts
    • Result: pass (40 passed, 0 failed)

Notes

  • Behavior after this change is consistent with other adapter error paths: malformed payloads are propagated as async rejections that callers can handle.

Copy link
Member

@KnorpelSenf KnorpelSenf left a comment

Choose a reason for hiding this comment

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

Nice catch

@KnorpelSenf KnorpelSenf merged commit 3f0a52e into grammyjs:main Feb 26, 2026
6 checks passed
@KnorpelSenf
Copy link
Member

@all-contributors add @huntharo for bug, code, and test

@allcontributors
Copy link
Contributor

@KnorpelSenf

I've put up a pull request to add @huntharo! 🎉

@KnorpelSenf
Copy link
Member

This was released in 1.40.1.

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.

Bug: http webhook adapter can throw uncaught exception on malformed / incomplete / empty JSON body

2 participants