4 Replies Latest reply on Jun 15, 2010 5:55 PM by areohbee

    Errors in button actions and observer functions don't display dialogs?

    johnrellis Most Valuable Participant

      In LR 3 SDK, it appears that errors don't display the error dialog if they occur in button-action functions or observer functions invoked as the result of some user action on a control. This makes debugging quite tedious.  I have a solution, but it seems heavy handed and I'm wondering if I'm missing something simple?

       

      For example, in Sample Plugins\helloworld.lrdevplugin\CustomDialogWithObserver.lua, modify "myCalledFunction" to raise an error:

       

          local function myCalledFunction()
              outputToLog( "props.myObservedString has been updated." )
              error "Example of an internal error"

      The error will cause the observer function to terminate immediately without displaying the error dialog.

       

      My heavy-handed solution is to wrap all button and observer functions with LrFunctionContext.callWithContext() and use LrDialogs.attachErrorDialogToFuntionContext.  To make that easy, I use this utility function to wrap any function with callWithContext():

       

      local function showErrors (func)
          return function (...)
              LrFunctionContext.callWithContext("wrapped",
                  function (context)
                      LrDialogs.attachErrorDialogToFunctionContext (context)
                      func (unpack (arg))
                      end)
              end
          end

      For example, here's how to wrap the action function of a button:

       

           viewFactory:push_button {title = "Search", action = showErrors (searchPush)}

       

      Is there an easier way, e.g. some call or configuration setting I've missed?

        • 1. Re: Errors in button actions and observer functions don't display dialogs?
          johnrellis Most Valuable Participant

          The same issue also occurs for validate functions of controls.

          • 2. Re: Errors in button actions and observer functions don't display dialogs?
            areohbee Level 5

            This was the case in 2.0 as well. One work-around is to wrap the function context at the outermost level. For example, if you wrap the entire "sectionsForTopOfDialog" function in a context, then you don't have to worry about various called functions... - this is what I just started doing, but I confess I haven't specifically checked whether the context is inherited by called functions as well - maybe you can tell me/us.

             

            I'm not sure why these functions don't just have a default context, like the export functions do...

             

            Rob

            • 3. Re: Errors in button actions and observer functions don't display dialogs?
              johnrellis Most Valuable Participant
              function(){return A.apply(null,[this].concat($A(arguments)))}

              One work-around is to wrap the function context at the outermost level. For example, if you wrap the entire "sectionsForTopOfDialog" function in a context, then you don't have to worry about various called functions... - this is what I just started doing, but I confess I haven't specifically checked whether the context is inherited by called functions as well - maybe you can tell me/us.

              In the "helloworld" sample plugin, the custom dialog defined in CustomDialogWithObserver.lua already wraps the entire creation of the dialog with callWithContext() (it needs to do that so it can create an observable property table).  But that doesn't have any effect on the callback functions for observers, control actions, and validate functions.  In that example, is there any other way to wrap "everything"?

              • 4. Re: Errors in button actions and observer functions don't display dialogs?
                areohbee Level 5

                Hi John,

                 

                I've noticed the same thing - and I had a similar issue with the "catalog:withWriteAccessDo" function (operates in a separate task). The button handlers and such don't seem to inherit context.

                 

                I don't know a way around other than wrapping each one individually - maybe Eric Scouten or one of the other SDK gurus will enlighten us.

                 

                Rob