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