{"record":{"id":"8a1e443360319dc5","repo":"RocketChat/Rocket.Chat","slug":"error-invalid-token","errorCode":null,"errorMessage":"error-invalid-token","messagePattern":"error-invalid-token","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"warning","filePath":"apps/meteor/server/api/v1/omnichannel/message.ts","lineNumber":282,"sourceCode":"\n\t\t\t\tconst guest: typeof this.bodyParams.visitor & { connectionData?: unknown } = this.bodyParams.visitor;\n\n\t\t\t\tif (settings.get('Livechat_Allow_collect_and_store_HTTP_header_informations')) {\n\t\t\t\t\tguest.connectionData = normalizeHttpHeaderData(this.request.headers);\n\t\t\t\t}\n\n\t\t\t\tvisitor = await registerGuest(guest, {\n\t\t\t\t\tshouldConsiderIdleAgent: settings.get<boolean>('Livechat_enabled_when_agent_idle'),\n\t\t\t\t\tshouldConsiderOfflineAgent: settings.get<boolean>('Livechat_accept_chats_with_no_agents'),\n\t\t\t\t});\n\t\t\t\tif (!visitor) {\n\t\t\t\t\tthrow new Error('error-livechat-visitor-registration');\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tconst guest = visitor;\n\t\t\tif (!guest) {\n\t\t\t\tthrow new Error('error-invalid-token');\n\t\t\t}\n\n\t\t\tconst sentMessages = await Promise.all(\n\t\t\t\tthis.bodyParams.messages.map(async (message: { msg: string }): Promise<{ username: string; msg: string; ts: number }> => {\n\t\t\t\t\tconst messageToSend = {\n\t\t\t\t\t\tguest,\n\t\t\t\t\t\tmessage: {\n\t\t\t\t\t\t\t_id: Random.id(),\n\t\t\t\t\t\t\trid,\n\t\t\t\t\t\t\ttoken: visitorToken,\n\t\t\t\t\t\t\tmsg: message.msg,\n\t\t\t\t\t\t},\n\t\t\t\t\t\troomInfo: {\n\t\t\t\t\t\t\tsource: {\n\t\t\t\t\t\t\t\ttype: isWidget(this.request.headers) ? OmnichannelSourceType.WIDGET : OmnichannelSourceType.API,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t};","sourceCodeStart":264,"sourceCodeEnd":300,"githubUrl":"https://github.com/RocketChat/Rocket.Chat/blob/f9d3ec372bb580fa8d036f94cf03925a478ef768/apps/meteor/server/api/v1/omnichannel/message.ts#L264-L300","documentation":"Thrown in the POST handler of 'livechat/messages' (message.ts:280-283) after the if/else block that either retrieves an existing visitor or registers a new one. const guest = visitor; if (!guest) throws. This is effectively dead code: in the 'if (visitor)' branch, visitor is guaranteed truthy; in the 'else' branch, registerGuest returning null already threw 'error-livechat-visitor-registration' at line 276. By line 280, visitor is always truthy.","triggerScenarios":"Theoretically triggered if the visitor variable becomes falsy between the registration/retrieval block and line 280, which is not possible under normal synchronous control flow. The error message 'error-invalid-token' is misleading as it relates to visitor nullness, not token validity.","commonSituations":"Never encountered in practice. The check is a defensive guard that cannot fire because the prior code paths either keep visitor truthy (existing visitor) or throw early (registration failure at line 276).","solutions":["Recognize this is unreachable — no runtime fix is needed. If encountered, investigate control-flow manipulation or monkey-patching of the visitor variable.","Consider removing the redundant guard or renaming the error message to reflect its actual (impossible) condition."],"exampleFix":"// before\nconst guest = visitor;\nif (!guest) {\n    throw new Error('error-invalid-token');\n}\n\n// after — remove dead code; visitor is guaranteed non-null here\nconst guest = visitor;\n// proceed directly to sendMessage logic","handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":["No caller-side prevention needed — this code path is effectively unreachable because the prior code at lines 256-278 either keeps visitor truthy (existing visitor) or throws 'error-livechat-visitor-registration' (line 276).","If maintaining this file, consider removing the redundant guard at lines 281-283 or renaming the misleading 'error-invalid-token' message."],"tags":["dead-code","omnichannel","redundant-guard","visitor","messages"],"backgroundTag":null,"analyzedSha":"f9d3ec372bb580fa8d036f94cf03925a478ef768","analyzedAt":"2026-08-12T19:07:17.372Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}