Add internal api to support C#
[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 class Exception : public std::exception {
38  public:
39   explicit Exception(int error_code, std::string file = __FILE__,
40       int line = __LINE__ ) {
41     error_code_ = error_code;
42     message_ = file.substr(file.find_last_of("/") + 1) + ":"
43         + std::to_string(line) + GetErrorString(error_code);
44     LOGE("%s", message_.c_str());
45   }
46   virtual ~Exception() {}
47   virtual const char *what(void) const noexcept {
48     return message_.c_str();
49   }
50   int GetErrorCode() {
51     return error_code_;
52   }
53
54  private:
55   int error_code_;
56   std::string message_;
57   std::string GetErrorString(int error_code) {
58     switch (error_code) {
59     case ERROR_INVALID_PARAMETER:
60       return ": INVALID_PARAMETER";
61     case ERROR_OUT_OF_MEMORY:
62       return ": OUT_OF_MEMORY";
63     case ERROR_IO_ERROR:
64       return ": IO_ERROR";
65     case ERROR_PERMISSION_DENIED:
66       return ": PERMISSION_DENIED";
67     case ERROR_INVALID_OPERATION:
68       return ": INVALID_OPERATION";
69     case ERROR_FROM_DB:
70       return ": ERROR_FROM_DB";
71     case ERROR_ALREADY_EXIST_ID:
72       return ": ALREADY_EXIST_ID";
73     case ERROR_FROM_DBUS:
74       return ": ERROR_FROM_DBUS";
75     case ERROR_NOT_EXIST_ID:
76       return ": NOT_EXIST_ID";
77     case ERROR_SERVICE_NOT_READY:
78       return ": SERVICE_NOT_READY";
79     case ERROR_MAX_EXCEEDED:
80       return ": MAX_EXCEEDED";
81     default:
82       return "";
83     }
84   }
85 };  // class Exception
86
87 }  // namespace notification
88
89 #endif  // NOTIFICATION_EX_EXCEPTION_H_