fix(cloud-api): guard contacts[0].profile.name to stop status webhooks from crashing#2609
Conversation
Reviewer's guide (collapsed on small PRs)Reviewer's GuideAdds a null-safe guard when reading WhatsApp Cloud API contact profile names so that status webhooks without a profile object no longer crash the handler and MESSAGES_UPDATE events can be processed correctly. Sequence diagram for WhatsApp Cloud API status webhook handling with guarded contact profile accesssequenceDiagram
participant WhatsAppCloudAPI
participant BusinessStartupService
WhatsAppCloudAPI->>BusinessStartupService: messageHandle(received)
alt [received.contacts[0].profile exists]
BusinessStartupService->>BusinessStartupService: pushName = received.contacts[0].profile.name
else [received.contacts[0].profile missing]
BusinessStartupService->>BusinessStartupService: pushName = received.contacts[0]?.profile?.name
end
BusinessStartupService->>BusinessStartupService: handle MESSAGES_UPDATE
File-Level Changes
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
|
🔴 👎 O guard em |
|
Thanks for the review! Looking at const incomingContact = received?.contacts?.[0];
if (incomingContact) {
pushName = incomingContact?.profile?.name ?? incomingContact?.name ?? incomingContact?.wa_id ?? undefined;
}( So a PR against Closing this, since the |
Problem
WhatsApp Cloud API status webhooks (delivered / read receipts) carry a
contactsarray that does not include aprofileobject — only message webhooks do. InBusinessStartupService.messageHandle, the code unconditionally readsreceived.contacts[0].profile.name:For a status payload,
received.contacts[0].profileisundefined, so this throws:The throw happens before the handler can persist/forward the update, so
MESSAGES_UPDATEevents (delivered/read) are never processed on theWHATSAPP-BUSINESSchannel and delivery/read ticks stay broken.Repro (abridged)
A Cloud API status webhook looks like:
{ "statuses": [{ "id": "wamid...", "status": "delivered", "recipient_id": "55..." }], "contacts": [{ "wa_id": "55..." }] }contacts[0].profileis absent →received.contacts[0].profile.namethrowsTypeError: Cannot read properties of undefined (reading 'name').Fix
One-line guard with optional chaining so
pushNameis simplyundefinedfor status webhooks (which don't need a pushName) and the handler continues to process the update: