Chordata_error.h 5.63 KB
Newer Older
Bruno Laurencich's avatar
Bruno Laurencich committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
/**
 * @file Chordata_error.h
 * Definitions of error objects
 *
 * @author Bruno Laurencich
 * @version 0.1.0 
 * @date 2018/08/20
 *
 * Notochord  
 * -- Hub program for the Chordata Open Source motion capture system
 *
 * http://chordata.cc
 * contact@chordata.cc
 *
 *
 * Copyright 2018 Bruno Laurencich
 *
 * This file is part of Notochord.
 *
 * Notochord is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Notochord is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Notochord. If not, see <https://www.gnu.org/licenses/>.
 *
 * This program uses code from various sources, the default license is GNU GPLv3
 * for all code, the dependencies where originally distributed as follows:
 * -LSM9DS# libraries: Copyright 2014,2015 Jim Lindblom @ SparkFun Electronics, as beerware.
 * -fmt library: Copyright (c) 2012 - present, Victor Zverovich, Under BSD 2-clause License.
 * -spdlog library: Copyright(c) 2015 Gabi Melman, under MIT license.
 * -args library: Copyright (c) 2016 Taylor C. Richberger, under MIT license.
 * -bcm2835 library: Copyright 2011-2013 Mike McCaule, Under GNU-GPLv2 License.
 * -oscpack library: Copyright (c) 2004-2013 Ross Bencina, under MIT license.
 * -Sensor fusion algorithms: Copyright 2011 SOH Madgwick, Under GNU-GPLv3 License.
 * -tinyxml2 library: Copyright <unknowk year> Lee Thomason, under zlib License.
 * -pstreams library: Copyright (C) 2001 - 2017 Jonathan Wakely, under Boost Software License
 *
 */

Bruno Laurencich's avatar
Bruno Laurencich committed
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
#ifndef __CHORDATA_ERROR_
#define __CHORDATA_ERROR_

#include "Chordata_utils.h"
#include "tinyxml2.h"
#include "args.hxx"

#include <iostream>
#include <exception>
#include <cstdint>
#include <string>

using std::string;

namespace Chordata{

	typedef void (*comfn)(std::string);
	// comfn out = &Chordata::Comunicator::log; 
	// comfn err = &Chordata::Comunicator::error;
	// comfn debug = &Chordata::Comunicator::debug;

	struct Error: public std::exception {
		const string msg;
		uint64_t running_time;

		Error(uint64_t time = 0):
			msg("Unknown"),
			running_time(time)
			{};

		Error(string _msg = "Unknown", uint64_t time = 0): 
			msg(_msg), 
			running_time(time)
			{};

		virtual const char* what() const noexcept override{
			return msg.c_str();
		}

	};

	struct IO_Error: public Error{
		static long int count;
		
		IO_Error(string _msg = "Unknown", uint64_t time = 0): 
		Error(_msg, time) {
			++ count;
		};
	};

	struct Hierarchy_Error: public Error{
		
		Hierarchy_Error(string _msg = "Unknown", uint64_t time = 0): 
		Error(_msg, time) 
		{};
	};

	struct Idiot_Error: public Error{
		
		Idiot_Error(string _msg = "", uint64_t time = 0): 
		Error("Idiot, look carefully at your code!\n" +_msg, time) 
		{};
	};

	struct Logic_Error: public Error/*, public std::logic_error*/{
		Logic_Error(string _msg = "Unknown", uint64_t time = 0):
		Error(_msg, time)
		// std::logic_error(_msg) 
		{};

		// virtual const char* what() const noexcept override{
		// 	return Error::what();
		// }
	};
	

	struct System_Error: public Error/*, public std::system_error*/{
		int c;
		System_Error(int ec, string _msg = "Unknown", uint64_t time = 0):
		Error(_msg, time),
		c(ec)
		// std::system_error(std::error_code(ec, std::system_category())) 
		{};

		int code() const noexcept{
			return c;
		};

		// virtual const char* what() const noexcept override{
		// 	return Error::what();
		// }
	};



}
/////////////////////////////////
///PARSING ERRORS
/////////////////////////////////
#define ARMATURE_XML_ERROR(msg, XML_E) XML_Parse_Error(msg, ARMATURE_PARSING, XML_E)

	//TODO: include filename (pass the parser?)
namespace Chordata{

	 
	struct Parsing_Error: public Error{
	public:	
		const std::string errorCause;
		
		Parsing_Error() = delete; //DEFAULT CONSTRUCTOR NOT ALLOWED

		Parsing_Error(std:: string _cause, 
					Parsing_stage _stage, uint64_t time = 0):
		 errorCause( fmt::format("Error parsing Chordata's config files \n[Stage: {}]\n"
161
		 						"Cause: {}", msgs::stages[_stage], _cause).c_str()),
Bruno Laurencich's avatar
Bruno Laurencich committed
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
		 stage(_stage),
		 Error(time) 
		 {};


		virtual const char* what() const throw() {
			return cause();
		}

		virtual const char* cause() const throw() {
			return errorCause.c_str();
		}


	private:
		std::string filename;
		Parsing_stage stage;
	};

	struct CMDLine_Parse_Error: public Parsing_Error{
		public:
		CMDLine_Parse_Error(std::string _cause, 
				args::Error& aerror,
				uint64_t time = 0 
				):
				Parsing_Error(_cause, CMD_LINE_PARSING, time),
				args_error(aerror),
				output(
190
					fmt::format("{}\n{}", (typeid(aerror) == typeid(args::Help)) ? "": aerror.what(), _cause).c_str()					 
Bruno Laurencich's avatar
Bruno Laurencich committed
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
					)
				{};

		virtual const char* what() const throw() {
			return output.c_str();
		}

		const auto& get_original() const{
			return args_error;
		}
		// static bool checkHelp(args::Error error){
		// 	return dynamic_cast<args::Help>(error) != nullptr;
		// }

		private:
		std::string output; 
		args::Error args_error;
	};


	struct XML_Parse_Error: public Parsing_Error{
		public:
		XML_Parse_Error(std::string _cause, Parsing_stage _stage,
				tinyxml2::XMLError xerror = tinyxml2::XML_SUCCESS,
				uint64_t time = 0 ):
				Parsing_Error(_cause, _stage, time),
				xml_error(xerror)
				 {};

		virtual const char* what() const throw() {
			return cause();
		}

	private:
		tinyxml2::XMLError xml_error;
	};

}

#endif