Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to throw Ember.js errors?

Tags:

ember.js

In an ember.js application, how can we throw application errors (and have them bubble through the controller-route-application hierarchy?)

My use case is related to catching non ember-data ajax errors and handling them through the same path as ember data errors.

(i.e. when experiencing an error from a non-restful endpoint, allow that error to bubble through the application similar to ember-data errors)

like image 914
Alexandros K Avatar asked Apr 16 '14 06:04

Alexandros K


2 Answers

If you want to throw errors, use throw new Error("Message");.

The user gets redirected to error route.

With Promises you can react on Exceptions, and handle them.

See: http://emberjs.com/api/classes/RSVP.Promise.html

like image 65
medokin Avatar answered Sep 19 '22 14:09

medokin


Ember has its own EmberError class which sub classes the Javascript error class.

import EmberError from '@ember/error';

export default Route.extend({
  /* Pseudo code... */
  model() {
    if(/* something bad happens */) {
      throw new EmberError('Oh, no! Something bad happened!');
    }
  },
});

You can do something with it in an error action in your route. If you want it to bubble up, remember to return true.

actions: {
  error(error) {
    // do something
    return true;
  },
},

Ember automagically creates an error substate and a error template where you can display info about the error (without redirecting). You can show error info in application-error.hbs or error.hbs like so:

<h1>{{model.name}} {{model.status}}</h1>
{{model.stack}}

See the Ember substate guide for more options.

like image 31
Rimian Avatar answered Sep 21 '22 14:09

Rimian