exceptions.hpp 2.96 KB
Newer Older
1
2
3
4
/** Custom exceptions.
 *
 * @file
 * @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
Steffen Vogel's avatar
Steffen Vogel committed
5
 * @copyright 2014-2019, Institute for Automation of Complex Power Systems, EONERC
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
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
 * @license GNU General Public License (version 3)
 *
 * VILLASnode
 *
 * This program 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
 * any later version.
 *
 * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 *********************************************************************************/

#pragma once

#include <string>
#include <sstream>
#include <system_error>
#include <utility>
#include <cerrno>

#include <fmt/core.h>
#include <jansson.h>

namespace villas {

class SystemError : public std::system_error {

public:
	SystemError(const std::string &what) :
		std::system_error(
			errno,
			std::system_category(),
			what
		)
	{ }

	template<typename... Args>
	SystemError(const std::string &what, Args&&... args) :
		SystemError(fmt::format(what, std::forward<Args>(args)...))
	{ }
};

class RuntimeError : public std::runtime_error {

public:
	template<typename... Args>
	RuntimeError(const std::string &what, Args&&... args) :
		std::runtime_error(fmt::format(what, std::forward<Args>(args)...))
	{ }
};

class JsonError : public std::runtime_error {

protected:
	json_error_t error;

public:
	JsonError(const json_error_t &err) :
		std::runtime_error("Failed to decode JSON document"),
		error(err)
	{ }

	template<typename... Args>
	JsonError(const json_error_t &err, const std::string &what, Args&&... args) :
		std::runtime_error(fmt::format(what, std::forward<Args>(args)...))
Steffen Vogel's avatar
Steffen Vogel committed
77
78
79
	{
		(void) err;
	}
80

Steffen Vogel's avatar
Steffen Vogel committed
81
	virtual const char * what() const noexcept
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
	{
		return fmt::format("{}: {} in {}:{}:{}",
			std::runtime_error::what(),
			error.text, error.source, error.line, error.column
		).c_str();
	}
};

class ConfigError : public std::runtime_error {

protected:

	std::string id;
	json_t *setting;

public:
	template<typename... Args>
	ConfigError(json_t *s, const std::string &i, const std::string &what, Args&&... args) :
		std::runtime_error(fmt::format(what, std::forward<Args>(args)...)),
		id(i),
		setting(s)
	{ }

Steffen Vogel's avatar
Steffen Vogel committed
105
	std::string docUri() const
106
107
108
109
110
111
	{
		std::string baseUri = "https://villas.fein-aachen.org/doc/jump?";

		return baseUri + id;
	}

Steffen Vogel's avatar
Steffen Vogel committed
112
	virtual const char * what() const noexcept
113
114
115
116
117
118
119
120
121
122
123
124
	{
		std::stringstream ss;

		ss << "Invalid configuration setting: " << std::endl;
		ss << " Please consult the user documentation for details:" << std::endl;
		ss << "   " << docUri() << std::endl;

		return ss.str().c_str();
	}
};

} // namespace villas