Call callbacks only when callable on async client
[platform/upstream/csr-framework.git] / src / framework / client / utils.cpp
1 /*
2  *  Copyright (c) 2016 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        utils.cpp
18  * @author      Kyungwook Tak (k.tak@samsung.com)
19  * @version     1.0
20  * @brief       Utils
21  */
22 #include "client/utils.h"
23
24 #include <exception>
25
26 #include "common/audit/logger.h"
27 #include "common/exception.h"
28 #include <csr-error.h>
29
30 __attribute__((constructor))
31 static void init_lib(void)
32 {
33         Csr::Audit::Logger::setTag("CSR_CLIENT");
34 }
35
36 namespace Csr {
37 namespace Client {
38
39 int exceptionGuard(const std::function<int()> &func)
40 {
41         try {
42                 return func();
43         } catch (const Exception &e) {
44                 ERROR("Exception caught. code: " << e.error() << " message: " << e.what());
45                 return e.error();
46         } catch (const std::invalid_argument &e) {
47                 ERROR("invalid argument: " << e.what());
48                 return CSR_ERROR_INVALID_PARAMETER;
49         } catch (const std::bad_alloc &e) {
50                 ERROR("memory allocation failed: " << e.what());
51                 return CSR_ERROR_OUT_OF_MEMORY;
52         } catch (const std::exception &e) {
53                 ERROR("std exception: " << e.what());
54                 return CSR_ERROR_SYSTEM;
55         } catch (...) {
56                 ERROR("Unknown exception occured!");
57                 return CSR_ERROR_SYSTEM;
58         }
59 }
60
61 void exceptionGuardAsync(const Callback &callbacks, void *userdata,
62                                                  const std::function<void()> &func)
63 {
64         try {
65                 func();
66                 if (callbacks.onCompleted != nullptr)
67                         callbacks.onCompleted(userdata);
68         } catch (const Exception &e) {
69                 if (e.error() == -999) {
70                         INFO("Async operation cancel exception!");
71                         if (callbacks.onCancelled != nullptr)
72                                 callbacks.onCancelled(userdata);
73                 } else {
74                         ERROR("Exception caught. code: " << e.error() << " message: " << e.what());
75                         if (callbacks.onError != nullptr)
76                                 callbacks.onError(e.error(), userdata);
77                 }
78         } catch (const std::invalid_argument &e) {
79                 ERROR("invalid argument: " << e.what());
80                 if (callbacks.onError != nullptr)
81                         callbacks.onError(CSR_ERROR_INVALID_PARAMETER, userdata);
82         } catch (const std::bad_alloc &e) {
83                 ERROR("memory allocation failed: " << e.what());
84                 if (callbacks.onError != nullptr)
85                         callbacks.onError(CSR_ERROR_OUT_OF_MEMORY, userdata);
86         } catch (const std::exception &e) {
87                 ERROR("std exception: " << e.what());
88                 if (callbacks.onError != nullptr)
89                         callbacks.onError(CSR_ERROR_SYSTEM, userdata);
90         } catch (...) {
91                 ERROR("Unknown exception occured!");
92                 if (callbacks.onError != nullptr)
93                         callbacks.onError(CSR_ERROR_SYSTEM, userdata);
94         }
95 }
96
97 }
98 }