{"record":{"id":"aad98d91af1795a6","repo":"RocketChat/Rocket.Chat","slug":"error-invalid-arguments-aad98d","errorCode":"error-invalid-arguments","errorMessage":"Invalid arguments","messagePattern":"Invalid arguments","errorType":"exception","errorClass":"Meteor.Error","httpStatus":null,"severity":"error","filePath":"apps/meteor/server/meteor-methods/auth/addUserToRole.ts","lineNumber":19,"sourceCode":"import { api } from '@rocket.chat/core-services';\nimport type { IRole, IUser } from '@rocket.chat/core-typings';\nimport { Roles, Users } from '@rocket.chat/models';\nimport { Meteor } from 'meteor/meteor';\n\nimport { hasPermissionAsync } from '../../lib/authorization/hasPermission';\nimport { addUserRolesAsync } from '../../lib/roles/addUserRoles';\nimport { settings } from '../../settings';\n\nexport const addUserToRole = async (userId: string, roleId: string, username: IUser['username'], scope?: string): Promise<boolean> => {\n\tif (!(await hasPermissionAsync(userId, 'access-permissions'))) {\n\t\tthrow new Meteor.Error('error-action-not-allowed', 'Accessing permissions is not allowed', {\n\t\t\tmethod: 'authorization:addUserToRole',\n\t\t\taction: 'Accessing_permissions',\n\t\t});\n\t}\n\n\tif (!roleId || typeof roleId.valueOf() !== 'string' || !username || typeof username.valueOf() !== 'string') {\n\t\tthrow new Meteor.Error('error-invalid-arguments', 'Invalid arguments', {\n\t\t\tmethod: 'authorization:addUserToRole',\n\t\t});\n\t}\n\n\tconst role = await Roles.findOneById<Pick<IRole, '_id'>>(roleId, { projection: { _id: 1 } });\n\n\tif (!role) {\n\t\tthrow new Meteor.Error('error-invalid-role', 'Invalid Role', {\n\t\t\tmethod: 'authorization:addUserToRole',\n\t\t});\n\t}\n\n\tif (role._id === 'admin' && !(await hasPermissionAsync(userId, 'assign-admin-role'))) {\n\t\tthrow new Meteor.Error('error-action-not-allowed', 'Assigning admin is not allowed', {\n\t\t\tmethod: 'authorization:addUserToRole',\n\t\t\taction: 'Assign_admin',\n\t\t});\n\t}","sourceCodeStart":1,"sourceCodeEnd":37,"githubUrl":"https://github.com/RocketChat/Rocket.Chat/blob/b2c16d5842cbe6b69b59bdf6fc5e5f1afcd1f0b0/apps/meteor/server/meteor-methods/auth/addUserToRole.ts#L1-L37","documentation":"addUserToRole() validates its inputs: roleId and username must both be present and 'string-valued' (checked via valueOf() so String objects pass but numbers/objects fail). Empty strings, null, or non-string arguments throw error-invalid-arguments before any database lookup happens.","triggerScenarios":"Calling authorization:addUserToRole / roles.addUserToRole with roleId undefined (a select defaulting to ''), username null, or numeric ids not cast to string; accidentally passing a DOM element or event instead of its .value.","commonSituations":"Forms whose state starts empty with submit not disabled; refactors renaming props (roleId vs _id); JSON payloads using numbers for ids.","solutions":["Ensure both roleId and username are non-empty strings before calling (cast with String(...) when needed).","Disable the submit action until role and user selectors hold values.","Log the outbound payload when this fires to spot serialization bugs."],"exampleFix":"// before: role select still on its empty default\nMeteor.call('authorization:addUserToRole', roleSelect.value /* '' */, username);\n\n// after: guard both arguments\nconst roleId = String(roleSelect.value || '').trim();\nconst uname = String(username || '').trim();\nif (!roleId || !uname) throw new Error('roleId and username are required');\nMeteor.call('authorization:addUserToRole', roleId, uname);","handlingStrategy":"validation","validationCode":"const isValidRoleAndUsername = (roleId: unknown, username: unknown): boolean =>\n  typeof roleId === 'string' && roleId.length > 0 &&\n  typeof username === 'string' && username.length > 0;","typeGuard":null,"tryCatchPattern":"try {\n  await Meteor.callAsync('authorization:addUserToRole', roleId, username, scope);\n} catch (err: any) {\n  if (err?.error === 'error-invalid-arguments' && err?.details?.method === 'authorization:addUserToRole') {\n    // log the payload shape to find the serialization bug, fix inputs, then retry\n    logBadPayload({ roleId, username });\n    return;\n  }\n  throw err;\n}","preventionTips":["Disable submit until both role and user selectors have non-empty values.","Cast ids to string at the boundary (String(id).trim()).","Keep an outbound-payload log for admin mutations to catch shape regressions."],"tags":["validation","roles","arguments","meteor-methods"],"backgroundTag":"missing-required-field","analyzedSha":"b2c16d5842cbe6b69b59bdf6fc5e5f1afcd1f0b0","analyzedAt":"2026-08-18T15:26:39.429Z","schemaVersion":2},"datasetVersion":"2026-08-21T13:17:26.733Z"}