Can I use mapEvent with UncaughtErrorEvent.UNCAUGHT_ERROR?

Tim's Avatar

Tim

11 Oct, 2012 10:09 AM

Hi,
I was wondering if there is a way I can listen for global errors in my app using flash's UncaughtErrorEvent.UNCAUGHT_ERROR event?
It needs to be listening to the loaderInfo like so:

loaderInfo.uncaughtErrorEvents.addEventListener( UncaughtErrorEvent.UNCAUGHT_ERROR, uncaughtErrorHandler);

How could I do this within the Robotlegs framework?

Cheers,
Tim

  1. 1 Posted by Tim on 11 Oct, 2012 10:10 AM

    Tim's Avatar

    P.S.
    I assume adding this in my MainContext won't work:
    commandMap.mapEvent(UncaughtErrorEvent.UNCAUGHT_ERROR, DisplayErrorCommand);

  2. Support Staff 2 Posted by Ondina D.F. on 11 Oct, 2012 10:53 AM

    Ondina D.F.'s Avatar

    Hi Tim,

    „ Commands are only executed in response to framework events“

    Details here:

    https://github.com/robotlegs/robotlegs-framework/wiki/best-practice...

    In the uncaughtErrorHandler you should dispatch a custom event (say DisplayErrorEvent) with UncaughtErrorEvent’s event.error as a payload.

    private function uncaughtErrorHandler(event:UncaughtErrorEvent):void
    {
       dispatchEvent(new DisplayErrorEvent(DisplayErrorEvent.SOME_ERROR, event.error));
    }
    

    Mapping the custom event:

    commandMap.mapEvent(DisplayErrorEvent.SOME_ERROR, DisplayErrorCommand);
    

    If your Loader is in a View, then the Mediator of this View should listen for DisplayErrorEvent and redispatch it, in order to trigger DisplayErrorCommand.

    You’ll have to inject DisplayErrorEvent into your DisplayErrorCommand, so you can use its payload.

    Does this help?

    Ondina

  3. 3 Posted by Tim on 11 Oct, 2012 11:16 AM

    Tim's Avatar

    Thanks Ondina,

    That sounds like a good solution - so in the view I would listen for the actual Flash event then in its view mediator I would re-dispatch it into the RobotLegs framework event chain something like:

    override public function onRegister():void
    {

    view.addEventListener(DisplayErrorEvent.SOME_ERROR, onUnCaughtError, false);

    }

    private function onUnCaughtError(e:DisplayErrorEvent):void

        {
            //re-dispatch event
    

    eventDispatcher.dispatchEvent(e);

        }
    
  4. Support Staff 4 Posted by Ondina D.F. on 11 Oct, 2012 11:40 AM

    Ondina D.F.'s Avatar

    My pleasure, Tim.

    so in the view I would listen for the actual Flash event then in its view mediator I would re-dispatch it

    Exactly.

    My suggestion is to use the eventMap to map the listeners in your Mediator:
    - with a handler like in your example

    override public function onRegister():void
    {
       eventMap.mapListener(view, DisplayErrorEvent.SOME_ERROR, onUnCaughtError, DisplayErrorEvent);
      //or:
       // addViewListener(DisplayErrorEvent.SOME_ERROR, onUnCaughtError, DisplayErrorEvent);
    }
    

    -without a handler method (the event will be redispatched automatically)

    eventMap.mapListener(view, DisplayErrorEvent.SOME_ERROR, dispatch, DisplayErrorEvent);

    More details about the advantages of using the EventMap in my response here:

    http://knowledge.robotlegs.org/discussions/problems/644-events-bein...

  5. 5 Posted by Tim on 11 Oct, 2012 01:24 PM

    Tim's Avatar

    Thanks again Ondina.
    I didn't realise you could use the eventMap.mapListener with a view instance as the first argument - I have always used it as addViewListener for listening to view events and the eventMap.mapListener for listening for other "external" events to the view.

    What I have now is in my view class:

    public function MainView()
    {
    loaderInfo.uncaughtErrorEvents.addEventListener( UncaughtErrorEvent.UNCAUGHT_ERROR, onUnCaughtErrorHandler);
    }
            
    protected function onUnCaughtErrorHandler(event:UncaughtErrorEvent):void
    {
                var errorEvent:AppEvent = new AppEvent(AppEvent.STATUS_ERROR);
                errorEvent.messageStr = "Uncaught error - id: "+ event.errorID;
                this.dispatchEvent(errorEvent);
    }
    

    And then in its mediator class

    override public function onRegister():void
    {
                //Listen for uncaught errors
                eventMap.mapListener(view, AppEvent.STATUS_ERROR, onUnCaughtError, AppEvent);
    }
            
    private function onUnCaughtError(e:AppEvent):void
    {
                // Re-dispatch event
                dispatch(e);
                
    }
    

    Or do I need to actually re-dispatch the event literally in the mediator's handler using a new AppEvent instance (I assume this might clone the event rather than passing the same one along)?

  6. Support Staff 6 Posted by Ondina D.F. on 11 Oct, 2012 02:40 PM

    Ondina D.F.'s Avatar

    You’re welcome, again.

    Or do I need to actually re-dispatch the event literally in the mediator's handler using a new AppEvent instance (I assume this might clone the event rather than passing the same one along)?

    Nope. If your custom event overrides clone(), then it will be re-dispatched.

    I’m sure you know how to create a custom event class, but just for the sake of completeness I’m adding a code snippet :)

    Custom event:

    public class SomeErrorEvent extends Event
        {
            public static const STATUS_ERROR:String = "statusError";
    
            public function SomeErrorEvent(type:String, someMessage:String, bubbles:Boolean = false, cancelable:Boolean = false)
            {
                super(type, bubbles, cancelable);
                _someMessage = someMessage;
            }
    
            private var _someMessage:String;
    
            public function get someMessage():String
            {
                return _someMessage;
            }
    
            override public function clone():Event
            {
                return new SomeErrorEvent(type, someMessage, bubbles, cancelable);
            }
        }
    

    In your View:

    protected function onUnCaughtErrorHandler(event:UncaughtErrorEvent):void
    {
       var errorMessage:String= "Uncaught error - id: "+ event.errorID;
       dispatchEvent(new SomeErrorEvent(SomeErrorEvent.STATUS_ERROR, errorMessage));
    
    }
    

    In your Mediator, as I said in my previous post, you can do just this:
    1.

    eventMap.mapListener(view, SomeErrorEvent.STATUS_ERROR, dispatch, SomeErrorEvent);
    

    or its equivalent:

    addViewListener(SomeErrorEvent.STATUS_ERROR, dispatch, SomeErrorEvent);
    

    or, as you did:
    2.

    eventMap.mapListener(view, SomeErrorEvent.STATUS_ERROR, onUnCaughtError, SomeErrorEvent);
    

    or its equivalent:

    addViewListener(SomeErrorEvent.STATUS_ERROR, onUnCaughtError, SomeErrorEvent);
    
    private function onUnCaughtError(event: SomeErrorEvent):void
    {
        dispatch(event);
    }
    

    Take a look at https://github.com/robotlegs/robotlegs-framework/blob/master/src/or...
    to see addViewListener and addContextListener as syntactical sugar for mapping listeners.

    Did I answer all your questions?

  7. 7 Posted by Tim Johnson on 12 Oct, 2012 11:45 AM

    Tim Johnson's Avatar

    Hi Ondina,
    Thanks for explaining it so clearly. Yes I do know how to create custom events but thanks for the refresher.

    I had missed the bit about using dispatch in your eventMap.mapListener - very handy. My way is the long way! ;)

    About RobotLegs 2 - would you recommend using it yet or is there a steep learning curve? Where can I find some good information on getting started or is not quite ready yet?

    Cheers,
    Tim

  8. Support Staff 8 Posted by Ondina D.F. on 12 Oct, 2012 12:48 PM

    Ondina D.F.'s Avatar

    Hey Tim!

    About RobotLegs 2 - would you recommend using it yet or is there a steep learning curve?

    I’ve ported 3 big projects from rl1 to rl2 already, and I also have a few new projects, where I used rl2 from the start.
    All of them work without problems.
    I don’t use all rl2’s new features, but the ones I was most interested in, Modular and Popups, are really a win.

    So, yes, I recommend using robotlegs 2 :)

    Where can I find some good information on getting started or is not quite ready yet?

    Go through the readme files under each folder:
    https://github.com/robotlegs/robotlegs-framework/tree/version2/src

    Look at the examples:
    http://knowledge.robotlegs.org/discussions/examples/20-links-to-rob...

    Try to build a simple application following the examples.

    When in doubt, ask questions in here (actually under Robotlegs 2).

    Read through the Robotlegs 2 discussions.

    Cheers,
    Ondina

  9. Ondina D.F. closed this discussion on 15 Oct, 2012 08:39 AM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac