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 "napi_preferences_error.h"
16
17 namespace OHOS {
18 namespace PreferencesJsKit {
19
20 static constexpr JsErrorCode JS_ERROR_MAPS[] = {
21 { E_NOT_STAGE_MODE, E_NOT_STAGE_MODE, "Only supported in stage mode" },
22 { E_DATA_GROUP_ID_INVALID, E_DATA_GROUP_ID_INVALID, "The data group id is not valid" },
23 { NativePreferences::E_DELETE_FILE_FAIL, E_DELETE_FILE_FAIL, "Failed to delete preferences file." },
24 { NativePreferences::E_GET_DATAOBSMGRCLIENT_FAIL, E_GET_DATAOBSMGRCLIENT_FAIL,
25 "Failed to obtain subscription service." },
26 { NativePreferences::E_NOT_SUPPORTED, E_NOT_SUPPORTED, "Capability not supported" },
27 };
28
GetJsErrorCode(int32_t errorCode)29 const std::optional<JsErrorCode> GetJsErrorCode(int32_t errorCode)
30 {
31 auto jsErrorCode = JsErrorCode{ errorCode, -1, "" };
32 auto iter = std::lower_bound(JS_ERROR_MAPS, JS_ERROR_MAPS + sizeof(JS_ERROR_MAPS) / sizeof(JS_ERROR_MAPS[0]),
33 jsErrorCode, [](const JsErrorCode &jsErrorCode1, const JsErrorCode &jsErrorCode2) {
34 return jsErrorCode1.nativeCode < jsErrorCode2.nativeCode;
35 });
36 if (iter < JS_ERROR_MAPS + sizeof(JS_ERROR_MAPS) / sizeof(JS_ERROR_MAPS[0]) && iter->nativeCode == errorCode) {
37 return *iter;
38 }
39 return std::nullopt;
40 }
41
42 } // namespace PreferencesJsKit
43 } // namespace OHOS
44