Application Crashing: Exited with code: 7 when throwing Meteor.Error() from Function Called from Method

I am trying to upgrade my application from V2 to V3, however whenever I throw a Meteor.Error() from another file, the entire application crashes, rather than just returning the error to the client.

Client calls getAnnouncement method:

	getAnnouncement: async function({ announcementID }) {
		this.unblock();
		check(announcementID, String);

		Security.checkLoggedIn(this.userId);
		Security.hasRole(this.userId, [
			"supplier_community_read"
			//"retailer_community_read"
		]);

		return await AnnouncementService.getAnnouncement(
			announcementID,
			this.userId
		);
	},

The Security class methods, checkLoggedIn and hasRole should run to validate the user permissions:

	static async hasRole(userId, role) {
		if (Meteor.isTest) return true;
		const auth = await Roles.userIsInRoleAsync(userId, role);
		if (!auth) {
			throw new Meteor.Error(403, "[Security.hasRole] Not Authorized");
		}
		return auth;
	}

Rather than the method returning the thrown error from the Security.hasRole method, the application is just crashing:

W20250322-12:27:18.722(-5)? (STDERR) errorClass [Error]: [Security.hasRole] Not Authorized [403]
W20250322-12:27:18.722(-5)? (STDERR)     at Function.hasRole (imports/api/services/Security/index.js:17:10)
W20250322-12:27:18.723(-5)? (STDERR)     at processTicksAndRejections (node:internal/process/task_queues:105:5) {
W20250322-12:27:18.723(-5)? (STDERR)   isClientSafe: true,
W20250322-12:27:18.723(-5)? (STDERR)   error: 403,
W20250322-12:27:18.723(-5)? (STDERR)   reason: '[Security.hasRole] Not Authorized',
W20250322-12:27:18.723(-5)? (STDERR)   details: undefined,
W20250322-12:27:18.723(-5)? (STDERR)   errorType: 'Meteor.Error'
W20250322-12:27:18.723(-5)? (STDERR) }
W20250322-12:27:18.723(-5)? (STDERR)
W20250322-12:27:18.723(-5)? (STDERR) Node.js v22.13.1
=> Exited with code: 7
=> Your application is crashing. Waiting for file change.

In V2, when calling the Security methods if the user did not have the correct permissions, the error would be thrown to the client. App would not crash.

In V3, when calling the Security methods if the user does not have the correct permissions, the error is being thrown to the terminal and the App is crashing.

Has anyone experienced a similar issue?

this.unblock();

I think it is no longer required in V3 since it is a Fiber thing (meteor/packages/ddp-server/livedata_server.js at 618e5a24baf8948323414b44ab5e686311055c04 · meteor/meteor · GitHub).

Ok, Security.hasRole is an async function inside and async function. In order to reach to the final return of getAnnouncement you need the resolutions of the async functions Security.xxxxx.
I think you need to await the Security function. What happens there is that you throw an error into a function and the resolution of that function is not “bubbling up” to the method level because you don’t await for a return or an error.

Can you try 2 things and let’s see if anything changes:

const auth = await Security.hasRole(....)
 console.log(auth)

and

final return of your method, will return the return in 'then'
return await Security.hasRole(this.userId, ["supplier_community_read"])
  .then(auth => {
     if (auth) {
       return await AnnouncementService.getAnnouncement(
			announcementID,
			this.userId
		)
     }
   })
   .catch(err => console.log(err)) // you are catching at the method level the error thrown in a function in the depth of your function.


1 Like

Fixed it by awaiting the Security method call.

await Security.hasRole()

You were correct on it not “bubbling up”.

Thanks!