ac1915f3518555b7a92dab3dde2e3bb9ed098723
[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                 callbacks.onCompleted(userdata);
67         } catch (const Exception &e) {
68                 if (e.error() == -999) {
69                         INFO("Async operation cancel exception!");
70                         callbacks.onCancelled(userdata);
71                 } else {
72                         ERROR("Exception caught. code: " << e.error() << " message: " << e.what());
73                         callbacks.onError(e.error(), userdata);
74                 }
75         } catch (const std::invalid_argument &e) {
76                 ERROR("invalid argument: " << e.what());
77                 callbacks.onError(CSR_ERROR_INVALID_PARAMETER, userdata);
78         } catch (const std::bad_alloc &e) {
79                 ERROR("memory allocation failed: " << e.what());
80                 callbacks.onError(CSR_ERROR_OUT_OF_MEMORY, userdata);
81         } catch (const std::exception &e) {
82                 ERROR("std exception: " << e.what());
83                 callbacks.onError(CSR_ERROR_SYSTEM, userdata);
84         } catch (...) {
85                 ERROR("Unknown exception occured!");
86                 callbacks.onError(CSR_ERROR_SYSTEM, userdata);
87         }
88 }
89
90 }
91 }