{"record":{"id":"39ba309ee6fbc459","repo":"RocketChat/Rocket.Chat","slug":"user-not-found-39ba30","errorCode":null,"errorMessage":"User not found","messagePattern":"User not found","errorType":"exception","errorClass":"Error","httpStatus":400,"severity":"error","filePath":"apps/meteor/server/lib/unbanUserFromRoom.ts","lineNumber":30,"sourceCode":"\t}\n\n\tconst room = await Rooms.findOneById(data.rid);\n\tif (!room || !(await roomCoordinator.getRoomDirectives(room.t).allowMemberAction(room, RoomMemberActions.BAN, fromId))) {\n\t\tthrow new Error('Not allowed');\n\t}\n\n\tconst fromUser = await Users.findOneById(fromId);\n\tif (!fromUser) {\n\t\tthrow new Error('Invalid user');\n\t}\n\n\tif (!(await canAccessRoomAsync(room, fromUser))) {\n\t\tthrow new Error('The required \"roomId\" or \"roomName\" param provided does not match any group');\n\t}\n\n\tconst bannedUser = await Users.findOneByUsernameIgnoringCase(data.username);\n\tif (!bannedUser) {\n\t\tthrow new Error('User not found');\n\t}\n\n\tawait executeUnbanUserFromRoom(data.rid, bannedUser, fromUser);\n\n\treturn true;\n};\n","sourceCodeStart":12,"sourceCodeEnd":37,"githubUrl":"https://github.com/RocketChat/Rocket.Chat/blob/b2c16d5842cbe6b69b59bdf6fc5e5f1afcd1f0b0/apps/meteor/server/lib/unbanUserFromRoom.ts#L12-L37","documentation":"Thrown by unbanUserFromRoom when Users.findOneByUsernameIgnoringCase(data.username) returns null, i.e. no user exists with the given username. The lookup is case-insensitive, so casing is not the issue; the username simply does not exist (typo, deleted user, or an id passed where a username was expected).","triggerScenarios":"/unban slash command with a misspelled or already-deleted username; passing the user's _id or an '@mention' token (with the leading @) instead of the raw username; REST unban call with a username whose account was removed after the ban was created.","commonSituations":"Banned user deletes their account before the unban; UI forwarding the display name instead of the username; copy-paste introducing trailing whitespace or an @ prefix.","solutions":["Strip any leading '@' and whitespace from the username before calling (the slash command expects a bare username)","Verify the user exists first via Users.findOneByUsernameIgnoringCase(username) or GET /api/v1/users.info?username=...","If the banned user was deleted, remove the stale ban record instead of unbanning"],"exampleFix":"// before\nawait unbanUserFromRoom(fromId, { rid, username: `@${name}` });\n\n// after\nconst cleanUsername = username.replace(/^@/, '').trim();\nconst target = await Users.findOneByUsernameIgnoringCase(cleanUsername, { projections: { _id: 1 } });\nif (!target) throw new Error(`No user named ${cleanUsername}`);\nawait unbanUserFromRoom(fromId, { rid, username: cleanUsername });","handlingStrategy":"validation","validationCode":"const clean = rawUsername.replace(/^@/, '').trim();\nconst target = await Users.findOneByUsernameIgnoringCase(clean, { projections: { _id: 1 } });\nif (!target) throw new Error(`No user named ${clean}`);\nawait unbanUserFromRoom(fromId, { rid, username: clean });","typeGuard":null,"tryCatchPattern":"try {\n  await unbanUserFromRoom(fromId, { rid, username });\n} catch (e) {\n  if (e instanceof Error && e.message === 'User not found') {\n    return { ok: false, reason: 'username-unknown' };\n  }\n  throw e;\n}","preventionTips":["Always pass the bare username, never the _id or an @mention","Trim and strip @ before submitting","Offer username autocomplete from real users in moderation UIs"],"tags":["users","bans","username","lookup"],"backgroundTag":"user-not-found","analyzedSha":"b2c16d5842cbe6b69b59bdf6fc5e5f1afcd1f0b0","analyzedAt":"2026-08-18T15:26:39.429Z","schemaVersion":2},"datasetVersion":"2026-08-21T13:17:26.733Z"}