Skip to content

Commit

Permalink
Better error message for borked JAVA_HOME environment variable
Browse files Browse the repository at this point in the history
And corresponding test
  • Loading branch information
dlenski committed Jul 1, 2024
1 parent c707794 commit 6e5c897
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
12 changes: 12 additions & 0 deletions test/test_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,18 @@ def test_bad_classpath():
test_reader = zxing.BarCodeReader(classpath=mkdtemp())


def test_wrong_JAVA_HOME():
saved_JAVA_HOME = os.environ.get('JAVA_HOME')
try:
os.environ['JAVA_HOME'] = '/non-existent/path/to/java/stuff'
test_reader = zxing.BarCodeReader()
with helper.assertRaises(zxing.BarCodeReaderException):
test_reader.decode(test_barcodes[0][0])
finally:
if saved_JAVA_HOME is not None:
os.environ['JAVA_HOME'] = saved_JAVA_HOME


@with_setup(setup_reader)
def test_nonexistent_file_error():
global test_reader
Expand Down
2 changes: 2 additions & 0 deletions zxing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ def decode(self, filenames, try_harder=False, possible_formats=None, pure_barcod
raise BarCodeReaderException("Java library could not read image (is it in a supported format?)", fn)
elif stdout.startswith(b'''Exception '''):
raise BarCodeReaderException("Unknown Java exception", self.java) from sp.CalledProcessError(0, cmd, stdout)
elif stdout.startswith(b'''The operation couldn't be completed. Unable to locate a Java Runtime.'''):
raise BarCodeReaderException("Unable to locate Java runtime (check JAVA_HOME variable and other configuration)", self.java) from sp.CalledProcessError(p.returncode, cmd, stdout)
elif p.returncode:
raise BarCodeReaderException("Unexpected Java subprocess return code", self.java) from sp.CalledProcessError(p.returncode, cmd, stdout)

Expand Down

0 comments on commit 6e5c897

Please sign in to comment.