Skip to content

Commit

Permalink
Simplify error handling to eliminate need for global error stack
Browse files Browse the repository at this point in the history
All of the functions for manipulating the global error stack
(CanteraError::save, setError, showErrors, etc.) are deprecated. The ability to
store an error is retained only for use in the C and Fortran interfaces so that
the last error message can be retrieved after a function returns an error code.
  • Loading branch information
speth committed Nov 17, 2015
1 parent eb8695d commit ee95c60
Show file tree
Hide file tree
Showing 21 changed files with 183 additions and 251 deletions.
4 changes: 1 addition & 3 deletions doc/sphinx/cxx-guide/simple-example.rst
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ The entire body of the program is put inside a function that is invoked within
a ``try`` block in the main program. In this way, exceptions thrown in the
function or in any procedure it calls may be caught. In this program, a
``catch`` block is defined for exceptions of type :ct:`CanteraError`. Cantera
throws exceptions of this type, so it is always a good idea to catch them. In
the ``catch`` block, function :ct:`showErrors` may be called to print the error
message associated with the exception.
throws exceptions of this type, so it is always a good idea to catch them.

The ``report`` function
=======================
Expand Down
10 changes: 1 addition & 9 deletions include/cantera/base/ctexceptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ namespace Cantera
*
* \include demo1a.cpp
*
* The function showErrors() will print out the fatal error condition to
* standard output.
*
* A group of defines may be used during debugging to assert conditions which
* should be true. These are named AssertTrace(), AssertThrow(), and
* AssertThrowMsg(). Examples of their usage is given below.
Expand Down Expand Up @@ -69,9 +66,6 @@ class CanteraError : public std::exception
public:
//! Normal Constructor for the CanteraError base class
/*!
* In the constructor, a call to the Application class is made to store
* the strings associated with the generated error condition.
*
* @param procedure String name for the function within which the error was
* generated.
* @param msg Descriptive string describing the type of error message.
Expand All @@ -87,9 +81,6 @@ class CanteraError : public std::exception
} else {
msg_ = fmt::format(msg, args...);
}
// Save the error in the global list of errors so that showError()
// can work
save();
}

//! Destructor for base class does nothing
Expand All @@ -99,6 +90,7 @@ class CanteraError : public std::exception
const char* what() const throw();

//! Function to put this error onto Cantera's error stack
//! @deprecated Unused. To be removed after Cantera 2.3.
void save();

//! Method overridden by derived classes to format the error message
Expand Down
6 changes: 6 additions & 0 deletions include/cantera/base/global.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,28 @@ class Logger;
//! Return the number of errors that have been encountered so far
/*!
* @ingroup errorhandling
* @deprecated Unused. To be removed after Cantera 2.3.
*/
int nErrors();

//! @copydoc Application::Messages::lastErrorMessage
//! @deprecated Unused. To be removed after Cantera 2.3.
std::string lastErrorMessage();

//! @copydoc Application::Messages::addError
//! @deprecated Unused. To be removed after Cantera 2.3.
void setError(const std::string& r, const std::string& msg);

//! @copydoc Application::Messages::getErrors
//! @deprecated Unused. To be removed after Cantera 2.3.
void showErrors(std::ostream& f);

//! @copydoc Application::Messages::logErrors
//! @deprecated Unused. To be removed after Cantera 2.3.
void showErrors();

//! @copydoc Application::Messages::popError
//! @deprecated Unused. To be removed after Cantera 2.3.
void popError();

