Skip to content

Commit

Permalink
Pass unhandled exc through GAMSModel.format_exception()
Browse files Browse the repository at this point in the history
  • Loading branch information
khaeru committed Dec 12, 2023
1 parent 914d772 commit 429104d
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions ixmp/model/gams.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,23 +193,28 @@ def __init__(self, name_=None, **model_options):
# Not set; use `quiet` to determine the value
self.gams_args.append(f"LogOption={'2' if self.quiet else '4'}")

def format_exception(self, exc, model_file, backend_class):
def format_exception(
self, exc: Exception, model_file: Path, backend_class: type
) -> Exception:
"""Format a user-friendly exception when GAMS errors."""
lst_file = Path(self.cwd).joinpath(model_file.name).with_suffix(".lst")
lp_5 = "LP status (5): optimal with unscaled infeasibilities"

if getattr(exc, "returncode", 0) > 0:
rc = getattr(exc, "returncode", 0)
if rc > 0:
# Convert a Windows return code >256 to its POSIX equivalent
msg = [
f"GAMS errored with return code {exc.returncode}:",
f" {RETURN_CODE[exc.returncode % 256]}",
f"GAMS errored with return code {rc}:",
f" {RETURN_CODE[rc % 256]}",
]
elif lst_file.exists() and lp_5 in lst_file.read_text(): # pragma: no cover
msg = [
"GAMS returned 0 but indicated:",
f" {lp_5}",
f"and {backend_class.__name__} could not read the solution.",
]
else:
return exc # Other exception

Check warning on line 217 in ixmp/model/gams.py

View check run for this annotation

Codecov / codecov/patch

ixmp/model/gams.py#L217

Added line #L217 was not covered by tests

# Add a reference to the listing file, if it exists
msg.extend(
Expand Down

0 comments on commit 429104d

Please sign in to comment.