fix(callback): add null guards and try-catch wrapper to callback query handler (issue #20) - #38
Merged
Merged
Conversation
…y handler (issue #20) Prevents runtime crashes from unexpected Telegram update shapes: - Early return if queryId is missing (can't answer without it) - Early return if data is missing/empty (nothing to dispatch) - Safe toString() check via typeof guard - Number.isFinite guards on msgId/userId to prevent NaN propagation - Outer try-catch prevents handler death from any malformed update
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes MEDIUM — Runtime Crash / Type Confusion in the callback query handler. Unsafe type assertions on Telegram update objects could cause
TypeErrorcrashes when GramJS returns an unexpected shape. Any unhandled error would kill the callback query handler for all future queries.Root Cause
The handler at
src/index.ts:1237had five unsafe patterns:queryIdpassed directly toanswerCallbackQuery()without null-checkdata?.toString()— ifdataexists but lackstoString(), it crashesNumber(undefined)→NaNformsgId(silent)Number(undefined)→NaNforuserId(silent)Changes
src/index.ts — callback query handler
queryIdis null/undefined, skip processing (can't answer without it)datais empty or not parseable, skip dispatchtypeof callbackUpdate.data?.toString === "function"instead of assuming the method existsmessageIdanduserIddefault to 0 instead of NaNRobustness Improvements
Closes #20
🤖 Generated with Claude Code