Publication errors, best practice?

What’s the best practice for handling publication errors and showing a meaningful error message to users?

For instance:

  1. Subscribing to data that’s not there
  2. Insufficient permissions/role
  3. Some explicit criteria like combinations of arguments or checking DB content before returning the publication

I’m looking through https://guide.meteor.com/data-loading.html but find nothing relating to this. All the examples just use this.ready() when publications “fail”.

I’m aware of the subscription onError() callback, but this plays very poorly with stuff like React/createContainer – or maybe I’m just too stupid to make it work. :slight_smile:

Any working examples or pointers would be nice, and I think that the guide should at least mention this stuff.

EDIT:
Just to clarify, my main problem is combining the React createContainer pattern with Meteor’s error handling. I can’t seem to find any good examples of this.
See my post below for example code.

1 Like

There’s a good article on handling publication errors here.

Thanks for the link, @robfallows, that’s a good article! :slight_smile:

Unfortunately it doesn’t address the issue of actually showing the error to the user.
Problem is, by the time the subscription onStop callback is fired, the view has already rendered (or failed due to lack of data).

Example:

const ViewContainer = createContainer(({params, location}) => {
    const {_id} = params;
    const {query} = location;
    const token = query && query.token;

    const subscription_handle = Meteor.subscribe('view_data', _id, token, {
        onStop(error) {
            if (error) {
                // error comes back with info from the publication:
                // 1) token is invalid
                // 2) token is expired 
                // 3) _id is non-existent.

                // I want to show this info in the view, but we have already rendered at this point.
            }
        }
    });

    const data = data_collection.findOne({_id});

    return {
        loading: !subscription_handle.ready(),
        data
    };

}, View);

Things I’ve tried:

  1. Async/await syntax: Not immediately useful, since the subscription callback is wrapped in this weird object structure. I struggled for a bit down this road, but gave up.
  2. ReactiveVar: This seemed like a pragmatic approach, but as soon as ReactiveVar got into the mix, the View component was re-rendered in an endless loop, crashing the browser.

Any help or ideas are much appreciated! :slight_smile:

More than two years later i’m stuck on the same problem. Were you able to solve it?

Best Markus