611cad131bc541c2a862776a4d85df55869f97a8
[platform/core/security/key-manager.git] / src / manager / common / exception.h
1 /*
2  *  Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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
15  */
16 /*
17  * @file       exception.h
18  * @author     Bartlomiej Grzelewski (b.grzelewski@samsung.com)
19  * @version    1.0
20  */
21 #pragma once
22
23 #include <exception>
24 #include <string>
25 #include <iostream>
26 #include <sstream>
27
28 #include <ckm/ckm-error.h>
29
30 #include <symbol-visibility.h>
31 #include <stringify.h>
32
33 namespace CKM {
34 namespace Exc {
35
36 class COMMON_API Exception : public std::exception {
37 public:
38     Exception(const char *path, const char *function, int line, const std::string &message = std::string())
39       : m_path(path)
40       , m_function(function)
41       , m_line(line)
42       , m_message(message)
43     {}
44
45     virtual ~Exception() noexcept {}
46
47     virtual const char *what(void) const noexcept {
48         return m_message.c_str();
49     }
50
51     virtual std::string message(void) const {
52         std::ostringstream msg;
53         msg << "[" << m_path << ":" << m_line << " " << m_function << "()] " << m_message;
54         return msg.str();
55     }
56
57     virtual int error(void) const = 0;
58
59 protected:
60     std::string m_path;
61     std::string m_function;
62     int m_line;
63     std::string m_message;
64 };
65
66 class DefaultExceptionLogger {
67 public:
68     template <typename... Args>
69     DefaultExceptionLogger(const Args&...) {}
70 };
71
72 template<
73     int Error = 0,
74     typename Stringify = StringifyAvoid,
75     typename Before = DefaultExceptionLogger,
76     typename After = DefaultExceptionLogger>
77 class COMMON_API DefineException : public Exception {
78 public:
79     template<typename... Args>
80     DefineException(const char *path, const char *function, int line, const Args&... args)
81       : Exception(path, function, line, Stringify()(args...))
82     {
83         Before(m_path, m_function, m_line, DefineException<Error,Stringify,Before,After>::error(), m_message);
84     }
85     ~DefineException() noexcept {
86         After(m_path, m_function, m_line, DefineException<Error,Stringify,Before,After>::error(), m_message);
87     }
88     virtual int error(void) const {
89         return Error;
90     }
91 };
92
93 class COMMON_API PrintError {
94 public:
95     PrintError(
96         const std::string &path,
97         const std::string &function,
98         int line, int error,
99         const std::string &message = std::string());
100 };
101
102 class COMMON_API PrintDebug {
103 public:
104     PrintDebug(
105         const std::string &path,
106         const std::string &function,
107         int line, int error,
108         const std::string &message = std::string());
109 };
110
111 typedef DefineException<CKM_API_ERROR_SERVER_ERROR,
112         Stringify, PrintError> InternalError;
113 typedef DefineException<CKM_API_ERROR_INPUT_PARAM,
114         Stringify, PrintError> 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         Stringify, PrintError> AuthenticationFailed;
121
122 } // namespace Exc
123 } // namespace CKM
124
125 #define ThrowErr(name, ...) \
126   throw name(__FILE__, __FUNCTION__, __LINE__, ##__VA_ARGS__);
127