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
16 #include "jsi_api_errcode.h"
17 #include "jsi_api_common.h"
18
19 namespace OHOS {
20 namespace ACELite {
21
22 constexpr uint32_t JSI_ERR_CODE_DEFAULT_ERR = 0;
23 constexpr uint32_t JSI_ERR_CODE_OUT_OF_MEMORY = 17620001;
24 constexpr uint32_t JSI_ERR_CODE_RUNTIME_ERROR = 17620002;
25 constexpr uint32_t JSI_ERR_CODE_CRYPTO_OPERATION = 17630001;
26
27 typedef struct {
28 uint32_t errorCode;
29 const char *errorMsg;
30 } JsiErrMsg;
31
32 static JsiErrMsg g_errMsg[] = {
33 { JSI_ERR_CODE_PARAM_CHECK_FAILED, "Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;\
34 2. Incorrect parameter types; 3. Parameter verification failed." },
35 { JSI_ERR_CODE_NOT_SUPPORTED, "Capability not supported. Failed to call the API due to limited device\
36 capabilities." },
37 { JSI_ERR_CODE_OUT_OF_MEMORY, "memory error." },
38 { JSI_ERR_CODE_RUNTIME_ERROR, "runtime error." },
39 { JSI_ERR_CODE_CRYPTO_OPERATION, "crypto operation error." },
40 };
41
GetJsiErrValueByErrCode(HcfResult errCode)42 static uint32_t GetJsiErrValueByErrCode(HcfResult errCode)
43 {
44 switch (errCode) {
45 case HCF_INVALID_PARAMS:
46 return JSI_ERR_CODE_PARAM_CHECK_FAILED;
47 case HCF_NOT_SUPPORT:
48 return JSI_ERR_CODE_NOT_SUPPORTED;
49 case HCF_ERR_MALLOC:
50 return JSI_ERR_CODE_OUT_OF_MEMORY;
51 case HCF_ERR_NAPI:
52 return JSI_ERR_CODE_RUNTIME_ERROR;
53 case HCF_ERR_CRYPTO_OPERATION:
54 return JSI_ERR_CODE_CRYPTO_OPERATION;
55 default:
56 return JSI_ERR_CODE_DEFAULT_ERR;
57 }
58 }
59
ThrowErrorCodeResult(int32_t errCode)60 JSIValue ThrowErrorCodeResult(int32_t errCode)
61 {
62 for (uint32_t index = 0; index < sizeof(g_errMsg) / sizeof(g_errMsg[0]); index++) {
63 if (g_errMsg[index].errorCode == GetJsiErrValueByErrCode((HcfResult)errCode)) {
64 return JSI::CreateErrorWithCode(g_errMsg[index].errorCode, g_errMsg[index].errorMsg);
65 }
66 }
67
68 return JSI::CreateUndefined();
69 }
70
CallbackErrorCodeOrDataResult(const JSIValue thisVal,const JSIValue args,int32_t errCode,const JSIValue data)71 void CallbackErrorCodeOrDataResult(const JSIValue thisVal, const JSIValue args, int32_t errCode, const JSIValue data)
72 {
73 for (uint32_t index = 0; index < sizeof(g_errMsg) /sizeof(g_errMsg[0]); index++) {
74 if (g_errMsg[index].errorCode == GetJsiErrValueByErrCode((HcfResult)errCode)) {
75 JSIValue errObj = JSI::CreateObject();
76 JSI::SetNumberProperty(errObj, "code", g_errMsg[index].errorCode);
77 JSI::SetStringProperty(errObj, "message", g_errMsg[index].errorMsg);
78 JSIValue params[ARRAY_MAX_SIZE] = { errObj, data };
79 JsiAsyncCallback(thisVal, args, params, ARRAY_MAX_SIZE);
80 return;
81 }
82 }
83 JSIValue params[ARRAY_MAX_SIZE] = { JSI::CreateUndefined(), data };
84 JsiAsyncCallback(thisVal, args, params, ARRAY_MAX_SIZE);
85 }
86
87 } // ACELite
88 } // OHOS
89