2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2011,2012,2013, by the GROMACS development team, led by
5 * David van der Spoel, Berk Hess, Erik Lindahl, and including many
6 * others, as listed in the AUTHORS file in the top-level source
7 * directory and at http://www.gromacs.org.
9 * GROMACS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
14 * GROMACS is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with GROMACS; if not, see
21 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * If you want to redistribute modifications to GROMACS, please
25 * consider that scientific software is very special. Version
26 * control is crucial - bugs must be traceable. We will be happy to
27 * consider code for inclusion in the official distribution, but
28 * derived work must not be called official GROMACS. Details are found
29 * in the README & COPYING files - if they are missing, get the
30 * official version at http://www.gromacs.org.
32 * To help us fund GROMACS development, we humbly ask that you cite
33 * the research papers on the package. Check out http://www.gromacs.org.
37 * Implements classes and functions in exceptions.h.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
40 * \ingroup module_utility
42 #include "exceptions.h"
50 #include <boost/exception/get_error_info.hpp>
51 #include <boost/shared_ptr.hpp>
53 #include "gromacs/legacyheaders/thread_mpi/system_error.h"
54 #include "gromacs/utility/errorcodes.h"
55 #include "gromacs/utility/gmxassert.h"
56 #include "gromacs/utility/stringutil.h"
58 #include "errorformat.h"
66 /********************************************************************
74 * Creates an error message object with the specified text.
76 * \param[in] text Text for the message.
78 explicit ErrorMessage(const std::string &text);
80 //! Whether this object is a context string.
81 bool isContext() const { return static_cast<bool>(child_); }
82 //! Returns the text for this object.
83 const std::string &text() const { return text_; }
85 * Returns the child object for a context object.
87 * Must not be called if isContext() returns false.
89 const ErrorMessage &child() const
91 GMX_ASSERT(isContext(),
92 "Attempting to access nonexistent message object");
97 * Creates a new message object with context prepended.
99 * \param[in] context Context string to add.
100 * \returns New error message object that has \p context as its text
101 * and \c this as its child.
102 * \throws std::bad_alloc if out of memory.
104 ErrorMessage prependContext(const std::string &context) const;
108 boost::shared_ptr<ErrorMessage> child_;
112 * Stores a reason or the top-most context string of an exception.
114 * \ingroup module_utility
116 typedef boost::error_info<struct errinfo_message_, ErrorMessage>
119 ErrorMessage::ErrorMessage(const std::string &text)
122 size_t length = text_.find_last_not_of(" \n");
123 if (length == std::string::npos)
125 length = text_.length() - 1;
127 text_.resize(length + 1);
131 ErrorMessage::prependContext(const std::string &context) const
133 ErrorMessage newMessage(context);
134 newMessage.child_.reset(new ErrorMessage(*this));
139 * Stores list of nested exceptions for Gromacs exceptions.
141 * \ingroup module_utility
143 typedef boost::error_info<struct errinfo_message_, internal::NestedExceptionList>
144 errinfo_nested_exceptions;
148 /********************************************************************
152 GromacsException::GromacsException(const ExceptionInitializer &details)
154 *this << errinfo_message(ErrorMessage(details.reason_));
155 if (details.hasNestedExceptions())
157 *this << errinfo_nested_exceptions(details.nested_);
161 const char *GromacsException::what() const throw()
163 const ErrorMessage *msg = boost::get_error_info<errinfo_message>(*this);
164 while (msg != NULL && msg->isContext())
168 return msg != NULL ? msg->text().c_str() : "No reason provided";
171 void GromacsException::prependContext(const std::string &context)
173 const ErrorMessage *msg = boost::get_error_info<errinfo_message>(*this);
174 GMX_RELEASE_ASSERT(msg != NULL, "Message should always be set");
175 *this << errinfo_message(msg->prependContext(context));
178 /********************************************************************
179 * Derived exception classes
182 int FileIOError::errorCode() const
187 int InvalidInputError::errorCode() const
189 return eeInvalidInput;
192 int InconsistentInputError::errorCode() const
194 return eeInconsistentInput;
197 int SimulationInstabilityError::errorCode() const
199 return eeInstability;
202 int InternalError::errorCode() const
204 return eeInternalError;
207 int APIError::errorCode() const
212 int NotImplementedError::errorCode() const
214 return eeNotImplemented;
218 /********************************************************************
226 * Abstracts actual output from the other logic in exception formatting.
228 * Object that implements this interface is passed to
229 * formatExceptionMessageInternal(), and is responsible for composing the
230 * output. This allows using the same implementation of interpreting the
231 * exceptions while still supporting output to different formats (e.g., to a
232 * string or to stderr).
234 class MessageWriterInterface
237 virtual ~MessageWriterInterface() {}
240 * Writes a single line of text into the output.
242 * \param[in] text Text to write on the line.
243 * \param[in] indent Suggested number of spaces to indent the line.
245 virtual void writeLine(const char *text, int indent) = 0;
247 * Writes information about a system error (errno-based).
249 * \param[in] errorNumber errno value
250 * \param[in] funcName Name of the system call (can be NULL).
251 * \param[in] indent Suggested number of spaces to indent the output.
253 virtual void writeErrNoInfo(int errorNumber, const char *funcName,
258 * Exception information writer for cases where exceptions should be avoided.
260 * Formats the messages into the provided FILE handle without checking for
261 * errors in fprintf() calls.
263 class MessageWriterFileNoThrow : public MessageWriterInterface
266 //! Initializes a writer that writes to the given file handle.
267 explicit MessageWriterFileNoThrow(FILE *fp) : fp_(fp) {}
269 virtual void writeLine(const char *text, int indent)
271 internal::printFatalErrorMessageLine(fp_, text, indent);
273 virtual void writeErrNoInfo(int errorNumber, const char *funcName,
276 std::fprintf(fp_, "%*sReason: %s\n", indent, "",
277 std::strerror(errorNumber));
278 if (funcName != NULL)
280 std::fprintf(fp_, "%*s(call to %s() returned error code %d)\n",
281 indent, "", funcName, errorNumber);
290 * Exception information writer to format into an std::string.
292 class MessageWriterString : public MessageWriterInterface
295 //! Post-processes the output string to not end in a line feed.
296 void removeTerminatingLineFeed()
298 if (result_.size() > 0U)
300 result_.erase(result_.size() - 1);
303 //! Returns the constructed string.
304 const std::string &result() const { return result_; }
306 virtual void writeLine(const char *text, int indent)
308 result_.append(indent, ' ');
309 result_.append(text);
310 result_.append("\n");
312 virtual void writeErrNoInfo(int errorNumber, const char *funcName,
315 writeLine(formatString("Reason: %s", std::strerror(errorNumber)).c_str(),
317 if (funcName != NULL)
319 writeLine(formatString("(call to %s() returned error code %d)",
320 funcName, errorNumber).c_str(),
330 * Prints error information for an exception object.
332 * \param[in] writer Writer to write out the information.
333 * \param[in] ex Exception object to print.
334 * \param[in] indent Indentation for the information.
336 * If the exception contains nested exceptions, information from them is
337 * recursively printed.
339 * Does not throw unless the writer throws.
341 void formatExceptionMessageInternal(MessageWriterInterface *writer,
342 const std::exception &ex, int indent)
344 const boost::exception *boostEx = dynamic_cast<const boost::exception *>(&ex);
347 // TODO: Add an option to print this information for the tests
348 // const char *const *funcPtr =
349 // boost::get_error_info<boost::throw_function>(*boostEx);
350 // const char *const *filePtr =
351 // boost::get_error_info<boost::throw_file>(*boostEx);
352 // const int *linePtr =
353 // boost::get_error_info<boost::throw_line>(*boostEx);
355 // std::string result;
356 // if (filePtr != NULL && linePtr != NULL)
358 // result = formatString("%s:%d: %s\n", *filePtr, *linePtr,
359 // funcPtr != NULL ? *funcPtr : "");
362 // TODO: Remove duplicate context if present in multiple nested exceptions.
363 const ErrorMessage *msg = boost::get_error_info<errinfo_message>(*boostEx);
366 while (msg != NULL && msg->isContext())
368 writer->writeLine(msg->text().c_str(), indent*2);
372 if (msg != NULL && !msg->text().empty())
374 writer->writeLine(msg->text().c_str(), indent*2);
379 writer->writeLine(ex.what(), indent*2);
382 const int *errorNumber
383 = boost::get_error_info<boost::errinfo_errno>(*boostEx);
384 if (errorNumber != NULL)
386 const char * const *funcName
387 = boost::get_error_info<boost::errinfo_api_function>(*boostEx);
388 writer->writeErrNoInfo(*errorNumber,
389 funcName != NULL ? *funcName : NULL,
393 // TODO: Treat also boost::nested_exception (not currently used, though)
395 const internal::NestedExceptionList *nested
396 = boost::get_error_info<errinfo_nested_exceptions>(*boostEx);
399 internal::NestedExceptionList::const_iterator ni;
400 for (ni = nested->begin(); ni != nested->end(); ++ni)
404 rethrow_exception(*ni);
406 catch (const std::exception &nestedEx)
408 formatExceptionMessageInternal(writer, nestedEx, indent + 1);
415 writer->writeLine(ex.what(), indent*2);
421 void printFatalErrorMessage(FILE *fp, const std::exception &ex)
423 const char *title = "Unknown exception";
424 bool bPrintType = false;
425 const GromacsException *gmxEx = dynamic_cast<const GromacsException *>(&ex);
426 // TODO: Treat more of the standard exceptions
429 title = getErrorCodeString(gmxEx->errorCode());
431 else if (dynamic_cast<const tMPI::system_error *>(&ex) != NULL)
433 title = "System error in thread synchronization";
435 else if (dynamic_cast<const std::bad_alloc *>(&ex) != NULL)
437 title = "Memory allocation failed";
439 else if (dynamic_cast<const std::logic_error *>(&ex) != NULL)
441 title = "Standard library logic error (bug)";
444 else if (dynamic_cast<const std::runtime_error *>(&ex) != NULL)
446 title = "Standard library runtime error (possible bug)";
453 // We can't call get_error_info directly on ex since our internal boost
454 // needs to be compiled with BOOST_NO_RTTI. So we do the dynamic_cast
456 const char *const *funcPtr = NULL;
457 const char *const *filePtr = NULL;
458 const int *linePtr = NULL;
459 const boost::exception *boostEx = dynamic_cast<const boost::exception *>(&ex);
462 funcPtr = boost::get_error_info<boost::throw_function>(*boostEx);
463 filePtr = boost::get_error_info<boost::throw_file>(*boostEx);
464 linePtr = boost::get_error_info<boost::throw_line>(*boostEx);
466 internal::printFatalErrorHeader(fp, title,
467 funcPtr != NULL ? *funcPtr : NULL,
468 filePtr != NULL ? *filePtr : NULL,
469 linePtr != NULL ? *linePtr : 0);
472 std::fprintf(fp, "(exception type: %s)\n", typeid(ex).name());
474 MessageWriterFileNoThrow writer(fp);
475 formatExceptionMessageInternal(&writer, ex, 0);
476 internal::printFatalErrorFooter(fp);
479 std::string formatExceptionMessageToString(const std::exception &ex)
481 MessageWriterString writer;
482 formatExceptionMessageInternal(&writer, ex, 0);
483 writer.removeTerminatingLineFeed();
484 return writer.result();
487 void formatExceptionMessageToFile(FILE *fp, const std::exception &ex)
489 MessageWriterFileNoThrow writer(fp);
490 formatExceptionMessageInternal(&writer, ex, 0);