Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added exception info to be inherited in pyautogui ImageNotFoundException #887

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

gtmsingh
Copy link

I tried using pyautogui.locateCenterOnScreen and handled exception to debug, but found that the exception message being passed from pyscreeze.ImageNotFoundException is lost in the decorator.

The code changes in this PR allows the info provided in the pyscreeze exception to bubble up the wrapper for better debugging in application side.

Tests ran locally for validation

Test case 1: Using pyscreeze API directly

Code:

try:
    return pyscreeze.locateCenterOnScreen(*args, minSearchTime=1, **kwargs), None
except pyscreeze.ImageNotFoundException as ex:
    logger.exception("Error in locating image on screen", extra={"error": ex})

Snippet of Log output:

"exc_info": [
  "<class 'pyscreeze.ImageNotFoundException'>",
  "Could not locate the image (highest confidence = 0.912)",
  "File \"<path-to-repo>\\util\\autogui.py\", line 87, in locateCenterOnScreen\n    return pyscreeze.locateCenterOnScreen(*args, minSearchTime=1, **kwargs), None\n           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n  File \"<path-to-repo>\\.venv\\Lib\\site-packages\\pyscreeze\\__init__.py\", line 447, in locateCenterOnScreen\n    coords = locateOnScreen(image, **kwargs)\n             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n  File \"<path-to-repo>\\.venv\\Lib\\site-packages\\pyscreeze\\__init__.py\", line 405, in locateOnScreen\n    retVal = locate(image, screenshotIm, **kwargs)\n             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n  File \"<path-to-repo>\\.venv\\Lib\\site-packages\\pyscreeze\\__init__.py\", line 383, in locate\n    points = tuple(locateAll(needleImage, haystackImage, **kwargs))\n             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n  File \"<path-to-repo>\\.venv\\Lib\\site-packages\\pyscreeze\\__init__.py\", line 257, in _locateAll_opencv\n    raise ImageNotFoundException('Could not locate the image (highest confidence = %.3f)' % result.max())"
],

Test case 2: Using current pyautogui API

Code:

try:
    return pyautogui.locateCenterOnScreen(*args, minSearchTime=1, **kwargs), None
except pyautogui.ImageNotFoundException as ex:
    logger.exception("Error in locating image on screen", extra={"error": ex})

Snippet of Log output:

"exc_info": [
  "<class 'pyautogui.ImageNotFoundException'>",
  "",
  "File \"<path-to-repo>\\util\\autogui.py\", line 86, in locateCenterOnScreen\n    return pg.locateCenterOnScreen(*args, minSearchTime=1, **kwargs), None\n           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n  File \"<path-to-repo>\\.venv\\Lib\\site-packages\\pyautogui\\__init__.py\", line 174, in wrapper\n    raise ImageNotFoundException  # Raise PyAutoGUI's ImageNotFoundException.\n    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^"
]

Test case 3: Using updated pyautogui API

Code:

try:
    return pyscreeze.locateCenterOnScreen(*args, minSearchTime=1, **kwargs), None
except pyscreeze.ImageNotFoundException as ex:
    logger.exception("Error in locating image on screen", extra={"error": ex})

Snippet of Log output:

"exc_info": [
  "<class 'pyautogui.ImageNotFoundException'>",
  "Could not locate the image (highest confidence = 0.641)",
  "File \"<path-to-repo>\\util\\autogui.py\", line 86, in locateCenterOnScreen\n    return pg.locateCenterOnScreen(*args, minSearchTime=1, **kwargs), None\n           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n  File \"<path-to-repo>\\.venv\\Lib\\site-packages\\pyautogui\\__init__.py\", line 174, in wrapper\n    raise ImageNotFoundException(ex) # Raise PyAutoGUI's ImageNotFoundException.\n    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"
],

@gtmsingh
Copy link
Author

gtmsingh commented Oct 11, 2024

Resolves #850

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant