Fix log level of NOT_INSTALLED as INFO
[platform/core/security/trust-anchor.git] / src / exception.cpp
1 /*
2  *  Copyright (c) 2017 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.cpp
18  * @author      Sangwan Kwon (sangwan.kwon@samsung.com)
19  * @version     1.0
20  * @brief       Exception guard and custom exceptions
21  */
22 #include "exception.hxx"
23
24 #include <klay/exception.h>
25
26 #include "logger.hxx"
27
28 namespace tanchor {
29
30 int exceptionGuard(const std::function<int()> &func)
31 {
32         try {
33                 return func();
34         } catch (const tanchor::Exception &e) {
35                 std::string errStr;
36                 switch (e.error()) {
37                 case ENOENT:
38                         errStr = "No such file or directory.";
39                         break;
40                 case ENOMEM:
41                         errStr = "Out of memory.";
42                         break;
43                 case EACCES:
44                 case EPERM:
45                         errStr = "Permission denied.";
46                         break;
47                 default:
48                         errStr = "Internal error.";
49                         break;
50                 }
51
52                 if (e.error() == TRUST_ANCHOR_ERROR_NOT_INSTALLED)
53                         INFO(SINK, e.what());
54                 else
55                         ERROR(SINK, errStr + e.what());
56
57                 return e.error();
58         } catch (const runtime::Exception &e) {
59                 ERROR(SINK, e.what());
60                 return TRUST_ANCHOR_ERROR_INTERNAL;
61         } catch (const std::invalid_argument &e) {
62                 ERROR(SINK, e.what());
63                 return TRUST_ANCHOR_ERROR_INVALID_PARAMETER;
64         } catch (const std::logic_error &e) {
65                 ERROR(SINK, e.what());
66                 return TRUST_ANCHOR_ERROR_INTERNAL;
67         } catch (const std::exception &e) {
68                 ERROR(SINK, e.what());
69                 return TRUST_ANCHOR_ERROR_INTERNAL;
70         } catch (...) {
71                 ERROR(SINK, "Unknown exception occurred.");
72                 return TRUST_ANCHOR_ERROR_INTERNAL;
73         }
74 }
75
76 Exception::Exception(int ec, const char *file, const char *function,
77                                          unsigned int line, const std::string &message) noexcept :
78         m_ec(ec),
79         m_message(FORMAT("[" << file << ":" << line << " " <<
80                                          function << "()]" << message))
81 {
82 }
83
84 const char *Exception::what() const noexcept
85 {
86         return this->m_message.c_str();
87 }
88
89 int Exception::error(void) const noexcept
90 {
91         return this->m_ec;
92 }
93
94 } // namespace tanchor