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

re-throw exceptions as std::runtime_error with original error #801

Merged
merged 1 commit into from
May 8, 2024

Conversation

hellkite500
Copy link
Member

@hellkite500 hellkite500 commented Apr 20, 2024

When getting time units from the base BMI class, exception context is lost from the subclasses which raise them. For example, delegating to to the python adapter where the python module raises an exception, the pybind::error_already_set exception and message is lost, and we are left with just an uncaught std::exception with no context. Catching the generic exception and copying the message into a runtime_error at least provides the error context provided by the original exception.

Changes

  • get_time_convert_factor now does a try/catch and throws runtime_error if any std::exception is caught

Checklist

  • PR has an informative and human-readable title
  • Changes are limited to a single goal (no scope creep)
  • Code can be automatically merged (no conflicts)
  • Code follows project standards (link if applicable)
  • Passes all existing automated tests
  • Any change in functionality is tested
  • New functions are documented (with a description, list of inputs, and expected output)
  • Placeholder code is flagged / future todos are captured in comments
  • Project documentation has been updated (including the "Unreleased" section of the CHANGELOG)
  • Reviewers requested with the Reviewers tool ➡️

Target Environment support

  • Linux
  • MacOs

src/bmi/Bmi_Adapter.cpp Outdated Show resolved Hide resolved
@PhilMiller PhilMiller changed the title re-throw exceptions as std::runtime_error with original err… re-throw exceptions as std::runtime_error with original error Apr 22, 2024
@PhilMiller
Copy link
Contributor

Were we actually getting exceptions thrown from GetTimeUnits? Were they actually related to that, or something coming from a first call to a BMI module post-Initialize?

@hellkite500
Copy link
Member Author

hellkite500 commented Apr 22, 2024

Were we actually getting exceptions thrown from GetTimeUnits? Were they actually related to that, or something coming from a first call to a BMI module post-Initialize?

If a python module, for example, implemented get_time_units as

    def get_time_units(self) -> str:
        raise NotImplementedError

Then this function would error with just

libc++abi: terminating due to uncaught exception of type std::exception: std::exception

I did a quick test on other BMI functionality used via ngen and didn't see the same uncaught exception message. With the proposed changes, the above sitaution produces

libc++abi: terminating due to uncaught exception of type std::runtime_error: NotImplementedError: <EMPTY MESSAGE>

At:
  <path/to/module.py>(<line>): get_time_units

@PhilMiller
Copy link
Contributor

That change in error message is definitely a big improvement.

In the BMI C adapter, we check every return, and throw on failures. Should each language BMI adapter be responsible for doing similarly? I.e. should Bmi_Py_Adapter catch whatever potentially gets thrown from any method, and wrap it up in a type that our higher-level language-independent code will recognize?

@donaldwj donaldwj merged commit 193ef67 into master May 8, 2024
19 checks passed
@donaldwj donaldwj deleted the time-error-rethrow branch May 8, 2024 14:50
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.

4 participants