Meteor-Community-Packages / meteor-collection-hooks

Meteor Collection Hooks
https://atmospherejs.com/matb33/collection-hooks
MIT License
657 stars 92 forks source link

Throw errors in hooks? #145

Closed beverlycodes closed 9 years ago

beverlycodes commented 9 years ago

Is there any way to throw an error in a hook such that the original collection method's callback will get the error? I'm hoping to perform validations in before.insert and before.update. At the moment, I can't find any way to pass validation errors up to the callback of the original insert/update call. All I can seem to do is return false in the hook, which doesn't give the caller any info about why the method failed.

markudevelop commented 9 years ago

@ryanfields found a solution for this one? +1

joelpickup commented 9 years ago

@ryanfields @voidale struggling with this as well... did either of you manage this?

matb33 commented 9 years ago

Thanks to @aramk's pull request, I've finally implemented this feature in 0.7.15

tagrudev commented 9 years ago

Here is a way to use it

MyCollection.before.update (userId, doc) ->
  new Meteor.Error("message", "The user must be logged in to post a comment")
  return false

# or this
MyCollection.before.update (userId, doc) ->
  throw new Meteor.Error("message", "The user must be logged in to post a comment")

# or this
MyCollection.before.update (userId, doc) ->
  throw new Error("Some error")