Chaste Commit::6e4f5fe395bca70eb7641cf6e0e87f450383ca5a
Exception.cpp
1/*
2
3Copyright (c) 2005-2026, 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 "FileFinder.hpp"
43#include "PosixPathFixer.hpp"
44#include "GetCurrentWorkingDirectory.hpp"
45#include "ChasteBuildRoot.hpp"
46
47#if (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR < 2 || PETSC_VERSION_MAJOR<3 ) // Before PETSc 3.2
48typedef PetscTruth PetscBool;
49#endif
50
51Exception::Exception(const std::string& rMessage,
52 const std::string& rFilename, unsigned lineNumber) : std::runtime_error(rMessage)
53{
54 SetMessage(rMessage, rFilename, lineNumber);
60 // std::string log_file_message = "Exception occurred (although possibly handled), error message:\n" + message;
61 // LOG(1, log_file_message);
62}
63
64void Exception::SetMessage(const std::string& rMessage,
65 const std::string& rFilename, unsigned lineNumber)
66{
67 // Strip off source root dir if exists
68 std::string filename(rFilename);
69 const size_t root_dir_length = std::char_traits<char>::length(ChasteSourceRootDir());
70 if (filename.compare(0,root_dir_length,ChasteSourceRootDir()) == 0)
71 {
72 filename.replace(0,root_dir_length,"./");
73 }
74
75 std::string posix_filename(ChastePosixPathFixer::ToPosix(fs::path(filename)));
76 mShortMessage = rMessage;
77 std::stringstream line_number_stream;
78 line_number_stream << lineNumber;
79 mMessage = std::string("\nChaste error: ") + posix_filename + ":" + line_number_stream.str() + ": " + mShortMessage;
80}
81
82std::string Exception::GetMessage() const
83{
84 return mMessage;
85}
86
87std::string Exception::GetShortMessage() const
88{
89 return mShortMessage;
90}
91
92const char* Exception::what() const noexcept {
93 return mMessage.c_str();
94}
95
96std::string Exception::CheckShortMessage(const std::string& rExpected) const
97{
98 std::string error;
99 if (mShortMessage != rExpected && mShortMessage != "Another process threw an exception; bailing out.")
100 {
101 error = "Incorrect exception message thrown: expected (" + rExpected + "); got (" + mShortMessage + ").";
102 }
103 return error;
104}
105
106std::string Exception::CheckShortMessageContains(const std::string& rExpected) const
107{
108 std::string error;
109 if (mShortMessage.find(rExpected) == std::string::npos && mShortMessage != "Another process threw an exception; bailing out.")
110 {
111 error = "Incorrect exception message thrown: expected it to contain (" + rExpected + "); got (" + mShortMessage + ").";
112 }
113 return error;
114}
115
116// LCOV_EXCL_START //Termination NEVER EVER happens under normal testing conditions.
117void Exception::Terminate(const std::string& rMessage, const std::string& rFilename, unsigned lineNumber)
118{
119 std::stringstream error_message;
120
121 error_message << "\nChaste termination: " << rFilename << ":" << lineNumber << ": " << rMessage<<"\n";
122 std::cerr << error_message.str();
123
124 /*
125 * Check if we're running in parallel.
126 */
127
128 PetscBool is_there;
129 PetscInitialized(&is_there);
130 if (is_there)
131 {
132 MPI_Abort(PETSC_COMM_WORLD, EXIT_FAILURE);
133 }
134 else
135 {
136 exit(EXIT_FAILURE);
137 }
138}
139
140// 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:82
void SetMessage(const std::string &rMessage, const std::string &rFilename, unsigned lineNumber)
Definition Exception.cpp:64
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:51
static void Terminate(const std::string &rMessage, const std::string &rFilename, unsigned lineNumber)
virtual const char * what() const noexcept override
Definition Exception.cpp:92
std::string GetShortMessage() const
Definition Exception.cpp:87
std::string CheckShortMessage(const std::string &rExpected) const
Definition Exception.cpp:96