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, "The operations is supported in stage mode only." },
22     { E_DATA_GROUP_ID_INVALID, E_DATA_GROUP_ID_INVALID, "Invalid dataGroupId." },
23     { NativePreferences::E_DELETE_FILE_FAIL, E_DELETE_FILE_FAIL,
24         "Failed to delete the user preferences persistence file." },
25     { NativePreferences::E_GET_DATAOBSMGRCLIENT_FAIL, E_GET_DATAOBSMGRCLIENT_FAIL,
26         "Failed to obtain the subscription service." },
27     { NativePreferences::E_NOT_SUPPORTED, E_NOT_SUPPORTED, "Capability not supported." },
28 };
29 
GetJsErrorCode(int32_t errorCode)30 const std::optional<JsErrorCode> GetJsErrorCode(int32_t errorCode)
31 {
32     auto jsErrorCode = JsErrorCode{ errorCode, -1, "" };
33     auto iter = std::lower_bound(JS_ERROR_MAPS, JS_ERROR_MAPS + sizeof(JS_ERROR_MAPS) / sizeof(JS_ERROR_MAPS[0]),
34         jsErrorCode, [](const JsErrorCode &jsErrorCode1, const JsErrorCode &jsErrorCode2) {
35             return jsErrorCode1.nativeCode < jsErrorCode2.nativeCode;
36         });
37     if (iter < JS_ERROR_MAPS + sizeof(JS_ERROR_MAPS) / sizeof(JS_ERROR_MAPS[0]) && iter->nativeCode == errorCode) {
38         return *iter;
39     }
40     return std::nullopt;
41 }
42 
43 } // namespace PreferencesJsKit
44 } // namespace OHOS
45