Flatland
exceptions.h
1 /*
2  * ______ __ __ __
3  * /\ _ \ __ /\ \/\ \ /\ \__
4  * \ \ \L\ \ __ __ /\_\ \_\ \ \ \____ ___\ \ ,_\ ____
5  * \ \ __ \/\ \/\ \\/\ \ /'_` \ \ '__`\ / __`\ \ \/ /',__\
6  * \ \ \/\ \ \ \_/ |\ \ \/\ \L\ \ \ \L\ \/\ \L\ \ \ \_/\__, `\
7  * \ \_\ \_\ \___/ \ \_\ \___,_\ \_,__/\ \____/\ \__\/\____/
8  * \/_/\/_/\/__/ \/_/\/__,_ /\/___/ \/___/ \/__/\/___/
9  * @copyright Copyright 2017 Avidbots Corp.
10  * @name world.h
11  * @brief Defines flatland Layer
12  * @author Chunshang Li
13  *
14  * Software License Agreement (BSD License)
15  *
16  * Copyright (c) 2017, Avidbots Corp.
17  * All rights reserved.
18  *
19  * Redistribution and use in source and binary forms, with or without
20  * modification, are permitted provided that the following conditions
21  * are met:
22  *
23  * * Redistributions of source code must retain the above copyright
24  * notice, this list of conditions and the following disclaimer.
25  * * Redistributions in binary form must reproduce the above
26  * copyright notice, this list of conditions and the following
27  * disclaimer in the documentation and/or other materials provided
28  * with the distribution.
29  * * Neither the name of the Avidbots Corp. nor the names of its
30  * contributors may be used to endorse or promote products derived
31  * from this software without specific prior written permission.
32  *
33  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
34  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
35  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
36  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
37  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
38  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
39  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
40  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
41  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
43  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
44  * POSSIBILITY OF SUCH DAMAGE.
45  */
46 
47 #ifndef FLATLAND_SERVER_EXCEPTIONS_H
48 #define FLATLAND_SERVER_EXCEPTIONS_H
49 
50 #include <yaml-cpp/yaml.h>
51 #include <boost/filesystem.hpp>
52 #include <exception>
53 #include <string>
54 
55 namespace flatland_server {
56 
57 class Exception : public std::runtime_error {
58  public:
62  Exception(const std::string &msg) : runtime_error(msg) {}
63 };
64 
65 class PluginException : public Exception {
66  public:
71  PluginException(const std::string &msg) : Exception(ErrorMsg(msg)) {}
72 
73  private:
78  static const std::string ErrorMsg(const std::string &msg) {
79  std::stringstream output;
80  output << "Flatland plugin: ";
81  output << msg;
82  return output.str();
83  }
84 };
85 
86 class YAMLException : public Exception {
87  public:
94  YAMLException(const std::string &msg,
95  const YAML::Exception &yaml_cpp_exception)
96  : Exception(
97  ErrorMsg(msg, yaml_cpp_exception.msg, yaml_cpp_exception.mark)) {}
98 
104  YAMLException(const std::string &msg) : Exception("Flatland YAML: " + msg) {}
105 
106  private:
113  static const std::string ErrorMsg(const std::string &msg,
114  const std::string &yaml_cpp_msg,
115  const YAML::Mark &yaml_cpp_mark) {
116  std::stringstream output;
117 
118  output << "Flatland YAML: ";
119  output << msg;
120  if (!(yaml_cpp_mark.pos == -1 && yaml_cpp_mark.line == -1 &&
121  yaml_cpp_mark.column == -1)) {
122  output << ", line " << yaml_cpp_mark.line + 1 << " col "
123  << yaml_cpp_mark.column + 1;
124  }
125 
126  if (yaml_cpp_msg.size() > 0) {
127  output << ", " << yaml_cpp_msg;
128  }
129 
130  return output.str();
131  }
132 };
133 }; // namespace flatland_server
134 
135 #endif // FLATLAND_SERVER_WORLD_H
Exception(const std::string &msg)
Constructor for the Exception class.
Definition: exceptions.h:62
YAMLException(const std::string &msg)
Constructor for the YAMLException class, stores and generates exception message using just a message...
Definition: exceptions.h:104
Definition: exceptions.h:57
PluginException(const std::string &msg)
Constructor for PluginException.
Definition: exceptions.h:71
Definition: body.h:55
Definition: exceptions.h:86
YAMLException(const std::string &msg, const YAML::Exception &yaml_cpp_exception)
Constructor for the YAMLException class, stores and generates exception message using yaml cpp except...
Definition: exceptions.h:94
Definition: exceptions.h:65