Search code examples
ember.jsember-cli

Display global/misc error message to user


I'm trying to display error messages for the user in Ember. Is there a way to do this from Logger? I've read about the error substate, but I think that only displays when there's an error in a transition. Is there a way for me to set a property in the ApplicationController to the error message object (and thus display it in a template)? If there's an error in an AJAX call, or a bug, or some other issue, I want the user to be notified that something is awry.

The Coffeescript:

 Ember.Logger.error = (message, cause, stack) ->
  console.error(message, cause, stack) #show in console
  Raygun.send(new Error(message), null, { cause: cause, stack: stack }) #log error on server

  # how do I display the error message to user?, THIS DOESN'T WORK b/c "this" is undefined:
  @container.lookup('controller:main').set('errorObjetct', message)

I'm using Ember 1.11.1 in ember-cli.

Anyone have any tips on this?

Bryan


Solution

  • Thanks @blessenm for the link. It doesn't precisely apply because in Ember-CLI I don't reference the global application variable, it did remind me that the initialize function passes the "container" object. Thus, I've been able to make it work like this:

    Ember.Logger.error = (message, cause, stack) ->
      console.error(message, cause, stack) #show in console
      Raygun.send(new Error(message), null, { cause: cause, stack: stack }) #log error on server
    
      # SOLUTION:
      container.lookup('route:application').set('errorObject', message)
    

    This runs in initializers/error-handling.coffee's initialize function.