Branch data Line data Source code
1 : : /* 2 : : ** Copyright (C) 2020 CERN 3 : : ** 4 : : ** This software is provided 'as-is', without any express or implied 5 : : ** warranty. In no event will the authors be held liable for any damages 6 : : ** arising from the use of this software. 7 : : ** 8 : : ** Permission is granted to anyone to use this software for any purpose, 9 : : ** including commercial applications, and to alter it and redistribute it 10 : : ** freely, subject to the following restrictions: 11 : : ** 12 : : ** 1. The origin of this software must not be misrepresented; you must not 13 : : ** claim that you wrote the original software. If you use this software 14 : : ** in a product, an acknowledgment in the product documentation would be 15 : : ** appreciated but is not required. 16 : : ** 2. Altered source versions must be plainly marked as such, and must not be 17 : : ** misrepresented as being the original software. 18 : : ** 3. This notice may not be removed or altered from any source distribution. 19 : : ** 20 : : ** Created on: 2021-10-18T18:44:27+02:00 21 : : ** Author: Matteo Ferrari <matteof> <matteo.ferrari.1@cern.ch> 22 : : ** 23 : : */ 24 : : 25 : : #ifndef EXCEPTION_HPP_ 26 : : #define EXCEPTION_HPP_ 27 : : 28 : : #include <string> 29 : : #include <system_error> 30 : : 31 : : #include <asm-generic/errno-base.h> 32 : : 33 : : namespace ccut { 34 : : 35 : : enum class ErrorCode 36 : : { 37 : : Ok = 0, 38 : : NotPermitted = EPERM, /**< 1 operation not permitted */ 39 : : Interrupted = EINTR, /**< 4 interrupted */ 40 : : IO = EIO, /**< 5 io error */ 41 : : Denied = EACCES, /**< 13 permission denied */ 42 : : Runtime = EFAULT, /**< 14 runtime error */ 43 : : InvalidArguments = EINVAL, /**< 22 invalid argument */ 44 : : Overflow = EOVERFLOW, /**< 75 overflow error */ 45 : : Timeout = ETIMEDOUT, /**< 110 timeout error */ 46 : : Canceled = ECANCELED, /**< 125 operation aborted */ 47 : : NotSupported = ENOTSUP, 48 : : 49 : : InternalError = 1000, /*< internal error */ 50 : : }; 51 : : 52 : : /** 53 : : * Represents an exception generated by CCUT library 54 : : */ 55 : : class Exception : public std::exception 56 : : { 57 : : public: 58 : : Exception() = delete; 59 : : explicit Exception(std::error_code ev); 60 : : Exception(std::error_code ev, const std::string &what); 61 : : 62 : 34 : inline const std::error_code getErrorCode() const noexcept 63 : : { 64 : 34 : return m_errorCode; 65 : : } 66 : : 67 : : /** 68 : : * Overload of std::exception 69 : : * @return Error message 70 : : */ 71 : : const char *what() const noexcept override; 72 : : 73 : : static const std::string Category; 74 : : 75 : : protected: 76 : : const std::error_code m_errorCode; //!< Error code 77 : : mutable std::string m_what; //!< Complete error message 78 : : }; 79 : : 80 : : std::error_code make_error_code(ccut::ErrorCode); 81 : : 82 : : Exception make_errno_exception(); 83 : : 84 : : } // namespace ccut 85 : : 86 : : namespace std { 87 : : 88 : : template<> 89 : : struct is_error_code_enum<ccut::ErrorCode> : true_type 90 : : {}; 91 : : 92 : : } /* namespace std */ 93 : : 94 : : #endif /* EXCEPTION_HPP_ */