Skip to content

Commit

Permalink
Convert Java to Python exceptions in {gs}et_meta()
Browse files Browse the repository at this point in the history
Per #296. This was missed in #353.
  • Loading branch information
khaeru committed May 25, 2021
1 parent 8ca020c commit fbe1ac6
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions ixmp/backend/jdbc.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"at.ac.iiasa.ixmp.dto.TimesliceDTO",
"at.ac.iiasa.ixmp.Platform",
"java.lang.Double",
"java.lang.Exception",
"java.lang.Integer",
"java.lang.NoClassDefFoundError",
"java.lang.IllegalArgumentException",
Expand Down Expand Up @@ -123,10 +124,20 @@ def _read_properties(file):

def _raise_jexception(exc, msg="unhandled Java exception: "):
"""Convert Java/JPype exceptions to ordinary Python RuntimeError."""
# Try to re-raise as a ValueError for bad model or scenario name
arg = exc.args[0] if isinstance(exc.args[0], str) else ""
match = re.search(r"getting '([^']*)' in table '([^']*)'", arg)
if match:
param = match.group(2).lower()
if param in {"model", "scenario"}:
raise ValueError(f"{param}={repr(match.group(1))}") from None

# Other exceptions
if _EXCEPTION_VERBOSE:
msg += "\n\n" + exc.stacktrace()
else:
msg += exc.message()

raise RuntimeError(msg) from None


Expand Down Expand Up @@ -1049,7 +1060,12 @@ def get_meta(
self._validate_meta_args(model, scenario, version)
if version is not None:
version = java.Long(version)
meta = self.jobj.getMeta(model, scenario, version, strict)

try:
meta = self.jobj.getMeta(model, scenario, version, strict)
except java.IxException as e:
_raise_jexception(e)

return {entry.getKey(): _unwrap(entry.getValue()) for entry in meta.entrySet()}

def set_meta(
Expand All @@ -1062,7 +1078,11 @@ def set_meta(
jmeta = java.HashMap()
for k, v in meta.items():
jmeta.put(str(k), _wrap(v))
self.jobj.setMeta(model, scenario, version, jmeta)

try:
self.jobj.setMeta(model, scenario, version, jmeta)
except java.IxException as e:
_raise_jexception(e)

def remove_meta(
self, categories, model: str = None, scenario: str = None, version: int = None
Expand Down

0 comments on commit fbe1ac6

Please sign in to comment.