2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
18 * @author Kyungwook Tak (k.tak@samsung.com)
22 #include "client/utils.h"
26 #include "common/audit/logger.h"
27 #include "common/exception.h"
28 #include <csr-error.h>
30 __attribute__((constructor))
31 static void init_lib(void)
33 Csr::Audit::Logger::setTag("CSR_CLIENT");
39 int exceptionGuard(const std::function<int()> &func)
43 } catch (const Exception &e) {
44 ERROR("Exception caught. code: " << e.error() << " message: " << e.what());
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;
56 ERROR("Unknown exception occured!");
57 return CSR_ERROR_SYSTEM;
61 void exceptionGuardAsync(const Callback &callbacks, void *userdata,
62 const std::function<void()> &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);
74 ERROR("Exception caught. code: " << e.error() << " message: " << e.what());
75 if (callbacks.onError != nullptr)
76 callbacks.onError(e.error(), userdata);
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);
91 ERROR("Unknown exception occured!");
92 if (callbacks.onError != nullptr)
93 callbacks.onError(CSR_ERROR_SYSTEM, userdata);