0 Replies Latest reply on Nov 2, 2012 4:02 AM by Gerbrand Stap

    Unhandled Exceptions

    Gerbrand Stap

      Creating a bug-free application is a hard thing to achieve and any bugs that remain in an application after release will always have a negative effect on the user experience. End users are always very capable in finding these remaining bugs, but won't always bother to report these bugs because the bug is not annoying enough compared to the hassle of contacting the software vendor and explaining the bug. That is where automatic bug reporting comes in handy, which consists mostly of the following steps:

      • Catch an unhandled exception
      • Gather information about the crash
      • Notify user and ask for permission to send data
      • Submit bug in bug-tracking software

       

      The latter two depend on your type of application and the bug-tracking software you use. The first two are specific to the platform on which the application is developed, and here it is explained how to do it in RhoMobile.

       

      Catching an unhandled exception

      Catching an unhandled exception in RhoMobile is not a documented feature. After some debugging an unhandled exception, I noticed that all request are routed through the Rho::RhoApplication.serve method. So it is possible to redefine this function in your own Application class (in application.rb) and insert error handling logic:

       

      def serve( req, res )
        begin
          super
        rescue Exception => ex
          # insert handling logic here
          raise
        end
      end
      

       

      The shown method changes nothing to the application, because 'super' is called and an Exception is re-raised. But all exceptions caused by requests from the UI can be handled here, which are probably most of them. Any exceptions raised in background processes are not caught here, so other approaches are needed to catch those.

       

      Gather information about the crash

      The req argument is very helpful in gathering information about the crash. It will contain a hash with data similar to:

       

      {
        "application"=>"app",
        "model"=>"Setting",
        "action"=>"save_connection",
        "request-method"=>"POST",
        "request-uri"=>"/app/Setting/save_connection",
        "request-query"=>"",
        "headers"=>{
          "Origin"=>"http://127.0.0.1:52268",
          "X-Requested-With"=>"XMLHttpRequest",
          "User-Agent"=>"Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/533.3 (KHTML, like Gecko) RhoSimulator Safari/533.3",
          "Content-Type"=>"application/x-www-form-urlencoded",
          "Accept"=>"*/*",
          "Referer"=>"http://127.0.0.1:52268/app/Login",
          "Content-Length"=>"48",
          "Connection"=>"Keep-Alive",
          "Accept-Encoding"=>"gzip",
          "Accept-Language"=>"nl-NL,en,*",
          "Host"=>"127.0.0.1:52268"
        },
        "request-body"=>"ssl=off&hostname=localhost&folder=RhoApp",
        :modelpath=>"C:/Ontwikkeling/workspace/Connect-It_Mobile_Client/app/Setting/"
      }
      

       

      The requested controller is available and all parameters passed in either the request-uri (GET) or request-body (POST). The exception will contain a clue of what went wrong and a backtrace of the methods that were called.

      The response won't contain much useful information since processing the request caused the exception.

      Further information that might be helpful are:

      • System.get_property( 'platform' ) - The platform (android, iOS, etc.)
      • System.get_property( 'device_name' ) - The type of device
      • System.get_property( 'os_version' ) - The OS version