{"record":{"id":"02c1d96e92be27ae","repo":"RocketChat/Rocket.Chat","slug":"invalid-main-message","errorCode":null,"errorMessage":"Invalid main message","messagePattern":"Invalid main message","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"apps/meteor/client/views/room/contextualBar/Threads/hooks/useThreadMainMessageQuery.ts","lineNumber":95,"sourceCode":"\n\tconst queryClient = useQueryClient();\n\tconst unsubscribeRef = useRef<(() => void) | undefined>(undefined);\n\n\tuseEffect(() => {\n\t\treturn () => {\n\t\t\tunsubscribeRef.current?.();\n\t\t\tunsubscribeRef.current = undefined;\n\t\t};\n\t}, [tmid]);\n\n\treturn useQuery({\n\t\tqueryKey: roomsQueryKeys.threadMainMessage(room._id, tmid),\n\n\t\tqueryFn: async ({ queryKey }) => {\n\t\t\tconst mainMessage = await getMessage(tmid);\n\n\t\t\tif (!mainMessage) {\n\t\t\t\tthrow new Error('Invalid main message');\n\t\t\t}\n\n\t\t\tconst debouncedInvalidate = withDebouncing({ wait: 10000 })(() => {\n\t\t\t\tqueryClient.invalidateQueries({ queryKey, exact: true });\n\t\t\t});\n\n\t\t\tunsubscribeRef.current =\n\t\t\t\tunsubscribeRef.current ||\n\t\t\t\tsubscribeToMessage(mainMessage, {\n\t\t\t\t\tonMutate: async (message) => {\n\t\t\t\t\t\tconst msg = await onClientMessageReceived(message);\n\t\t\t\t\t\tqueryClient.setQueryData(queryKey, () => msg);\n\t\t\t\t\t\tdebouncedInvalidate();\n\t\t\t\t\t},\n\t\t\t\t\tonDelete: () => {\n\t\t\t\t\t\tonDelete?.();\n\t\t\t\t\t\tqueryClient.invalidateQueries({ queryKey, exact: true });\n\t\t\t\t\t},","sourceCodeStart":77,"sourceCodeEnd":113,"githubUrl":"https://github.com/RocketChat/Rocket.Chat/blob/f9d3ec372bb580fa8d036f94cf03925a478ef768/apps/meteor/client/views/room/contextualBar/Threads/hooks/useThreadMainMessageQuery.ts#L77-L113","documentation":"Thrown inside the threadMainMessage React Query queryFn when getMessage(tmid) returns a falsy value. This fetches the root/main message of a thread (identified by tmid — the thread message ID, which is the _id of the message that started the thread). If the fetch returns null/undefined, the query fails with 'Invalid main message', preventing the thread view from rendering with incomplete data.","triggerScenarios":"The thread's main message was deleted after the thread was created. The user does not have permission to read the main message. The tmid is invalid or refers to a message in a different room. Network error causes getMessage to return null. The main message is in a room the user left.","commonSituations":"Thread root message was deleted by a moderator but replies remain. User was removed from the room where the thread started. Stale tmid from cached thread metadata after room data changed. API returns null due to permission checks.","solutions":["Verify the thread main message exists and is accessible before opening the thread view.","Catch the error in the query consumer and show a 'thread main message unavailable' state.","Refresh thread metadata if the main message might have been deleted.","Check user permissions on the thread's parent room before querying."],"exampleFix":"// before\nconst { data } = useThreadMainMessageQuery({ room, tmid });\n// after\nconst { data, isError } = useThreadMainMessageQuery({ room, tmid });\nif (isError) {\n  return <ThreadUnavailable />;\n}","handlingStrategy":"try-catch","validationCode":"const mainMessage = await getMessage(tmid);\nif (!mainMessage) {\n  // do not open thread view, or show fallback\n  return;\n}","typeGuard":"const isThreadMainMessageValid = (msg: unknown): msg is IMessage =>\n  Boolean(msg && typeof msg === 'object' && '_id' in msg);","tryCatchPattern":"const { data, isError } = useThreadMainMessageQuery({ room, tmid });\nif (isError) {\n  return <ThreadMainMessageUnavailable />;\n}","preventionTips":["Verify the thread main message exists before opening the thread view.","Handle the React Query error state with a user-facing fallback UI.","Check user permissions on the parent room before querying thread data.","Refresh thread metadata if the main message may have been deleted."],"tags":["thread","message","react-query","permissions","cache"],"backgroundTag":null,"analyzedSha":"f9d3ec372bb580fa8d036f94cf03925a478ef768","analyzedAt":"2026-08-12T19:07:17.372Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}