Skip to content

Improve exceptions for abcName()#2014

Merged
ischoegl merged 8 commits intoCantera:mainfrom
ischoegl:improve-exceptions
Oct 22, 2025
Merged

Improve exceptions for abcName()#2014
ischoegl merged 8 commits intoCantera:mainfrom
ischoegl:improve-exceptions

Conversation

@ischoegl
Copy link
Copy Markdown
Member

@ischoegl ischoegl commented Oct 21, 2025

Changes proposed in this pull request

This is a minor follow-up PR to #2010. I had noticed that exception handling for *Name(size_t k) was pretty inconsistent. In some instances, an exception was raised, in other cases, an <unknown> was returned, and in other cases, the index wasn't checked at all (i.e., undefined behavior). This PR updates the code so that it consistently throws an IndexError if the index is out of bounds.

The only instance where transitional behavior is implemented is kineticsSpeciesName (it's only user-facing, but some user code could check for <unknown>). For 0D and 1D instances of componentName, the implementation was inconsistent (some instances raised exceptions while others didn't), so I opted to raise exceptions for all, so all specializations follow the same approach.

Checklist

  • The pull request includes a clear description of this code change
  • Commit messages have short titles and reference relevant issues
  • Build passes (scons build & scons test) and unit tests address code coverage
  • Style & formatting of contributed code follows contributing guidelines
  • The pull request is ready for review

@codecov
Copy link
Copy Markdown

codecov bot commented Oct 21, 2025

Codecov Report

❌ Patch coverage is 57.89474% with 24 lines in your changes missing coverage. Please review.
✅ Project coverage is 75.47%. Comparing base (5e351a1) to head (cf0088c).
⚠️ Report is 13 commits behind head on main.

Files with missing lines Patch % Lines
src/oneD/Domain1D.cpp 14.28% 4 Missing and 2 partials ⚠️
src/kinetics/Kinetics.cpp 42.85% 3 Missing and 1 partial ⚠️
src/zeroD/ReactorNet.cpp 70.00% 3 Missing ⚠️
src/equil/MultiPhase.cpp 81.81% 1 Missing and 1 partial ⚠️
src/equil/vcs_VolPhase.cpp 50.00% 1 Missing and 1 partial ⚠️
src/oneD/OneDim.cpp 0.00% 1 Missing and 1 partial ⚠️
src/oneD/Boundary1D.cpp 0.00% 1 Missing ⚠️
src/zeroD/ConstPressureMoleReactor.cpp 0.00% 1 Missing ⚠️
src/zeroD/ConstPressureReactor.cpp 0.00% 1 Missing ⚠️
src/zeroD/IdealGasConstPressureMoleReactor.cpp 0.00% 1 Missing ⚠️
... and 1 more
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2014      +/-   ##
==========================================
+ Coverage   75.43%   75.47%   +0.03%     
==========================================
  Files         454      454              
  Lines       56773    56798      +25     
  Branches     9345     9356      +11     
==========================================
+ Hits        42825    42866      +41     
+ Misses      10785    10764      -21     
- Partials     3163     3168       +5     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ischoegl ischoegl force-pushed the improve-exceptions branch 3 times, most recently from 967a766 to af53e69 Compare October 21, 2025 17:34
@ischoegl ischoegl marked this pull request as ready for review October 21, 2025 17:53
@ischoegl ischoegl requested a review from a team October 21, 2025 19:08
@ischoegl
Copy link
Copy Markdown
Member Author

@speth ... this should be fairly straightforward.

Copy link
Copy Markdown
Member

@speth speth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @ischoegl. The previous behavior on a couple of these was definitely odd. I have just a couple of minor comments.

Comment on lines +54 to +57
if (m < m_mm) {
return m_elementNames[m];
}
throw IndexError("Phase::elementName", "element", m, m_mm);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous approach using checkElementIndex achieved the same thing, didn't it?

Copy link
Copy Markdown
Member Author

@ischoegl ischoegl Oct 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is equivalent, but I thought the Phase::elementName labeling is nicer for this specific case. Fwiw, it's parallel to the situation with Phase::speciesName. Technically, the functions only share the if (m < m_mm) { line ... other than the trivial ‘}‘

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, of course the same comment would apply to speciesName. I guess I just think it's odd to change this after you just introduced the return value for checkElementIndex and friends which enabled this when this is one of the few places that function is used in that way. It's a minor point, though, and obviously not something to hold up this PR over.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair point. Was facing an internal tug of war between ‚dry‘ and the most intuitive exception message. At the same time, it’s a really inconsequential choice.

@ischoegl ischoegl requested a review from speth October 22, 2025 00:43
@ischoegl ischoegl merged commit 6ae17f6 into Cantera:main Oct 22, 2025
52 of 53 checks passed
@ischoegl ischoegl deleted the improve-exceptions branch October 22, 2025 11:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants