7 Replies Latest reply on Mar 4, 2016 9:20 AM by johnrellis

    LrSocket bind

    kimaldis Level 1

      I have a function that creates an LrSocket for receiving data. All works fine. Within the onMessage callback I'm using loadstring() to run some Luacode, as below. This also works fine until there's a syntax error in thecodestring passed to loadstring(). Normally, ouside of onMessage, there'd be some kind of  error message but within onMessage, it just dies quietly. I'd like to have some indication of where the error is. I've tried using pcall() and I've tried wrapping it in an assert(). Neither works. I wondered if anybody maybe had any thoughts.


                  onMessage = function( socket, message )
                      local code = "LrDialogss.showBezel( 'Test Message', 5 )"  -- deliberate syntax error
                      local f = assert( loadstring( code )  )
                      Debug.logn( "Done Loadstring" )
                      Debug.logn( "Done Function Call" ) -- never gets here if error in 'code'
        • 1. Re: LrSocket bind
          johnrellis Most Valuable Participant

          Try using LrTasks.pcall() instead of the standard Lua pcall().

          • 2. Re: LrSocket bind
            kimaldis Level 1

            afraid not. Same thing exactly. It's beginning to look like a bug, I think.

            • 3. Re: LrSocket bind
              Griffel Level 1

              I assume, running into a syntax error stops execution of the script. So the line with assert will not be reached.

              • 4. Re: LrSocket bind
                kimaldis Level 1

                No. The line    "local f = assert( loadstring( code )  )"  runs and I get a log from the first Debug.logn line. It's the second Debug.logn that it never gets to.It's dying somewhere in f().


                Changing it to assert( f() ) doesn't work. Neither does add pcall().

                • 5. Re: LrSocket bind
                  johnrellis Most Valuable Participant
                  It's dying somewhere in f().

                  Oh, I misunderstood that.  So the compilation of the string by loadstring() is succeeding, it's the execution of the resulting function that's failing.  To avoid further ambiguity, when you invoke f() with exactly the following lines:


                  local success, result = LrTasks.pcall (f)

                  Debug.logn ("pcall", success)


                  What happens?  Note that a common mistake, which I've made a number of times, is to use pcall like this: LrTasks.pcall (f()).  That will have no effect.

                  • 6. Re: LrSocket bind
                    kimaldis Level 1

                    That did it. Yes, I was using pcall( f() ). Which in hindsight was pretty dumb.


                    Now I'm getting some error messages back and I can actually debug my code


                    Thanks for your help, John. Appreciated.

                    • 7. Re: LrSocket bind
                      johnrellis Most Valuable Participant

                      One other thing occurred to me: When an error occurs in many of the callback functions a plugin passes to the SDK API, the error is by default silently ignored.  I can never remember for which callbacks this happens, so I make it a habit to wrap all callbacks with Debug.showErrors.  E.g.


                      LrSocket.bind {...

                      onMessage = Debug.showErrors (function (socket, message



                      I think it's a pretty good bet that it you do this, then you won't need the pcall().   I didn't notice this originally, because I wasn't very familiar with LrSocket.