Chaste Commit::ca8ccdedf819b6e02855bc0e8e6f50bdecbc5208
Exception.cpp
1/*
2
3Copyright (c) 2005-2024, University of Oxford.
4All rights reserved.
5
6University of Oxford means the Chancellor, Masters and Scholars of the
7University of Oxford, having an administrative office at Wellington
8Square, Oxford OX1 2JD, UK.
9
10This file is part of Chaste.
11
12Redistribution and use in source and binary forms, with or without
13modification, are permitted provided that the following conditions are met:
14 * Redistributions of source code must retain the above copyright notice,
15 this list of conditions and the following disclaimer.
16 * Redistributions in binary form must reproduce the above copyright notice,
17 this list of conditions and the following disclaimer in the documentation
18 and/or other materials provided with the distribution.
19 * Neither the name of the University of Oxford nor the names of its
20 contributors may be used to endorse or promote products derived from this
21 software without specific prior written permission.
22
23THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33
34*/
35
36#include "Exception.hpp"
37
38#include <iostream>
39#include <string>
40#include <petsc.h>
41
42#include "Exception.hpp"
43#include "FileFinder.hpp"
44#include "PosixPathFixer.hpp"
45#include "GetCurrentWorkingDirectory.hpp"
46#include "ChasteBuildRoot.hpp"
47#include "PosixPathFixer.hpp"
48
49#if (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR < 2 || PETSC_VERSION_MAJOR<3 ) // Before PETSc 3.2
50typedef PetscTruth PetscBool;
51#endif
52
53Exception::Exception(const std::string& rMessage,
54 const std::string& rFilename, unsigned lineNumber) : std::runtime_error(rMessage)
55{
56 SetMessage(rMessage, rFilename, lineNumber);
62 // std::string log_file_message = "Exception occurred (although possibly handled), error message:\n" + message;
63 // LOG(1, log_file_message);
64}
65
66void Exception::SetMessage(const std::string& rMessage,
67 const std::string& rFilename, unsigned lineNumber)
68{
69 // Strip off source root dir if exists
70 std::string filename(rFilename);
71 const size_t root_dir_length = std::char_traits<char>::length(ChasteSourceRootDir());
72 if (filename.compare(0,root_dir_length,ChasteSourceRootDir()) == 0)
73 {
74 filename.replace(0,root_dir_length,"./");
75 }
76
77 std::string posix_filename(ChastePosixPathFixer::ToPosix(fs::path(filename)));
78 mShortMessage = rMessage;
79 std::stringstream line_number_stream;
80 line_number_stream << lineNumber;
81 mMessage = std::string("\nChaste error: ") + posix_filename + ":" + line_number_stream.str() + ": " + mShortMessage;
82}
83
84std::string Exception::GetMessage() const
85{
86 return mMessage;
87}
88
89std::string Exception::GetShortMessage() const
90{
91 return mShortMessage;
92}
93
94const char* Exception::what() const noexcept {
95 return mMessage.c_str();
96}
97
98std::string Exception::CheckShortMessage(const std::string& rExpected) const
99{
100 std::string error;
101 if (mShortMessage != rExpected && mShortMessage != "Another process threw an exception; bailing out.")
102 {
103 error = "Incorrect exception message thrown: expected (" + rExpected + "); got (" + mShortMessage + ").";
104 }
105 return error;
106}
107
108std::string Exception::CheckShortMessageContains(const std::string& rExpected) const
109{
110 std::string error;
111 if (mShortMessage.find(rExpected) == std::string::npos && mShortMessage != "Another process threw an exception; bailing out.")
112 {
113 error = "Incorrect exception message thrown: expected it to contain (" + rExpected + "); got (" + mShortMessage + ").";
114 }
115 return error;
116}
117
118// LCOV_EXCL_START //Termination NEVER EVER happens under normal testing conditions.
119void Exception::Terminate(const std::string& rMessage, const std::string& rFilename, unsigned lineNumber)
120{
121 std::stringstream error_message;
122
123 error_message << "\nChaste termination: " << rFilename << ":" << lineNumber << ": " << rMessage<<"\n";
124 std::cerr << error_message.str();
125
126 /*
127 * Check if we're running in parallel.
128 */
129
130 PetscBool is_there;
131 PetscInitialized(&is_there);
132 if (is_there)
133 {
134 MPI_Abort(PETSC_COMM_WORLD, EXIT_FAILURE);
135 }
136 else
137 {
138 exit(EXIT_FAILURE);
139 }
140}
141
142// LCOV_EXCL_STOP // Termination NEVER EVER happens under normal testing conditions
const char * ChasteSourceRootDir()
static std::string ToPosix(const fs::path path)
std::string GetMessage() const
Definition Exception.cpp:84
void SetMessage(const std::string &rMessage, const std::string &rFilename, unsigned lineNumber)
Definition Exception.cpp:66
std::string mShortMessage
std::string CheckShortMessageContains(const std::string &rExpected) const
std::string mMessage
Exception(const std::string &rMessage, const std::string &rFilename, unsigned lineNumber)
Definition Exception.cpp:53
static void Terminate(const std::string &rMessage, const std::string &rFilename, unsigned lineNumber)
virtual const char * what() const noexcept override
Definition Exception.cpp:94
std::string GetShortMessage() const
Definition Exception.cpp:89
std::string CheckShortMessage(const std::string &rExpected) const
Definition Exception.cpp:98