Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nice error on nested triggers #348

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ldanilek
Copy link
Contributor


By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

}),
).rejects.toThrow(
new RegExp(
`Triggers\\.wrapDB called multiple times in a single mutation\\.\\s+` +
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I'd be less aggressive on the regex because I find it annoying to have to update the test whenever the error message changes. Maybe just checking the first sentence + checking a link to docs shows up in there somewhere?

@@ -90,6 +90,15 @@ export class Triggers<
}

wrapDB = <C extends Ctx>(ctx: C): C => {
if (wrapDBCalled) {
throw new Error(
`Triggers.wrapDB called multiple times in a single mutation. Not allowed due to potential deadlock.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this could merit a specific example of what this looks like, maybe just in the triggers docs? And we could link to it from the error message?

Like

Troubleshooting: Triggers.wrapDB called multiple times in a single mutation

This usually happens if a mutation is directly calling another mutation, for instance:

export const joinTeam = mutation(...)

export const createTeam = mutation({
	handler: (ctx) => {
		const currentUserId = // load from auth
		const team = await ctx.db.insert("teams", { creatorId: currentUserId, ... })
		// ❌ This creates a new `Triggers` since it's calling another mutation
		await joinTeam({ teamId })
	}
})

See for the recommended pattern.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants