Skip to content

Instantly share code, notes, and snippets.

@phillipkregg
Last active May 2, 2018 19:26
Show Gist options
  • Save phillipkregg/6a3710e6fdce9778de0811ea778d36d0 to your computer and use it in GitHub Desktop.
Save phillipkregg/6a3710e6fdce9778de0811ea778d36d0 to your computer and use it in GitHub Desktop.
Ember Error Handling
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Basic Error Handling',
errorsArray: Ember.A([]),
displayError(error) {
this.get('errorsArray').pushObject({
message: error
});
}
});
import Ember from 'ember';
import config from './config/environment';
const Router = Ember.Router.extend({
location: 'none',
rootURL: config.rootURL
});
Router.map(function() {
this.route('login');
this.route('test-errors');
});
export default Router;
import Ember from 'ember';
export default Ember.Route.extend({
actions: {
error(error, transition) {
this.controllerFor('application').displayError(error.message);
}
}
});
import Ember from 'ember';
export default Ember.Route.extend({
});
import Ember from 'ember';
export default Ember.Route.extend({
beforeModel() {
return Ember.RSVP.reject('bad things happened!');
},
actions: {
error(error, transition) {
// Assuming we got here due to the error in `beforeModel`,
// we can expect that error === "bad things!",
// but a promise model rejecting would also
// call this hook, as would any errors encountered
// in `afterModel`.
// The `error` hook is also provided the failed
// `transition`, which can be stored and later
// `.retry()`d if desired.
debugger;
this.controllerFor('application').displayError(error);
this.transitionTo('login');
}
}
});
body {
margin: 12px 16px;
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
font-size: 12pt;
}
<h1>{{appName}}</h1>
<br>
<br>
{{outlet}}
<br>
<br>
{{link-to 'Try to go to the "test-errors" route, but get redirected to login' 'test-errors'}}
{{#if errorsArray.length}}
<div style="font-size: 20px; color: red; margin-top: 40px;">Errors!</div>
{{/if}}
<ul>
{{#each errorsArray as |error|}}
<li>{{error.message}}</li>
{{/each}}
</ul>
<h1>Redirected from "test-errors"!</h1>
<h1>Never gets here because of error and redirect</h1>
{
"version": "0.13.1",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "2.16.2",
"ember-template-compiler": "2.16.2",
"ember-testing": "2.16.2"
},
"addons": {
"ember-data": "2.16.3"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment