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." },
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::WAL_OVER_LIMITS, 14800047, "the WAL file size exceeds the default limit."}
36 };
37
GetJsErrorCode(int32_t errorCode)38 const std::optional<JsErrorCode> GetJsErrorCode(int32_t errorCode)
39 {
40 auto jsErrorCode = JsErrorCode{ errorCode, -1, "" };
41 auto iter = std::lower_bound(JS_ERROR_CODE_MSGS,
42 JS_ERROR_CODE_MSGS + sizeof(JS_ERROR_CODE_MSGS) / sizeof(JS_ERROR_CODE_MSGS[0]), jsErrorCode,
43 [](const JsErrorCode &jsErrorCode1, const JsErrorCode &jsErrorCode2) {
44 return jsErrorCode1.status < jsErrorCode2.status;
45 });
46 if (iter < JS_ERROR_CODE_MSGS + sizeof(JS_ERROR_CODE_MSGS) / sizeof(JS_ERROR_CODE_MSGS[0]) &&
47 iter->status == errorCode) {
48 return *iter;
49 }
50 return std::nullopt;
51 }
52
GenerateNapiError(Status status,int32_t & errCode,std::string & errMessage)53 Status GenerateNapiError(Status status, int32_t &errCode, std::string &errMessage)
54 {
55 auto errorMsg = GetJsErrorCode(status);
56 if (errorMsg.has_value()) {
57 auto napiError = errorMsg.value();
58 errCode = napiError.jsCode;
59 errMessage = napiError.message;
60 } else {
61 // unmatched status return unified error code
62 errCode = -1;
63 errMessage = "";
64 }
65 ZLOGD("GenerateNapiError errCode is %{public}d", errCode);
66 if (errCode == 0) {
67 return Status::SUCCESS;
68 }
69 return status;
70 }
71
ThrowNapiError(napi_env env,int32_t status,const std::string & errMessage,bool isParamsCheck)72 void ThrowNapiError(napi_env env, int32_t status, const std::string &errMessage, bool isParamsCheck)
73 {
74 ZLOGD("ThrowNapiError message: %{public}s", errMessage.c_str());
75 if (status == Status::SUCCESS) {
76 return;
77 }
78 auto errorMsg = GetJsErrorCode(status);
79 JsErrorCode napiError;
80 if (errorMsg.has_value()) {
81 napiError = errorMsg.value();
82 } else {
83 napiError.jsCode = -1;
84 napiError.message = "";
85 }
86
87 std::string message(napiError.message);
88 if (isParamsCheck) {
89 napiError.jsCode = 401;
90 message += errMessage;
91 }
92
93 std::string jsCode;
94 if (napiError.jsCode == -1) {
95 jsCode = "";
96 } else {
97 jsCode = std::to_string(napiError.jsCode);
98 }
99 napi_throw_error(env, jsCode.c_str(), message.c_str());
100 }
101 } // namespace OHOS::DistributedKVStore