Skip to content

Instantly share code, notes, and snippets.

@ilovett
Last active December 30, 2015 17:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ilovett/7861344 to your computer and use it in GitHub Desktop.
Save ilovett/7861344 to your computer and use it in GitHub Desktop.
Ember.js - Improving Error.Route with more explicit error code handling
<h2>404</h2>
Sorry bra ,we couldn't find that.
var App = Ember.Application.create();
/** App.Error
*
* Extend the JavaScript Error object to have a code
* which we can pass around the app.
*
* @param {string} message A message describing the error
* @param {integer} code A generic code describing the error
*/
App.Error = function(message, code) {
this.name = 'Error';
this.message = message;
this.code = code;
this.stack = (new Error()).stack;
}
App.Error.prototype = new Error;
Generic / default error template
App.ErrorRoute = Ember.Route.extend({
'renderTemplate' : function() {
switch (this.get('controller.content.code')) {
case 404 : return this.render('404');
default : return this.render();
}
}
});
App.Translation.reopenClass({
'findOne' : function(data) {
var d = new Ember.Deferred();
Ember.$.ajax({
'url' : '/api/v1/translation/',
'context' : this,
'dataType' : 'json',
'data' : data
})
.done(function(data) {
try {
if ( ! data.objects[0])
throw new App.Error("Unable to find translation", 404);
d.resolve(App.Translation.create(data.objects[0]));
}
catch (error) {
d.reject(error);
}
})
.fail(function(data) {
d.reject();
});
return d;
}
});
App.TranslationRoute = Ember.Route.extend({
'model' : function(params) {
return App.Translation.findOne({ 'hash' : params.translation_hash });
},
'serialize' : function(model) {
"use strict";
return {
'translation_hash' : model.get('hash'),
};
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment