CMS 3D CMS Logo

exceptions.h
Go to the documentation of this file.
1 #ifndef __EXCEPTIONS__
2 #define __EXCEPTIONS__
3 
4 #include <cstdlib>
5 #include <iostream>
6 #include <exception>
7 #include <string>
8 
9 #include "boost/exception/exception.hpp"
10 #include "boost/exception/diagnostic_information.hpp"
11 
12 #include "boost/property_tree/exceptions.hpp"
13 #include "boost/filesystem.hpp"
14 //#include "boost/program_options/errors.hpp"
15 
16 // TODO: utilise the exceptions provided by boost as much as possible
17 
18 namespace AllInOneConfig {
19 
20  class ConfigError : public std::exception {
21  const char *msg;
22  const char *what() const throw() override { return msg; }
23 
24  public:
25  ConfigError(const char *m) : msg(m) {}
26  };
27 
29  const char *red = "\x1B[31m" /*, * green = "\x1B[32m"*/ /*, * black = "\x1B[30m"*/;
30  return red + s /*+ black*/; // TODO: clarify colours...
31  }
32 
33  template <int FUNC(int, char **)>
34  int exceptions(int argc, char *argv[]) {
35  try {
36  return FUNC(argc, argv);
37  } catch (const boost::exception &e) {
38  std::cerr << colorify("Boost exception: ") << boost::diagnostic_information(e);
39  throw;
40  } catch (const boost::property_tree::ptree_bad_data &e) {
41  std::cerr << colorify("Property Tree Bad Data Error: ") << e.data<std::string>() << '\n';
42  } catch (const boost::property_tree::ptree_bad_path &e) {
43  std::cerr << colorify("Property Tree Bad Path Error: ") << e.path<std::string>() << '\n';
44  } catch (const boost::property_tree::ptree_error &e) {
45  std::cerr << colorify("Property Tree Error: ") << e.what() << '\n';
46  } catch (const boost::filesystem::filesystem_error &e) {
47  std::cerr << colorify("Filesystem Error:") << e.what() << '\n';
48  } catch (const std::logic_error &e) {
49  std::cerr << colorify("Logic Error: ") << e.what() << '\n';
50  } catch (const std::exception &e) {
51  std::cerr << colorify("Standard Error: ") << e.what() << '\n';
52  }
53  return EXIT_FAILURE;
54  }
55 
56 } // namespace AllInOneConfig
57 #endif
int exceptions(int argc, char *argv[])
Definition: exceptions.h:34
std::string colorify(std::string s)
Definition: exceptions.h:28
const char * what() const override
Definition: exceptions.h:22
ConfigError(const char *m)
Definition: exceptions.h:25