Show route on error anyway


#1

Hi all! I have route with next model:

model: function model(params) {
  this.store.unloadAll('comment');
  this.get('adapterContext').setRecipeId(params.recipe_id);
  return new _ember['default'].RSVP.hash({
    recipe: this.store.findRecord('recipe', params.recipe_id),
    comments: this.store.findAll('comment')
  });
}

In some cases request to /comments is failing. But I still want to show current route (just with some visual changes). Now I just get errors in console and transition to route failed. How can I change it?


#2

Do I get this correctly: Your Backend sometimes responds for /comments with a 500 or 404 error?

Let’s pretend you get back a 500 error sometimes, then you could do something like this:

model() {
 this.store.unloadAll('comment');
  this.get('adapterContext').setRecipeId(params.recipe_id);

let comments = this.store.findAll('comment').then((comments) => {
  return comments;
}, (response) => {
  if(response.errors[0].status === '500') {
    return null;
  } else {
    // Other errors you actually want to deal with
  }
});

  return new _ember['default'].RSVP.hash({
    recipe: this.store.findRecord('recipe', params.recipe_id),
    comments: comments
  });
}

The site would still render the template correctly, because you handled the error, while resolving the promise


#3

What a dumb I am! I totally forgot that findAll returns just a promise and model just a function. It exactly what I need. Thanks!