ember.js - EmberJS / Ember-Data: 404's Not Getting Caught -
i trying catch 404 errors in ember app, , redirect /not-found
.
i have errors
action on applicationcontroller
, , have rsvp.on('error')
function 404's aren't getting caught. 404 error thrown console jquery, error not getting passed error handler.
errors initializer:
import ember 'ember'; var initialize = function(container) { var errorreporting = container.lookup("service:errorreporting"); ember.rsvp.on('error', function(err) { ember.warn("ember.rsvp error..... logging error:"); console.log(err); if (err.name && err.name === 'transitionaborted') { ember.debug("transitionaborted error. doesn't should catching these."); } else { container.lookup('route:application').send('error', err); } }); window.onerror = function(err) { // window general errors. ember.warn("uncaught error (tripped window.onerror)..... logging error:"); console.log(err); errorreporting.report(err); }; }; export default { name: 'errors', initialize: initialize };
the error action on applicationroute
huge (and can post it), doesn't seem getting called.
edit 1: route code
import ember 'ember'; import authenticatedroutemixin 'simple-auth/mixins/authenticated-route-mixin'; export default ember.route.extend(authenticatedroutemixin, { titletoken: function(model) { return model.get('name'); }, model: function(params) { return this.store.find('location', params.location_id); } });
edit 2: applicationroute / error handler
error: function(err, transition) { if (!ember.isnone(transition)) { transition.abort(); } let errorholder = this._geterrordatafrom(err); let errormessage = this._geterrormessagefrom(errorholder); let isfourohfour = (typeof(err.status) !== 'undefined' && err.status === 404) || errorholder.reason === 'not_found'; if (isfourohfour) { return this.transitionto('not-found'); } let requireauthentication = (errorholder.reason === 'not_authenticated'); if (requireauthentication) { window.localstorage.setitem('toast-on-reload', errorholder.message); return this.session.invalidate(); } let isvalidationerror = ( errorholder.reason === "validation_error" || ( !ember.isnone(errorholder.errors) && !ember.isnone(errorholder.message) ) ); if (isvalidationerror) { this.toast.error(errormessage); return; } let verificationrequired = (errorholder.reason === "verification"); if (verificationrequired) { this.toast.error(errormessage); return this.transitionto('verification'); } let invalidrequest = (errorholder.reason === 'unprocessable_entity'); if (invalidrequest) { this.toast.error(errormessage); return; } this.errorreporting.report(errorholder); this.toast.error(errormessage); return this.transitionto('error'); } }, _geterrordatafrom: function(obj) { if (!ember.isnone(obj.responsejson)) { return obj.responsejson; } else if ( !ember.isnone(obj.success) || !ember.isnone(obj.errors)) { return obj; } else if (!ember.isnone(obj.jqxhr) && !ember.isnone(obj.jqxhr.responsejson)) { return obj.jqxhr.responsejson; } else { ember.warn("no error handler available, using default ( {} ). error:"); console.log(obj); return {}; } }, _geterrormessagefrom: function(errorholder) { if ( typeof(errorholder.errors) === 'object' && !ember.isnone(errorholder.errors.message) ) { return errorholder.errors.message; } else if (!ember.isnone(errorholder.errors)) { return errorholder.errors; } else if (!ember.isnone(errorholder.message)) { return errorholder.message; } else { return "sorry, went wrong."; } }
if want use error
event, place handler inside actions
hash in application route.
alternatively, consider use of error route. can define in pods/application/error
, templates, routes, , controllers other route. see http://guides.emberjs.com/v1.10.0/routing/loading-and-error-substates/#toc_code-error-code-substates. error code passed error route model.
finally, in many cases it's simple , reliable catch
error find
.
model: function(params, transition) { return this.store.find('location', params.location_id) . catch(err => this.send('ajaxerror', err)); }
then define ajaxerror
action on application route same kinds of things doing in error
hook now. however, catch ajax errors, not other sorts of errors might occur during transitions, , swallowed (or in case reported ember.rsvp.on('error'
).
Comments
Post a Comment