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 "JS_ERROR_UTILS"
16 #include "js_error_utils.h"
17
18 namespace OHOS::DistributedKVStore {
19 using JsErrorCode = OHOS::DistributedKVStore::JsErrorCode;
20
21 static const std::map<int32_t, JsErrorCode> jsErrCodeMsgMap {
22 {Status::INVALID_ARGUMENT, {401, "Parameter error."}},
23 {Status::OVER_MAX_SUBSCRIBE_LIMITS, {15100001, "Over max subscribe limits."}},
24 {Status::STORE_META_CHANGED, {15100002, "Open existed database with changed options."}},
25 {Status::CRYPT_ERROR, {15100003, "Database corrupted."}},
26 {Status::NOT_FOUND, {15100004, "Not found."}},
27 {Status::ALREADY_CLOSED, {15100005, "Database or result set already closed."}},
28 {Status::STORE_ALREADY_SUBSCRIBE, {0, ""}},
29 {Status::STORE_NOT_OPEN, {0, ""}},
30 {Status::STORE_NOT_SUBSCRIBE, {0, ""}},
31 };
32
GetJsErrorCode(int32_t errorCode)33 const std::optional<JsErrorCode> GetJsErrorCode(int32_t errorCode)
34 {
35 auto iter = jsErrCodeMsgMap.find(errorCode);
36 if (iter != jsErrCodeMsgMap.end()) {
37 return iter->second;
38 }
39 return std::nullopt;
40 }
41
GenerateNapiError(Status status,int32_t & errCode,std::string & errMessage)42 Status GenerateNapiError(Status status, int32_t &errCode, std::string &errMessage)
43 {
44 auto errormsg = GetJsErrorCode(status);
45 if (errormsg.has_value()) {
46 auto napiError = errormsg.value();
47 errCode = napiError.jsCode;
48 errMessage = napiError.message;
49 } else {
50 // unmatched status return unified error code
51 errCode = -1;
52 errMessage = "";
53 }
54 ZLOGD("GenerateNapiError errCode is %{public}d", errCode);
55 if (errCode == 0) {
56 return Status::SUCCESS;
57 }
58 return status;
59 }
60
ThrowNapiError(napi_env env,int32_t status,std::string errMessage,bool isParamsCheck)61 void ThrowNapiError(napi_env env, int32_t status, std::string errMessage, bool isParamsCheck)
62 {
63 ZLOGD("ThrowNapiError message: %{public}s", errMessage.c_str());
64 if (status == Status::SUCCESS) {
65 return;
66 }
67 auto errormsg = GetJsErrorCode(status);
68 JsErrorCode napiError;
69 if (errormsg.has_value()) {
70 napiError = errormsg.value();
71 } else {
72 napiError.jsCode = -1;
73 napiError.message = "";
74 }
75
76 if (isParamsCheck) {
77 napiError.message += errMessage;
78 napiError.jsCode = 401;
79 }
80
81 std::string jsCode;
82 if (napiError.jsCode == -1) {
83 jsCode = "";
84 } else {
85 jsCode = std::to_string(napiError.jsCode);
86 }
87 napi_throw_error(env, jsCode.c_str(), napiError.message.c_str());
88 }
89 } // namespace OHOS::DistributedKVStore