UseTracker and error

Hello,

I do not understand how one can handle erros with react-meteor-data, for example I have this hook

const useFavorite = ({ userId, subjectId }) => {
  return useTracker(() => {
    const sub = subCache.subscribe("favoriteOnSubject", subjectId);
    const fav = Favorites.findOne({ userId, objectId: subjectId });
    return [fav, !sub.ready()];
  }, [userId, subjectId]);
};

But if an error happen server side how can you catch it ?

Thanks

const useFavorite = ({ userId, subjectId }) => {
  return useTracker(() => {
    let sub, fav;
    try {
      sub = subCache.subscribe("favoriteOnSubject", subjectId);
      fav = Favorites.findOne({ userId, objectId: subjectId });
      return [fav, !sub.ready()];
    } catch (error) {
      // report error
      MyErrorLogger.error(error);
      return [fav, sub && !sub.ready(), error];
    }
  }, [userId, subjectId]);
};

…meaning that useFavorite returns an array of 2 or 3 elements; if there is a 3rd present, it indicates an error, so that the outer component can render a notification to the user. I find arrays as return value a bit ugly though, I suggest to return an object instead, but it’s all up to you.

You need to handle it inside the publication