Skip to content

Instantly share code, notes, and snippets.

@pixelhandler
Last active September 15, 2015 02:37
Show Gist options
  • Save pixelhandler/8856330 to your computer and use it in GitHub Desktop.
Save pixelhandler/8856330 to your computer and use it in GitHub Desktop.
Handle 404 with Ember Data, pseudo code: see route.js, application_adapter.js, router.js (given a response.status = 404) the router would redirect to a 404 route.
App.ApplicationAdapter = DS.RESTAdapter.extend({
ajaxError: function(jqXHR) {
var error = this._super(jqXHR),
errors = {};
if (jqXHR && jqXHR.status === 422) {
var jsonErrors = Ember.$.parseJSON(jqXHR.responseText)["errors"];
forEach(Ember.keys(jsonErrors), function (key) {
errors[Ember.String.camelize(key)] = jsonErrors[key];
});
return new DS.InvalidError(errors);
} else if (jqXHR && jqXHR.status === 404) {
errors['404'] = 'Not Found';
return new DS.InvalidError(errors);
} else {
return error;
}
}
});
App.ThingDetailRoute = Em.Route.extend({
model: function(params, transition) {
return this.store.find('thing', params.thing_id).then(
(function(_this) {
return function(model) {
return _this.store.push('thing', model);
};
})(this),
(function(_this) {
return function(invalid) {
if (invalid.errors && invalid.errors['404']) {
return _this.transitionTo('not-found', invalid);
}
};
})(this));
}
});
App.Router.map(function() {
this.resource('thing', {path: '/thing'}, function() {
this.route('detail', {path: '/:thing_id'});
});
this.route('not-found', {path: '/*path'});
});
@shadow-fox
Copy link

Shouldn't you handle the error part in the route's action hooks already baked error action ?

@pixelhandler
Copy link
Author

@shadow-fox yeah that is an option too: http://emberjs.com/guides/routing/loading-and-error-substates/#toc_code-error-code-substates ; could use a specific ErrorRoute too

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment