Skip to content

Commit

Permalink
Improve copy exception formatter
Browse files Browse the repository at this point in the history
This time I'm more clever and look for the keywords "cannot" and "cast"
in the exception string. Perhaps this will be more tolerant of slight
variations of the exception stirng than relying on hard coded indices.
  • Loading branch information
elevans committed Jan 14, 2025
1 parent a01f655 commit 7f26813
Showing 1 changed file with 7 additions and 11 deletions.
18 changes: 7 additions & 11 deletions src/imagej/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,15 +257,11 @@ def _format_copy_exception(exc: str, fun_name: str) -> str:
:return: The formatted exception.
"""
# format cast exception
exc = str(exc)
m = exc.split(" ")
from_class = ""
to_class = ""
if m[0] == "java.lang.ClassCastException:" and "net.imglib2.util" in fun_name:
from_class = m[2]
to_class = m[8]
elif m[0] == "java.lang.RuntimeException:" and "net.imagej.ops" in fun_name:
from_class = m[4]
to_class = m[10]
msg = f"Error: Unsupported type cast via {fun_name}\n Source type: {from_class}\n Target type: {to_class}"

return msg
if "cannot be cast to" in exc:
from_class = m[m.index("cannot") - 1]
to_class = m[m.index("cast") + 3]
return f"Error: Unsupported type cast via {fun_name}\n Source type: {from_class}\n Target type: {to_class}"
else:
return exc

0 comments on commit 7f26813

Please sign in to comment.