Fix wrong log value
[platform/core/api/notification.git] / notification-ex / exception.h
1 /*
2  * Copyright (c) 2019 Samsung Electronics Co., Ltd.
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 #ifndef NOTIFICATION_EX_EXCEPTION_H_
18 #define NOTIFICATION_EX_EXCEPTION_H_
19
20 #include <dlog.h>
21
22 #include <string>
23 #include <exception>
24
25 #include "notification-ex/common.h"
26
27 #ifdef LOG_TAG
28 #undef LOG_TAG
29 #endif
30
31 #define LOG_TAG "NOTIFICATION_EX"
32
33 #define THROW(error_code) throw Exception(error_code, __FILE__, __LINE__)
34
35 namespace notification {
36
37 /* LCOV_EXCL_START */
38 class Exception : public std::exception {
39  public:
40   explicit Exception(int error_code, std::string file = __FILE__,
41       int line = __LINE__ ) {
42     error_code_ = error_code;
43     message_ = file.substr(file.find_last_of("/") + 1) + ":"
44         + std::to_string(line) + GetErrorString(error_code);
45     LOGE("%s", message_.c_str());
46   }
47   virtual ~Exception() {}
48   virtual const char *what(void) const noexcept {
49     return message_.c_str();
50   }
51   int GetErrorCode() {
52     return error_code_;
53   }
54
55  private:
56   int error_code_;
57   std::string message_;
58   std::string GetErrorString(int error_code) {
59     switch (error_code) {
60     case ERROR_INVALID_PARAMETER:
61       return ": INVALID_PARAMETER";
62     case ERROR_OUT_OF_MEMORY:
63       return ": OUT_OF_MEMORY";
64     case ERROR_IO_ERROR:
65       return ": IO_ERROR";
66     case ERROR_PERMISSION_DENIED:
67       return ": PERMISSION_DENIED";
68     case ERROR_FROM_DB:
69       return ": ERROR_FROM_DB";
70     case ERROR_ALREADY_EXIST_ID:
71       return ": ALREADY_EXIST_ID";
72     case ERROR_FROM_DBUS:
73       return ": ERROR_FROM_DBUS";
74     case ERROR_NOT_EXIST_ID:
75       return ": NOT_EXIST_ID";
76     case ERROR_SERVICE_NOT_READY:
77       return ": SERVICE_NOT_READY";
78     default:
79       return "";
80     }
81   }
82 };  // class Exception
83 /* LCOV_EXCL_STOP */
84
85 }  // namespace notification
86
87 #endif  // NOTIFICATION_EX_EXCEPTION_H_