/*!
Expand Down
58 changes: 17 additions & 41 deletions src/base/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ Application::Messages::Messages()
}

Application::Messages::Messages(const Messages& r) :
errorMessage(r.errorMessage),
errorRoutine(r.errorRoutine)
errorMessage(r.errorMessage)
{
// install a default logwriter that writes to standard
// output / standard error
Expand All @@ -74,15 +73,22 @@ Application::Messages& Application::Messages::operator=(const Messages& r)
return *this;
}
errorMessage = r.errorMessage;
errorRoutine = r.errorRoutine;
logwriter.reset(new Logger(*r.logwriter));
return *this;
}

void Application::Messages::addError(const std::string& r, const std::string& msg)
{
errorMessage.push_back(msg);
errorRoutine.push_back(r);
if (msg.size() != 0) {
errorMessage.push_back(
"\n\n************************************************\n"
" Cantera Error! \n"
"************************************************\n\n"
"Procedure: " + r +
"\nError: " + msg + "\n");
} else {
errorMessage.push_back(msg);
}
}

int Application::Messages::getErrorCount()
Expand Down Expand Up @@ -289,64 +295,34 @@ long int Application::readStringRegistryKey(const std::string& keyName, const st
void Application::Messages::popError()
{
if (!errorMessage.empty()) {
errorRoutine.pop_back();
errorMessage.pop_back();
}
}

std::string Application::Messages::lastErrorMessage()
{
if (!errorMessage.empty()) {
string head =
"\n\n************************************************\n"
" Cantera Error! \n"
"************************************************\n\n";
return head+string("\nProcedure: ")+errorRoutine.back()
+string("\nError: ")+errorMessage.back();
return errorMessage.back();
} else {
return "<no Cantera error>";
}
}

void Application::Messages::getErrors(std::ostream& f)
{
size_t i = errorMessage.size();
if (i == 0) {
return;
}
f << endl << endl;
f << "************************************************" << endl;
f << " Cantera Error! " << endl;
f << "************************************************" << endl
<< endl;
for (size_t j = 0; j < i; j++) {
f << endl;
f << "Procedure: " << errorRoutine[j] << endl;
f << "Error: " << errorMessage[j] << endl;
for (size_t j = 0; j < errorMessage.size(); j++) {
f << errorMessage[j] << endl;
}
f << endl << endl;
errorMessage.clear();
errorRoutine.clear();
}

void Application::Messages::logErrors()
{
size_t i = errorMessage.size();
if (i == 0) {
return;
}
writelog("\n\n");
writelog("************************************************\n");
writelog(" Cantera Error! \n");
writelog("************************************************\n\n");
for (size_t j = 0; j < i; j++) {
writelog("\n");
writelog(string("Procedure: ")+ errorRoutine[j]+" \n");
writelog(string("Error: ")+ errorMessage[j]+" \n");
for (size_t j = 0; j < errorMessage.size(); j++) {
writelog(errorMessage[j]);
writelogendl();
}
writelog("\n\n");
errorMessage.clear();
errorRoutine.clear();
}

void Application::setDefaultDirectories()
Expand Down
10 changes: 5 additions & 5 deletions src/base/application.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,12 @@ class Application
* that Cantera accumulates in the Application class.
* @param r Procedure name which is generating the error condition
* @param msg Descriptive message of the error condition.
*
* If only one argument is specified, that string is used as the
* entire message.
* @ingroup errorhandling
*/
void addError(const std::string& r, const std::string& msg);
void addError(const std::string& r, const std::string& msg="");

//! Return the number of errors that have been encountered so far.
/*!
Expand Down Expand Up @@ -141,9 +144,6 @@ class Application
//! Current list of error messages
std::vector<std::string> errorMessage;

//! Current error Routine
std::vector<std::string> errorRoutine;

//! Current pointer to the logwriter
std::unique_ptr<Logger> logwriter;
};
Expand Down Expand Up @@ -198,7 +198,7 @@ class Application
static void ApplicationDestroy();

//! @copydoc Messages::addError
void addError(const std::string& r, const std::string& msg) {
void addError(const std::string& r, const std::string& msg="") {
pMessenger->addError(r, msg);
}

Expand Down
4 changes: 2 additions & 2 deletions src/base/ctexceptions.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! @file ctexceptions.cpp
#include "cantera/base/ctexceptions.h"
#include "application.h"
#include "cantera/base/global.h"

#include <sstream>

Expand All @@ -15,12 +16,11 @@ CanteraError::CanteraError(const std::string& procedure) :
procedure_(procedure),
saved_(false)
{
// Save the error in the global list of errors so that showError() can work
save();
}

void CanteraError::save()
{
warn_deprecated("CanteraError::save", "To be removed after Cantera 2.3.");
if (!saved_) {
Application::Instance()->addError(procedure_, getMessage());
saved_ = true;
Expand Down
4 changes: 4 additions & 0 deletions src/base/global.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,21 +93,25 @@ void close_XML_File(const std::string& file)

int nErrors()
{
warn_deprecated("nErrors", "To be removed after Cantera 2.3");
return app()->getErrorCount();
}

void popError()
{
warn_deprecated("popError", "To be removed after Cantera 2.3");
app()->popError();
}

string lastErrorMessage()
{
warn_deprecated("lastErrorMessage", "To be removed after Cantera 2.3");
return app()->lastErrorMessage();
}

void showErrors(std::ostream& f)
{
warn_deprecated("showErrors", "To be removed after Cantera 2.3");
app()->getErrors(f);
}

Expand Down
7 changes: 4 additions & 3 deletions src/clib/clib_defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "cantera/base/global.h"
#include "cantera/base/ctexceptions.h"
#include "../base/application.h"
#include <iostream>

#ifdef _WIN32
Expand Down Expand Up @@ -53,17 +54,17 @@ T handleAllExceptions(T ctErrorCode, T otherErrorCode)
try {
throw;
} catch (CanteraError& cterr) {
cterr.save();
Application::Instance()->addError(cterr.what());
return ctErrorCode;
} catch (std::exception& err) {
std::cerr << "Cantera: caught an instance of "
<< err.what() << std::endl;
setError("handleAllExceptions", err.what());
Application::Instance()->addError(err.what());
return otherErrorCode;
} catch (...) {
std::cerr << "Cantera: caught an instance of "
"an unknown exception type" << std::endl;
setError("handleAllExceptions", "unknown exception");
Application::Instance()->addError("unknown C++ exception");
return otherErrorCode;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/clib/ct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1374,7 +1374,7 @@ extern "C" {
int getCanteraError(int buflen, char* buf)
{
try {
string e = lastErrorMessage();
string e = Application::Instance()->lastErrorMessage();
copyString(e, buf, buflen);
return int(e.size());
} catch (...) {
Expand Down
Loading

0 comments on commit ee95c60

Please sign in to comment.