{"record":{"id":"990e4e301063f8ff","repo":"RocketChat/Rocket.Chat","slug":"error-could-not-change-email","errorCode":"error-could-not-change-email","errorMessage":"Could not change email","messagePattern":"Could not change email","errorType":"exception","errorClass":"Meteor.Error","httpStatus":null,"severity":"error","filePath":"apps/meteor/server/meteor-methods/users/setEmail.ts","lineNumber":33,"sourceCode":"\t}\n}\n\nexport const setEmailFunction = async (email: string, user: Meteor.User | IUser) => {\n\tcheck(email, String);\n\n\tif (!settings.get('Accounts_AllowEmailChange')) {\n\t\tthrow new Meteor.Error('error-action-not-allowed', 'Changing email is not allowed', {\n\t\t\tmethod: 'setEmail',\n\t\t\taction: 'Changing_email',\n\t\t});\n\t}\n\n\tif (user.emails?.[0]?.address === email) {\n\t\treturn email;\n\t}\n\n\tif (!(await setEmail(user._id, email))) {\n\t\tthrow new Meteor.Error('error-could-not-change-email', 'Could not change email', {\n\t\t\tmethod: 'setEmail',\n\t\t});\n\t}\n\n\treturn email;\n};\n\nMeteor.methods<ServerMethods>({\n\tasync setEmail(email) {\n\t\tmethodDeprecationLogger.method('setEmail', '9.0.0', '/v1/users.updateOwnBasicInfo');\n\t\tconst user = await Meteor.userAsync();\n\n\t\tif (!user) {\n\t\t\tthrow new Meteor.Error('error-invalid-user', 'Invalid user', { method: 'setEmail' });\n\t\t}\n\n\t\treturn setEmailFunction(email, user);\n\t},","sourceCodeStart":15,"sourceCodeEnd":51,"githubUrl":"https://github.com/RocketChat/Rocket.Chat/blob/b2c16d5842cbe6b69b59bdf6fc5e5f1afcd1f0b0/apps/meteor/server/meteor-methods/users/setEmail.ts#L15-L51","documentation":"A legacy guard: thrown when setEmail(user._id, email) from server/lib/users/setEmail.ts returns falsy. In the current implementation setEmail either throws a specific error (error-invalid-email, error-field-unavailable when the address is taken, domain-blocked errors from validateEmailDomain, error-email-send-failed) or returns a truthy user object - so this branch is effectively dead code, and real failures surface as those sibling error codes instead.","triggerScenarios":"Effectively unreachable with current server code. Historically covered a generic update failure; today a failing setEmailFunction call surfaces error-field-unavailable (email already in use), a blocked-domain error, or an SMTP failure instead.","commonSituations":"Client code written against old behavior that branches on error-could-not-change-email; the actual thrown code differs, so error handling silently misses; documentation copied from legacy wikis.","solutions":["Catch and switch on the actual Meteor.Error code - most often error-field-unavailable (email taken) or a domain-validation error","Verify the new address is not used by another account first (GET /api/v1/users.info?username= or email lookup) before submitting","Treat error-could-not-change-email as an unexpected-state fallback and log err.details for diagnosis"],"exampleFix":"// before\nif (err.error === 'error-could-not-change-email') showError('Could not change email');\n\n// after\nswitch (err.error) {\n  case 'error-field-unavailable': showError('Email already in use'); break;\n  case 'error-invalid-email': showError('Invalid email'); break;\n  case 'error-email-send-failed': showError('Notification email failed'); break;\n  default: showError('Could not change email');\n}","handlingStrategy":"try-catch","validationCode":"// avoid the common cause: check the address is not used by another account first\n// GET /api/v1/users.info?userId=... or a directory search before submitting the change","typeGuard":null,"tryCatchPattern":"catch (err) {\n  if (!(err instanceof Meteor.Error)) throw err;\n  switch (err.error) {\n    case 'error-could-not-change-email': logUnexpected(err.details); break; // legacy/defensive\n    case 'error-field-unavailable': showFieldError('email', 'Email already in use'); break;\n    case 'error-invalid-email': showFieldError('email', 'Invalid email'); break;\n    default: throw err;\n  }\n}","preventionTips":["Branch on the specific sibling error codes (error-field-unavailable, error-invalid-email, error-email-send-failed), not this legacy code","Verify the new address is unique to the workspace before submitting","Keep error handling in sync with the server version - this guard is effectively unreachable in current code"],"tags":["meteor","email","account","legacy"],"backgroundTag":"email-already-in-use","analyzedSha":"b2c16d5842cbe6b69b59bdf6fc5e5f1afcd1f0b0","analyzedAt":"2026-08-18T15:26:39.429Z","contentChangedAt":"2026-08-18T15:26:39.429Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}