Skip to content

Add stack trace option to CanteraError#1730

Merged
ischoegl merged 4 commits intoCantera:mainfrom
speth:error-traceback
Jul 9, 2024
Merged

Add stack trace option to CanteraError#1730
ischoegl merged 4 commits intoCantera:mainfrom
speth:error-traceback

Conversation

@speth
Copy link
Copy Markdown
Member

@speth speth commented Jul 7, 2024

Changes proposed in this pull request

Add a static method CanteraError::setStackTraceDepth that can be used to add the top N C++ stack frames to the message of a CanteraError. This can provide additional context on where certain errors are being thrown from.

If applicable, provide an example illustrating new features this pull request is introducing

From Python, instantiate a Solution object where the YAML file specifies a negative temperature for the initial state:

import cantera as ct
ct.CanteraError.set_stack_trace_depth(10)
ct.Solution('broken.yaml')

Now, the Python traceback is followed by the CanteraError message and the top of the C++ stack:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "build/python/cantera/solutionbase.pyx", line 37, in cantera.solutionbase._SolutionBase.__cinit__
    self._cinit(infile=infile, name=name, adjacent=adjacent, origin=origin,
  File "build/python/cantera/solutionbase.pyx", line 75, in cantera.solutionbase._SolutionBase._cinit
    self._init_yaml(infile, name, adjacent, yaml, transport)
  File "build/python/cantera/solutionbase.pyx", line 160, in cantera.solutionbase._SolutionBase._init_yaml
    soln = newSolution(
cantera._utils.CanteraError: 
*******************************************************************************
CanteraError thrown by Phase::setTemperature:
temperature must be positive. T = -300
-------------------------------------------------------------------------------
 0# Cantera::CanteraError::CanteraError(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const&) in /Users/speth/src/cantera/build/lib/libcantera_shared.3.1.0.dylib
 1# Cantera::CanteraError::CanteraError<double>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const&, double const&) in /Users/speth/src/cantera/build/lib/libcantera_shared.3.1.0.dylib
 2# Cantera::Phase::setTemperature(double) in /Users/speth/src/cantera/build/lib/libcantera_shared.3.1.0.dylib
 3# Cantera::ThermoPhase::setState_TP(double, double) in /Users/speth/src/cantera/build/lib/libcantera_shared.3.1.0.dylib
 4# Cantera::ThermoPhase::setState(Cantera::AnyMap const&) in /Users/speth/src/cantera/build/lib/libcantera_shared.3.1.0.dylib
 5# Cantera::setupPhase(Cantera::ThermoPhase&, Cantera::AnyMap const&, Cantera::AnyMap const&) in /Users/speth/src/cantera/build/lib/libcantera_shared.3.1.0.dylib
 6# Cantera::newThermo(Cantera::AnyMap const&, Cantera::AnyMap const&) in /Users/speth/src/cantera/build/lib/libcantera_shared.3.1.0.dylib
 7# Cantera::newSolution(Cantera::AnyMap const&, Cantera::AnyMap const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const&, std::__1::vector<std::__1::shared_ptr<Cantera::Solution>, std::__1::allocator<std::__1::shared_ptr<Cantera::Solution>>> const&, std::__1::map<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>, std::__1::shared_ptr<Cantera::Solution>, std::__1::less<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>>, std::__1::allocator<std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const, std::__1::shared_ptr<Cantera::Solution>>>> const&) in /Users/speth/src/cantera/build/lib/libcantera_shared.3.1.0.dylib
 8# Cantera::newSolution(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const&, std::__1::vector<std::__1::shared_ptr<Cantera::Solution>, std::__1::allocator<std::__1::shared_ptr<Cantera::Solution>>> const&) in /Users/speth/src/cantera/build/lib/libcantera_shared.3.1.0.dylib
 9# __pyx_pw_7cantera_12solutionbase_13_SolutionBase_9_init_yaml(_object*, _object* const*, long, _object*) in /Users/speth/src/cantera/build/python/cantera/_cantera.cpython-311-darwin.so
*******************************************************************************

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 Jul 7, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 72.80%. Comparing base (b2c0af5) to head (eaae5d1).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1730   +/-   ##
=======================================
  Coverage   72.80%   72.80%           
=======================================
  Files         381      381           
  Lines       54022    54033   +11     
  Branches     9210     9213    +3     
=======================================
+ Hits        39330    39341   +11     
  Misses      11707    11707           
  Partials     2985     2985           

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

Copy link
Copy Markdown
Member

@ischoegl ischoegl left a comment

Choose a reason for hiding this comment

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

Nice!

@speth speth force-pushed the error-traceback branch from f2b2509 to 1ef95b9 Compare July 7, 2024 18:56
@speth speth force-pushed the error-traceback branch from b96f702 to eaae5d1 Compare July 8, 2024 18:00
Copy link
Copy Markdown
Member

@ischoegl ischoegl left a comment

Choose a reason for hiding this comment

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

Thanks, @speth - this all looks good to me!

@ischoegl ischoegl merged commit 7a69a6e into Cantera:main Jul 9, 2024
@speth speth deleted the error-traceback branch July 23, 2024 15:18
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.

2 participants