{"record":{"id":"127133de796286cc","repo":"RocketChat/Rocket.Chat","slug":"error-as-any-message","errorCode":null,"errorMessage":"(error as any)?.message","messagePattern":"\\(error as any\\)\\?\\.message","errorType":"exception","errorClass":"Meteor.Error","httpStatus":null,"severity":"error","filePath":"apps/meteor/server/lib/rooms/addUserToRoom.ts","lineNumber":64,"sourceCode":"\n\tif (!userToBeAdded) {\n\t\tthrow new Meteor.Error('user-not-found');\n\t}\n\n\tif (\n\t\t!(await roomDirectives.allowMemberAction(room, RoomMemberActions.JOIN, userToBeAdded._id)) &&\n\t\t!(await roomDirectives.allowMemberAction(room, RoomMemberActions.INVITE, userToBeAdded._id))\n\t) {\n\t\treturn;\n\t}\n\n\ttry {\n\t\tconst inviterUser = inviter && ((await Users.findOneById(inviter._id)) || undefined);\n\t\t// Not \"duplicated\": we're moving away from callbacks so this is a patch function. We should migrate the next one to be a patch or use this same patch, instead of calling both\n\t\tawait beforeAddUserToRoomPatch([userToBeAdded.username!], room, inviterUser);\n\t\tawait beforeAddUserToRoom.run({ user: userToBeAdded, inviter: inviterUser }, room);\n\t} catch (error) {\n\t\tthrow new Meteor.Error((error as any)?.message);\n\t}\n\n\t// TODO: are we calling this twice?\n\n\tawait callbacks.run('beforeAddedToRoom', { user: userToBeAdded, inviter });\n\n\ttry {\n\t\tawait Apps.self?.triggerEvent(AppEvents.IPreRoomUserJoined, room, userToBeAdded, inviter);\n\t} catch (error: any) {\n\t\tif (error.name === AppsEngineException.name) {\n\t\t\tthrow new Meteor.Error('error-app-prevented', error.message);\n\t\t}\n\n\t\tthrow error;\n\t}\n\n\t// for federation rooms we stop here since everything else will be handled by the federation invite flow\n\tif (isRoomNativeFederated(room)) {","sourceCodeStart":46,"sourceCodeEnd":82,"githubUrl":"https://github.com/RocketChat/Rocket.Chat/blob/b2c16d5842cbe6b69b59bdf6fc5e5f1afcd1f0b0/apps/meteor/server/lib/rooms/addUserToRoom.ts#L46-L82","documentation":"addUserToRoom re-wraps any error thrown by the beforeAddUserToRoom callback chain (the patch function plus callbacks) into a new Meteor.Error whose code is the original error's message. The resulting error code is therefore whatever a callback threw (e.g. 'error-invalid-user', 'unauthorized', or a custom string), and the original error's structure and details are lost.","triggerScenarios":"Any 'beforeAddUserToRoom' callback (core patches blocking banned/deactivated users, apps, custom server code) throws during addUserToRoom; its message surfaces as the outer Meteor error code.","commonSituations":"Developers puzzled by unexpected error codes when adding users; custom callbacks denying membership; debugging which callback vetoed the add.","solutions":["Search for code registering on 'beforeAddUserToRoom' (callbacks.add / patch functions) to find which one throws.","Fix the underlying denial that the callback reports.","When catching, inspect err.error - it carries the callback's original message; consider an upstream fix to preserve the error object."],"exampleFix":"// before: callback throws plain errors that become opaque codes\nthrow new Error('user is banned');\n\n// after: throw a Meteor.Error so the outer wrap keeps a stable code\nthrow new Meteor.Error('error-user-banned', 'User is banned');","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  await addUserToRoom(rid, user, inviter);\n} catch (error: any) {\n  // error.error here is the *message* thrown by a beforeAddUserToRoom callback.\n  // Log error.error and error.reason, then inspect registered 'beforeAddUserToRoom'\n  // callbacks/patches to find which one vetoed the add.\n}","preventionTips":["In your own 'beforeAddUserToRoom' callbacks, throw Meteor.Errors with stable codes - they become the outer error code here.","Audit registered callbacks on 'beforeAddUserToRoom' when odd error codes appear.","Do not rely on structured details from the wrapped error - this wrap drops them."],"tags":["callbacks","rooms","error-wrapping","debugging"],"backgroundTag":"callback-chain-error","analyzedSha":"b2c16d5842cbe6b69b59bdf6fc5e5f1afcd1f0b0","analyzedAt":"2026-08-18T15:26:39.429Z","contentChangedAt":"2026-08-18T15:26:39.429Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}