alexmngn
alexmngn

Reputation: 9607

Adapter error when connection is lost

When my application loses the internet connection, I get an error coming from the adapter and I'm not sure why it does that.

Error: Adapter operation failed
    at new Error (native)
    at Error.EmberError (http://localhost:5966/assets/vendor.js:25883:21)
    at Error.ember$data$lib$adapters$errors$$AdapterError (http://localhost:5966/assets/vendor.js:66151:50)
    at ember$data$lib$system$adapter$$default.extend.handleResponse (http://localhost:5966/assets/vendor.js:67455:16)

My application adapter looks like this:

export default DS.JSONAPIAdapter.extend(DataAdapterMixin, {
    host: config.apiUrl,

    handleResponse(status, headers, payload) {
        if (status === 422 && payload.errors) {
            return new DS.InvalidError(payload.errors);
        }

        return this._super(...arguments);
    }
});

The error action in my application route never gets triggered.

export default Ember.Route.extend({
    actions: {
        error(error, transition) {
            console.log(error, transition); //Never displayed
        }
    }
});

I'm making the call to the store in my controller.

export default Ember.Controller.extend({
    actions: {
        getUsers() {
            this.get('store').findAll('user').then((users) => {
                this.set('users', users);
            });
        }
    }
});

Any idea how to fix this error and trigger the error hook in my route?

Thanks

Upvotes: 1

Views: 213

Answers (1)

t-sauer
t-sauer

Reputation: 841

I think you have to catch the error yourself so it doesn't get caught by the ember data implementation.

getUsers() {
    this.get('store').findAll('user').then((users) => {
        this.set('users', users);
    }).catch((error) => {
        // Add some custom error handling or do nothing to prevent the exception from getting thrown
    });
}

Also your error hook in the route will only get fired when a promise in a transition (for example in one of the model hooks) rejects. If you have a promise in a controller you have to trigger the action/event yourself.

Upvotes: 1

Related Questions