0

How can I check errors in my code using NVDA error log? I've created an addon, which seemingly doesn't works due to some error. How can I debug my code using error log or any other method possible?

The error I'm facing is in the following code:

import globalPluginHandler
import ui
import versionInfo
from NVDAObjects.window.scintilla  import Scintilla

class GlobalPlugin(globalPluginHandler.GlobalPlugin):
  def _messag(self, gesture):
    ui.message("notepad++ opened") # speak hello when notepad++ is the current screen

  def chooseNVDAObjectOverlayClasses(self, obj, clsList):
    if obj.windowClassName == u'Scintilla' and obj.windowControlID == 0:
       clsList.insert(0, self._messag)
  • Why isn't there a `__gestures = {...}` statement as in the developer guide example? – Gribouillis Jul 22 '17 at 08:19
  • Gesture should only be there when I am giving some input via keyboard. However, here I am speaking a message when Notepad++ becomes the active window – Hritik Gupta Jul 22 '17 at 08:34
  • In the developer's guide, they insert a class in `clsList` instead of a bound method. Can't you try to do the same? Also, if nothing happens, it could be because the callback is not bound to an event. – Gribouillis Jul 22 '17 at 09:52
  • 1
    Thanks a lot. Working now :) – Hritik Gupta Jul 22 '17 at 10:51

1 Answers1

0

I don't know anything about NVDA, but when python programs fail, they do it by raising an exception. You could perhaps include a mechanism to catch these exceptions and send them to a file. For example let's take a plugin from the NVDA developer's guide

--- start ---
# Version announcement plugin for NVDA
# Developer guide example 2

import globalPluginHandler
import ui
import versionInfo

class GlobalPlugin(globalPluginHandler.GlobalPlugin):

    @MY_EXCEPTION_CATCHER
    def script_announceNVDAVersion(self, gesture):
        ui.message(versionInfo.version)

    __gestures={
        "kb:NVDA+shift+v": "announceNVDAVersion",
    }

--- end ---

I added a decorator to the code that catches the exceptions that may occur and displays them in an html file that can be displayed in browser for example

import functools
import cgitb
import sys

def MY_EXCEPTION_CATCHER(func):
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        try:
            return func(*args, **kwargs)
        except Exception:
            with open('exception.html', 'w') as outfile:
                outfile.write(cgitb.html(sys.exc_info())
            raise
    return wrapper

I hope it helps.

Gribouillis
  • 2,230
  • 1
  • 9
  • 14
  • What didn't work? You may need to adapt the path to the exception.html file, or apply the decorator to other functions in the plugin's class. – Gribouillis Jul 22 '17 at 08:21
  • Tried that. But the raise and return keywords become unusually highlighted in the editor. Also after running the code, I see no difference. – Hritik Gupta Jul 22 '17 at 10:34
  • The only difference, if there was an exception during the decorated callback, would be that you have a file `exception.html` somewhere (you could give a full path for this file). Also if you want to check that the callback gets actually executed, you can try to raise an exception in the callback itself, such as `raise RuntimeError('Hello there')` – Gribouillis Jul 22 '17 at 10:40