1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include <map>
16 #include "convertor_error_code.h"
17 #include "oh_preferences_err_code.h"
18 #include "preferences_errno.h"
19
20 namespace OHOS::PreferencesNdk {
21
22 struct NdkErrCode {
23 int nativeCode;
24 int ndkCode;
25 };
26
27 const std::map<int, int> ERROR_CODE_MAP = {
28 { OHOS::NativePreferences::E_OK, OH_Preferences_ErrCode::PREFERENCES_OK },
29 { OHOS::NativePreferences::E_INVALID_ARGS, OH_Preferences_ErrCode::PREFERENCES_ERROR_INVALID_PARAM },
30 { OHOS::NativePreferences::E_KEY_EMPTY, OH_Preferences_ErrCode::PREFERENCES_ERROR_INVALID_PARAM },
31 { OHOS::NativePreferences::E_KEY_EXCEED_MAX_LENGTH, OH_Preferences_ErrCode::PREFERENCES_ERROR_INVALID_PARAM },
32 { OHOS::NativePreferences::E_VALUE_EXCEED_MAX_LENGTH, OH_Preferences_ErrCode::PREFERENCES_ERROR_INVALID_PARAM },
33 { OHOS::NativePreferences::E_NOT_SUPPORTED, OH_Preferences_ErrCode::PREFERENCES_ERROR_NOT_SUPPORTED },
34 { OHOS::NativePreferences::E_DELETE_FILE_FAIL, OH_Preferences_ErrCode::PREFERENCES_ERROR_DELETE_FILE },
35 { OHOS::NativePreferences::E_GET_DATAOBSMGRCLIENT_FAIL,
36 OH_Preferences_ErrCode::PREFERENCES_ERROR_GET_DATAOBSMGRCLIENT },
37 { OHOS::NativePreferences::E_NO_DATA, OH_PREFERENCES_ERR_CODE_H::PREFERENCES_ERROR_KEY_NOT_FOUND },
38 { OHOS::NativePreferences::E_OBSERVER_RESERVE, OH_Preferences_ErrCode::PREFERENCES_OK }
39 };
40
NativeErrToNdk(int nativeCode)41 int ConvertorErrorCode::NativeErrToNdk(int nativeCode)
42 {
43 auto iter = ERROR_CODE_MAP.find(nativeCode);
44 if (iter != ERROR_CODE_MAP.end()) {
45 return iter->second;
46 }
47 return PREFERENCES_ERROR_STORAGE;
48 }
49 }