1 /*
2 * Copyright (c) 2022 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 #define LOG_TAG "JsErrorCode"
16
17 #include "js_error_utils.h"
18
19 #include <algorithm>
20
21 namespace OHOS::DistributedKVStore {
22 using JsErrorCode = OHOS::DistributedKVStore::JsErrorCode;
23
24 static constexpr JsErrorCode JS_ERROR_CODE_MSGS[] = {
25 { Status::INVALID_ARGUMENT, 401, "Parameter error: Parameters verification failed." },
26 { Status::STORE_NOT_OPEN, 0, "" },
27 { Status::STORE_ALREADY_SUBSCRIBE, 0, "" },
28 { Status::STORE_NOT_SUBSCRIBE, 0, "" },
29 { Status::NOT_FOUND, 15100004, "Not found." },
30 { Status::STORE_META_CHANGED, 15100002, "Open existed database with changed options." },
31 { Status::PERMISSION_DENIED, 202, "Permission denied" },
32 { Status::CRYPT_ERROR, 15100003, "Database corrupted." },
33 { Status::OVER_MAX_LIMITS, 15100001, "Over max limits." },
34 { Status::ALREADY_CLOSED, 15100005, "Database or result set already closed." },
35 { Status::DATA_CORRUPTED, 15100003, "Database corrupted" },
36 { Status::WAL_OVER_LIMITS, 14800047, "the WAL file size exceeds the default limit."}
37 };
38
GetJsErrorCode(int32_t errorCode)39 const std::optional<JsErrorCode> GetJsErrorCode(int32_t errorCode)
40 {
41 auto jsErrorCode = JsErrorCode{ errorCode, -1, "" };
42 auto iter = std::lower_bound(JS_ERROR_CODE_MSGS,
43 JS_ERROR_CODE_MSGS + sizeof(JS_ERROR_CODE_MSGS) / sizeof(JS_ERROR_CODE_MSGS[0]), jsErrorCode,
44 [](const JsErrorCode &jsErrorCode1, const JsErrorCode &jsErrorCode2) {
45 return jsErrorCode1.status < jsErrorCode2.status;
46 });
47 if (iter < JS_ERROR_CODE_MSGS + sizeof(JS_ERROR_CODE_MSGS) / sizeof(JS_ERROR_CODE_MSGS[0]) &&
48 iter->status == errorCode) {
49 return *iter;
50 }
51 return std::nullopt;
52 }
53
GenerateNapiError(Status status,int32_t & errCode,std::string & errMessage)54 Status GenerateNapiError(Status status, int32_t &errCode, std::string &errMessage)
55 {
56 auto errorMsg = GetJsErrorCode(status);
57 if (errorMsg.has_value()) {
58 auto napiError = errorMsg.value();
59 errCode = napiError.jsCode;
60 errMessage = napiError.message;
61 } else {
62 // unmatched status return unified error code
63 errCode = -1;
64 errMessage = "";
65 }
66 ZLOGD("GenerateNapiError errCode is %{public}d", errCode);
67 if (errCode == 0) {
68 return Status::SUCCESS;
69 }
70 return status;
71 }
72
ThrowNapiError(napi_env env,int32_t status,const std::string & errMessage,bool isParamsCheck)73 void ThrowNapiError(napi_env env, int32_t status, const std::string &errMessage, bool isParamsCheck)
74 {
75 ZLOGD("ThrowNapiError message: %{public}s", errMessage.c_str());
76 if (status == Status::SUCCESS) {
77 return;
78 }
79 auto errorMsg = GetJsErrorCode(status);
80 JsErrorCode napiError;
81 if (errorMsg.has_value()) {
82 napiError = errorMsg.value();
83 } else {
84 napiError.jsCode = -1;
85 napiError.message = "";
86 }
87
88 std::string message(napiError.message);
89 if (isParamsCheck) {
90 napiError.jsCode = 401; // 401 is parameter validation error code
91 message += errMessage;
92 }
93
94 if (napiError.jsCode == -1) {
95 napi_throw_error(env, "", message.c_str());
96 return;
97 }
98
99 napi_value errorObj = nullptr;
100 napi_value errorCode = nullptr;
101 napi_value errorMessage = nullptr;
102 napi_create_int32(env, napiError.jsCode, &errorCode);
103 napi_create_string_utf8(env, message.c_str(), NAPI_AUTO_LENGTH, &errorMessage);
104 napi_create_error(env, errorCode, errorMessage, &errorObj);
105 napi_throw(env, errorObj);
106 }
107 } // namespace OHOS::DistributedKVStore