1 /*
2 * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development 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 "common.h"
17
18 static const char *TAG = "[nodeapi_exterrinfo]";
19
testNapiExterrinfo(napi_env env,napi_callback_info info)20 napi_value testNapiExterrinfo(napi_env env, napi_callback_info info)
21 {
22 // pages/nodeapi/datatypes/napiextendederrorinfo
23 char buffer[PARAM100];
24 size_t argc = PARAM2;
25 napi_value argv[PARAM2];
26 napi_status status;
27 const napi_extended_error_info *extended_error_info;
28
29 // 解析传入的参数
30 status = napi_get_cb_info(env, info, &argc, argv, NULL, NULL);
31 if (status != napi_ok) {
32 napi_throw_error(env, NULL, "Failed to parse arguments");
33 return NULL;
34 }
35
36 // 检查参数数量
37 if (argc < PARAM2) {
38 napi_throw_error(env, NULL, "Expected 2 arguments");
39 return NULL;
40 }
41
42 int32_t num1 = 0;
43 int32_t num2 = 0;
44 status = napi_get_value_int32(env, argv[0], &num1);
45 if (status != napi_ok) {
46 napi_throw_error(env, NULL, "Invalid first argument");
47 return NULL;
48 }
49
50 status = napi_get_value_int32(env, argv[1], &num2);
51 if (status != napi_ok) {
52 status = napi_get_last_error_info(env, &extended_error_info);
53 if (status == napi_ok && extended_error_info != NULL) {
54 const char *errorMessage =
55 extended_error_info->error_message != NULL ? extended_error_info->error_message : "Unknown error";
56 OH_LOG_Print(LOG_APP, LOG_ERROR, GLOBAL_RESMGR, TAG, "errmsg %{public}s!, engine_err_code %{public}d!.",
57 errorMessage, extended_error_info->engine_error_code);
58 std::string res = "Failed to create threadsafe function em = " + std::string(errorMessage) +
59 ", eec = " + std::to_string(extended_error_info->engine_error_code) +
60 ", ec = " + std::to_string(extended_error_info->error_code);
61 napi_throw_error(env, NULL, res.c_str());
62 }
63
64 return NULL;
65 }
66
67 // 执行加法操作
68 int32_t result = num1 + num2;
69 if (result > PARAM10) {
70 napi_throw_error(env, NULL, "Invalid result > 10.");
71 return NULL;
72 }
73
74 // 返回结果
75 napi_value resultValue;
76 status = napi_create_int32(env, result, &resultValue);
77 if (status != napi_ok) {
78 napi_throw_error(env, NULL, "Failed to create result value");
79 return NULL;
80 }
81
82 return resultValue;
83 }
84