/** Common exceptions. * * @file * @author Steffen Vogel * @copyright 2014-2020, Institute for Automation of Complex Power Systems, EONERC * @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 . *********************************************************************************/ #pragma once #include #include #include #include #include #include #include #include namespace villas { class SystemError : public std::system_error { public: SystemError(const std::string &what) : std::system_error( errno, std::system_category(), what ) { } template SystemError(const std::string &what, Args&&... args) : SystemError(fmt::format(what, std::forward(args)...)) { } }; class RuntimeError : public std::runtime_error { public: template RuntimeError(const std::string &what, Args&&... args) : std::runtime_error(fmt::format(what, std::forward(args)...)) { } }; class MemoryAllocationError : public RuntimeError { public: MemoryAllocationError() : RuntimeError("Failed to allocate memory") { } }; class JsonError : public std::runtime_error { protected: const json_t *setting; json_error_t error; public: template JsonError(const json_t *s, const json_error_t &e, const std::string &what = std::string(), Args&&... args) : std::runtime_error(fmt::format(what, std::forward(args)...)), setting(s), error(e) { } virtual const char * what() const noexcept { 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: /** A setting-id referencing the setting. */ std::string id; json_t *setting; json_error_t error; std::string msg; public: template ConfigError(json_t *s, const std::string &i, const std::string &what = "Failed to parse configuration") : std::runtime_error(what), id(i), setting(s) { error.position = -1; } template ConfigError(json_t *s, const std::string &i, const std::string &what, Args&&... args) : std::runtime_error(fmt::format(what, std::forward(args)...)), id(i), setting(s) { } template ConfigError(json_t *s, const json_error_t &e, const std::string &i, const std::string &what = "Failed to parse configuration") : std::runtime_error(what), id(i), setting(s), error(e) { } template ConfigError(json_t *s, const json_error_t &e, const std::string &i, const std::string &what, Args&&... args) : std::runtime_error(fmt::format(what, std::forward(args)...)), id(i), setting(s), error(e) { } std::string docUri() const { std::string baseUri = "https://villas.fein-aachen.org/doc/jump?"; return baseUri + id; } virtual const char * what() const noexcept { if (msg.empty()) { std::stringstream ss; ss << std::runtime_error::what() << std::endl; ss << " Please consult the user documentation for details: " << docUri(); if (error.position >= 0) ss << std::endl << " " << error.text << " in " << error.source << ":" << error.line << ":" << error.column; } return msg.c_str(); } }; } /* namespace villas */