grammers icon indicating copy to clipboard operation
grammers copied to clipboard

Panic on message.chat() call

Open Dagunov opened this issue 4 months ago • 3 comments

Hi!

So I managed to send a message, and if I'm sending a message to myself, group or channel then everything is okay, but when I send a dm and try to process Update::NewMessage(message) update with my, just sent, message, when I try to call message.chat() program panics in Message::peer_id:

pub(crate) fn peer_id(&self) -> &tl::enums::Peer {
    utils::peer_from_message(&self.raw)
        .or_else(|| self.fetched_in.as_ref())
        .expect("empty messages from updates should contain peer_id")
}

I'm not sure if I'm doing something wrong.

Dagunov avatar Aug 02 '25 16:08 Dagunov

I'm not sure if I'm doing something wrong.

No, there seems to be an edge-case I failed to account for. I do not know when I'll have the time to look into it, but if you'd be willing to look into the problem, I'd be happy to assist in finding it and reviewing PRs.

Lonami avatar Aug 03 '25 12:08 Lonami

The reason is here:

pub fn chat(&self) -> types::Chat { utils::always_find_entity(self.peer_id(), &self.chats, &self.client) }

Because

pub(crate) fn peer_id(&self) -> &tl::enums::Peer { utils::peer_from_message(&self.raw) .or_else(|| self.fetched_in.as_ref()) .expect("empty messages from updates should contain peer_id") }

Changing peer_id to return Option<&Peer> instead of unwrapping would fix the panic, but this would require updating many places in the codebase to handle Option or Result instead of assuming the peer is always present. This may also require adding a new error variant (e.g. MissingPeerId) to InvocationError or another suitable error type, so that this case can be properly propagated to the caller instead of panicking.

Jackhammer88 avatar Aug 14 '25 11:08 Jackhammer88

#347

Jackhammer88 avatar Aug 14 '25 12:08 Jackhammer88