2 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License
18 * @author Bartlomiej Grzelewski (b.grzelewski@samsung.com)
28 #include <ckm/ckm-error.h>
30 #include <symbol-visibility.h>
31 #include <stringify.h>
36 class COMMON_API Exception : public std::exception {
38 Exception(const char *path, const char *function, int line, const std::string &message = std::string())
40 , m_function(function)
45 virtual ~Exception() noexcept {}
47 virtual const char *what(void) const noexcept {
48 return m_message.c_str();
51 virtual std::string message(void) const {
52 std::ostringstream msg;
53 msg << "[" << m_path << ":" << m_line << " " << m_function << "()] " << m_message;
57 virtual int error(void) const = 0;
61 std::string m_function;
63 std::string m_message;
66 class DefaultExceptionLogger {
68 template <typename... Args>
69 DefaultExceptionLogger(const Args&...) {}
74 typename Stringify = StringifyAvoid,
75 typename Before = DefaultExceptionLogger,
76 typename After = DefaultExceptionLogger>
77 class COMMON_API DefineException : public Exception {
79 template<typename... Args>
80 DefineException(const char *path, const char *function, int line, const Args&... args)
81 : Exception(path, function, line, Stringify()(args...))
83 Before(m_path, m_function, m_line, DefineException<Error,Stringify,Before,After>::error(), m_message);
85 ~DefineException() noexcept {
86 After(m_path, m_function, m_line, DefineException<Error,Stringify,Before,After>::error(), m_message);
88 virtual int error(void) const {
93 class COMMON_API PrintError {
96 const std::string &path,
97 const std::string &function,
99 const std::string &message = std::string());
102 class COMMON_API PrintDebug {
105 const std::string &path,
106 const std::string &function,
108 const std::string &message = std::string());
111 typedef DefineException<CKM_API_ERROR_SERVER_ERROR,
112 Stringify, PrintError> InternalError;
113 typedef DefineException<CKM_API_ERROR_INPUT_PARAM,
114 StringifyDebug, PrintDebug> InputParam;
115 typedef DefineException<CKM_API_ERROR_DB_LOCKED,
116 Stringify, PrintError> DatabaseLocked;
117 typedef DefineException<CKM_API_ERROR_FILE_SYSTEM,
118 Stringify, PrintError> FileSystemFailed;
119 typedef DefineException<CKM_API_ERROR_AUTHENTICATION_FAILED,
120 StringifyDebug, PrintDebug> AuthenticationFailed;
125 #define ThrowErr(name, ...) \
126 throw name(__FILE__, __FUNCTION__, __LINE__, ##__VA_ARGS__);