Handling errors on meteor client side


#1

I’m currently using meteor-collection2 and I’ve a problem with errors thrown by it’s validation system.

On “lib” folder, I defined my collection, and attach a SimpleSchema to it. Then, I defined a Meteor.method :

# define methods
Meteor.methods
  # create
  RecipeCommentCreate: (options) ->
    # find recipe
    recipe = Recipes.findOne(options.recipeId)

    if !recipe
      throw new (Meteor.Error)(404, 'Recipe not found')

    # init comment
    comment =
      message: options.message
      recipe:
        _id: recipe._id

    # insert comment
    RecipeComments.insert(comment)

So, this code is running both on client-side and on server-side, for using latency compensation (and it’s perfect, because collection2 can work on both sides).

But, on calling this methode (thanks to an event on client-side) :

# call method
Meteor.call 'RecipeCommentCreate', options, (error, result) ->
  if error
    # throw error
    Errors.throw error.reason
  else
    # reset form
    form.reset()

This doesn’t work in the right way, because error in callback is only errors thrown by the server-side. Errors thrown by collection2 on the client side just make log in my browser console, and don’t stop the code :

[Log] Exception while simulating the effect of invoking 'RecipeCommentCreate'  (meteor.js, line 890)

So, if someone can help me to understand what I do in the wrong way…
Many thanks,
Yannis


#2

Actually, we just helped another guy with the same issue here: Catching meteor publish errors
rob details the correct way to do it. You’ll have to wrap it around Meteor.isServer/isClient if you’re sharing code.