solver  1.0
Exceptions.cpp
1 /*
2  This file is part of the EnergyOptimizatorOfRoboticCells program.
3 
4  EnergyOptimizatorOfRoboticCells is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  EnergyOptimizatorOfRoboticCells is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with EnergyOptimizatorOfRoboticCells. If not, see <http://www.gnu.org/licenses/>.
16 */
17 
18 #include <iostream>
19 #include <sstream>
20 #include "Shared/Exceptions.h"
21 
22 using namespace std;
23 
24 vector<string> split(const std::string& toSplit, const char& delim) {
25  string r;
26  vector<string> splitted;
27  stringstream ss(toSplit);
28  while (getline(ss, r, delim))
29  splitted.push_back(r);
30 
31  return splitted;
32 }
33 
34 string concat(const vector<string>& toConcat, const std::string& beforeItem, const std::string& afterItem) {
35  string result;
36  for (const string& item : toConcat) {
37  result += beforeItem;
38  result += item;
39  result += afterItem;
40  }
41 
42  size_t pos = result.find_last_of(afterItem);
43  if (pos != string::npos)
44  result = result.substr(0, pos); // remove last afterItem
45 
46  return result;
47 }
48 
49 string exceptionToString(const std::exception& e, uint32_t level) {
50  string msg;
51  if (level == 0)
52  msg += string(100, '!')+'\n';
53 
54  try {
55  try {
56  const SolverException& se = dynamic_cast<const SolverException&>(e);
57  msg += se.whatIndented(level);
58  msg += '\n';
59  } catch (...) {
60  vector<string> lines = split(e.what(), '\n');
61  for (const string& line : lines)
62  msg += string(level, '\t')+line+'\n';
63  }
64 
65  rethrow_if_nested(e);
66  } catch (const exception& e) {
67  msg += '\n';
68  msg += exceptionToString(e, level+1);
69  }
70 
71  if (level == 0)
72  msg += string(100, '!');
73 
74  return msg;
75 }
76 
std::string exceptionToString(const std::exception &e, uint32_t level=0)
The recursive method creates the formatted error message for the given exception and their nested sub...
Definition: Exceptions.cpp:49
STL namespace.
A general exception of the program.
Definition: Exceptions.h:58
The file defines extended exceptions for the better error handling in the program.
std::vector< std::string > split(const std::string &toSplit, const char &delim)
It splits the input string, e.g. split("abc ab c", ' ') -> {"abc", "ab", "c"}.
Definition: Exceptions.cpp:24
virtual std::string whatIndented(uint32_t level=0) const
Virtual function returning the formatted error message.
Definition: Exceptions.h:80
std::string concat(const std::vector< std::string > &toConcat, const std::string &beforeItem, const std::string &afterItem)
Method concatenates the given strings, e.g. concat({"abc", "ab"}, "\t", "\n") -> "\tabc\n\tab".
Definition: Exceptions.cpp:34