From ac40fe2f743f6515cb00cb5abbf8b1b00eac0053 Mon Sep 17 00:00:00 2001 From: cff-gite Date: Sat, 25 May 2024 15:04:16 +0800 Subject: [PATCH 1/8] sensor error code rectification Signed-off-by: cff-gite Change-Id: I9943974da9a87bcc3d7bbdb6d9c9ef707025ba68 --- frameworks/js/napi/src/sensor_js.cpp | 166 +++++++++---------- frameworks/js/napi/src/sensor_napi_error.cpp | 12 +- frameworks/js/napi/src/sensor_napi_utils.cpp | 3 +- 3 files changed, 92 insertions(+), 89 deletions(-) diff --git a/frameworks/js/napi/src/sensor_js.cpp b/frameworks/js/napi/src/sensor_js.cpp index 655b95c1..ac5bc5c6 100644 --- a/frameworks/js/napi/src/sensor_js.cpp +++ b/frameworks/js/napi/src/sensor_js.cpp @@ -237,7 +237,7 @@ static void UpdateOnceCallback(napi_env env, int32_t sensorTypeId, napi_value ca CHKPV(asyncCallbackInfo); napi_status status = napi_create_reference(env, callback, 1, &asyncCallbackInfo->callback[0]); if (status != napi_ok) { - ThrowErr(env, PARAMETER_ERROR, "napi_create_reference fail"); + ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "napi_create_reference fail"); return; } std::vector> callbackInfos = g_onceCallbackInfos[sensorTypeId]; @@ -253,23 +253,23 @@ static napi_value Once(napi_env env, napi_callback_info info) napi_value thisVar = nullptr; napi_status status = napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr); if (status != napi_ok || argc < 2) { - ThrowErr(env, PARAMETER_ERROR, "napi_get_cb_info fail or number of parameter invalid"); + ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Must be greater than 2"); return nullptr; } if ((!IsMatchType(env, args[0], napi_number)) || (!IsMatchType(env, args[1], napi_function))) { - ThrowErr(env, PARAMETER_ERROR, "Wrong argument type"); + ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "args[0] is napi_number and args[1] is napi_function"); return nullptr; } int32_t sensorTypeId = INVALID_SENSOR_ID; if (!GetNativeInt32(env, args[0], sensorTypeId)) { - ThrowErr(env, PARAMETER_ERROR, "Wrong argument type, get number fail"); + ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "args[0] should be sensorTypeId"); return nullptr; } if (!CheckSubscribe(sensorTypeId)) { SEN_HILOGD("No subscription to change sensor data, registration is required"); int32_t ret = SubscribeSensor(sensorTypeId, REPORTING_INTERVAL, DataCallbackImpl); if (ret != ERR_OK) { - ThrowErr(env, ret, "SubscribeSensor fail"); + ThrowErr(env, ret, "Parameter verification failed", "ret must be positive"); return nullptr; } } @@ -309,7 +309,7 @@ static void UpdateCallbackInfos(napi_env env, int32_t sensorTypeId, napi_value c CHKPV(asyncCallbackInfo); napi_status status = napi_create_reference(env, callback, 1, &asyncCallbackInfo->callback[0]); if (status != napi_ok) { - ThrowErr(env, PARAMETER_ERROR, "napi_create_reference fail"); + ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "napi_create_reference fail"); return; } std::vector> callbackInfos = g_onCallbackInfos[sensorTypeId]; @@ -353,16 +353,16 @@ static napi_value On(napi_env env, napi_callback_info info) napi_value thisVar = nullptr; napi_status status = napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr); if (status != napi_ok || argc < 2) { - ThrowErr(env, PARAMETER_ERROR, "napi_get_cb_info fail or number of parameter invalid"); + ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "must be greater than 2"); return nullptr; } if ((!IsMatchType(env, args[0], napi_number)) || (!IsMatchType(env, args[1], napi_function))) { - ThrowErr(env, PARAMETER_ERROR, "Wrong argument type"); + ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "args[0] is napi_number and args[1] is napi_function"); return nullptr; } int32_t sensorTypeId = INVALID_SENSOR_ID; if (!GetNativeInt32(env, args[0], sensorTypeId)) { - ThrowErr(env, PARAMETER_ERROR, "Wrong argument type, get number fail"); + ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "args[0] should is sensorTypeId"); return nullptr; } int64_t interval = REPORTING_INTERVAL; @@ -374,7 +374,7 @@ static napi_value On(napi_env env, napi_callback_info info) SEN_HILOGD("Interval is %{public}" PRId64, interval); int32_t ret = SubscribeSensor(sensorTypeId, interval, DataCallbackImpl); if (ret != ERR_OK) { - ThrowErr(env, ret, "SubscribeSensor fail"); + ThrowErr(env, ret, "Parameter verification failed", "Must be positive"); return nullptr; } UpdateCallbackInfos(env, sensorTypeId, args[1]); @@ -443,12 +443,12 @@ static napi_value Off(napi_env env, napi_callback_info info) napi_value thisVar = nullptr; napi_status status = napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr); if (status != napi_ok || argc < 1) { - ThrowErr(env, PARAMETER_ERROR, "napi_get_cb_info fail or number of parameter invalid"); + ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Must be greater than 1"); return nullptr; } int32_t sensorTypeId = INVALID_SENSOR_ID; if ((!IsMatchType(env, args[0], napi_number)) || (!GetNativeInt32(env, args[0], sensorTypeId))) { - ThrowErr(env, PARAMETER_ERROR, "Wrong argument type or get number fail"); + ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "args[0] should be sensorTypeId"); return nullptr; } int32_t subscribeSize = -1; @@ -459,7 +459,7 @@ static napi_value Off(napi_env env, napi_callback_info info) } else if (IsMatchType(env, args[1], napi_function)) { subscribeSize = RemoveCallback(env, sensorTypeId, args[1]); } else { - ThrowErr(env, PARAMETER_ERROR, "Wrong argument type, args[1] should is napi_function"); + ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "args[1] should be napi_function"); return nullptr; } if (CheckSystemSubscribe(sensorTypeId) || (subscribeSize > 0)) { @@ -468,7 +468,7 @@ static napi_value Off(napi_env env, napi_callback_info info) } int32_t ret = UnsubscribeSensor(sensorTypeId); if (ret == PARAMETER_ERROR || ret == PERMISSION_DENIED) { - ThrowErr(env, ret, "UnsubscribeSensor fail"); + ThrowErr(env, ret, "Parameter verification failed", "UnsubscribeSensor fail"); } return nullptr; } @@ -504,46 +504,46 @@ static napi_value GetGeomagneticField(napi_env env, napi_callback_info info) napi_value thisVar = nullptr; napi_status status = napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr); if (status != napi_ok || argc < 2) { - ThrowErr(env, PARAMETER_ERROR, "napi_get_cb_info fail or number of parameter invalid"); + ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Must be greater than 2"); return nullptr; } if ((!IsMatchType(env, args[0], napi_object)) || (!IsMatchType(env, args[1], napi_number))) { - ThrowErr(env, PARAMETER_ERROR, "Wrong argument type"); + ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "args[0] is args[0] and args[1] is napi_number"); return nullptr; } napi_value napiLatitude = GetNamedProperty(env, args[0], "latitude"); if (napiLatitude == nullptr) { - ThrowErr(env, PARAMETER_ERROR, "napiLatitude is null"); + ThrowErr(env, PARAMETER_ERROR, "napiLatitude is null", "Must be positive"); return nullptr; } double latitude = 0; if (!GetNativeDouble(env, napiLatitude, latitude)) { - ThrowErr(env, PARAMETER_ERROR, "Get latitude fail"); + ThrowErr(env, PARAMETER_ERROR, "Get latitude fail", "The positive value of type double"); return nullptr; } napi_value napiLongitude = GetNamedProperty(env, args[0], "longitude"); if (napiLongitude == nullptr) { - ThrowErr(env, PARAMETER_ERROR, "napiLongitude is null"); + ThrowErr(env, PARAMETER_ERROR, "napiLongitude is null", "Must be positive"); return nullptr; } double longitude = 0; if (!GetNativeDouble(env, napiLongitude, longitude)) { - ThrowErr(env, PARAMETER_ERROR, "Get longitude fail"); + ThrowErr(env, PARAMETER_ERROR, "Get longitude fail", "The positive value of type double"); return nullptr; } napi_value napiAltitude = GetNamedProperty(env, args[0], "altitude"); if (napiAltitude == nullptr) { - ThrowErr(env, PARAMETER_ERROR, "napiAltitude is null"); + ThrowErr(env, PARAMETER_ERROR, "napiAltitude is null", "Must be positive"); return nullptr; } double altitude = 0; if (!GetNativeDouble(env, napiAltitude, altitude)) { - ThrowErr(env, PARAMETER_ERROR, "Get altitude fail"); + ThrowErr(env, PARAMETER_ERROR, "Get altitude fail", "The positive value of type double"); return nullptr; } int64_t timeMillis = 0; if (!GetNativeInt64(env, args[1], timeMillis)) { - ThrowErr(env, PARAMETER_ERROR, "Get timeMillis fail"); + ThrowErr(env, PARAMETER_ERROR, "Get timeMillis fail", "The positive value of type int64_t"); return nullptr; } GeomagneticField geomagneticField(latitude, longitude, altitude, timeMillis); @@ -583,41 +583,41 @@ static napi_value TransformCoordinateSystem(napi_env env, napi_callback_info inf napi_value thisVar = nullptr; napi_status status = napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr); if (status != napi_ok || argc < 2) { - ThrowErr(env, PARAMETER_ERROR, "napi_get_cb_info fail or number of parameter invalid"); + ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Must be greater than 2"); return nullptr; } if ((!IsMatchArrayType(env, args[0])) || (!IsMatchType(env, args[1], napi_object))) { - ThrowErr(env, PARAMETER_ERROR, "Wrong argument type"); + ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "args[0] is array and args[1] is napi_object"); return nullptr; } std::vector inRotationVector; if (!GetFloatArray(env, args[0], inRotationVector)) { - ThrowErr(env, PARAMETER_ERROR, "Get inRotationVector fail"); + ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Get inRotationVector fail"); return nullptr; } size_t length = inRotationVector.size(); if ((length != DATA_LENGTH) && (length != THREE_DIMENSIONAL_MATRIX_LENGTH)) { - ThrowErr(env, PARAMETER_ERROR, "Wrong inRotationVector length"); + ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Wrong inRotationVector length"); return nullptr; } napi_value napiAxisX = GetAxisX(env, args[1]); if (napiAxisX == nullptr) { - ThrowErr(env, PARAMETER_ERROR, "napiAxisX is null"); + ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "napiAxisX must be positive"); return nullptr; } int32_t axisX = 0; if (!GetNativeInt32(env, napiAxisX, axisX)) { - ThrowErr(env, PARAMETER_ERROR, "Get axisY fail"); + ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Parameter verification failed"); return nullptr; } napi_value napiAxisY = GetAxisY(env, args[1]); if (napiAxisY == nullptr) { - ThrowErr(env, PARAMETER_ERROR, "napiAxisY is null"); + ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "napiAxisY must be positive"); return nullptr; } int32_t axisY = 0; if (!GetNativeInt32(env, napiAxisY, axisY)) { - ThrowErr(env, PARAMETER_ERROR, "Get axisY fail"); + ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Parameter verification failed"); return nullptr; } sptr asyncCallbackInfo = @@ -627,7 +627,7 @@ static napi_value TransformCoordinateSystem(napi_env env, napi_callback_info inf SensorAlgorithm sensorAlgorithm; int32_t ret = sensorAlgorithm.TransformCoordinateSystem(inRotationVector, axisX, axisY, outRotationVector); if (ret != OHOS::ERR_OK) { - ThrowErr(env, ret, "Transform coordinate system fail"); + ThrowErr(env, ret, "Parameter verification failed", "Must be positive"); return nullptr; } else { for (size_t i = 0; i < length; ++i) { @@ -649,15 +649,15 @@ static napi_value GetAngleModify(napi_env env, napi_callback_info info) napi_value thisVar = nullptr; napi_status status = napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr); if (status != napi_ok || argc < 2) { - ThrowErr(env, PARAMETER_ERROR, "napi_get_cb_info fail or number of parameter invalid"); + ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Must be greater than 2"); return nullptr; } if (!IsMatchArrayType(env, args[0])) { - ThrowErr(env, PARAMETER_ERROR, "Wrong argument type, should be array"); + ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Wrong argument type"); return nullptr; } if (!IsMatchArrayType(env, args[1])) { - ThrowErr(env, PARAMETER_ERROR, "Wrong argument type, should be array"); + ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Wrong argument type"); return nullptr; } sptr asyncCallbackInfo = @@ -665,19 +665,19 @@ static napi_value GetAngleModify(napi_env env, napi_callback_info info) CHKPP(asyncCallbackInfo); std::vector curRotationVector; if (!GetFloatArray(env, args[0], curRotationVector)) { - ThrowErr(env, PARAMETER_ERROR, "Get curRotationVector fail"); + ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Get curRotationVector fail"); return nullptr; } std::vector preRotationVector; if (!GetFloatArray(env, args[1], preRotationVector)) { - ThrowErr(env, PARAMETER_ERROR, "Get preRotationVector fail"); + ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Get preRotationVector fail"); return nullptr; } std::vector angleChange(ROTATION_VECTOR_LENGTH); SensorAlgorithm sensorAlgorithm; int32_t ret = sensorAlgorithm.GetAngleModify(curRotationVector, preRotationVector, angleChange); if (ret != OHOS::ERR_OK) { - ThrowErr(env, ret, "Get angle modify fail"); + ThrowErr(env, ret, "Parameter verification failed", "Must be positive"); return nullptr; } else { asyncCallbackInfo->data.reserveData.length = ROTATION_VECTOR_LENGTH; @@ -699,11 +699,11 @@ static napi_value GetDirection(napi_env env, napi_callback_info info) napi_value thisVar = nullptr; napi_status status = napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr); if (status != napi_ok || argc < 1) { - ThrowErr(env, PARAMETER_ERROR, "napi_get_cb_info fail or number of parameter invalid"); + ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Must be greater than 1"); return nullptr; } if (!IsMatchArrayType(env, args[0])) { - ThrowErr(env, PARAMETER_ERROR, "Wrong argument type, should be array"); + ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Wrong argument type"); return nullptr; } sptr asyncCallbackInfo = @@ -711,14 +711,14 @@ static napi_value GetDirection(napi_env env, napi_callback_info info) CHKPP(asyncCallbackInfo); std::vector rotationMatrix; if (!GetFloatArray(env, args[0], rotationMatrix)) { - ThrowErr(env, PARAMETER_ERROR, "Get rotationMatrix fail"); + ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Get rotationMatrix fail"); return nullptr; } std::vector rotationAngle(ROTATION_VECTOR_LENGTH); SensorAlgorithm sensorAlgorithm; int32_t ret = sensorAlgorithm.GetDirection(rotationMatrix, rotationAngle); if (ret != OHOS::ERR_OK) { - ThrowErr(env, ret, "Get direction fail"); + ThrowErr(env, ret, "Parameter verification failed", "Must be positive"); return nullptr; } else { asyncCallbackInfo->data.reserveData.length = ROTATION_VECTOR_LENGTH; @@ -740,11 +740,11 @@ static napi_value CreateQuaternion(napi_env env, napi_callback_info info) napi_value thisVar = nullptr; napi_status status = napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr); if (status != napi_ok || argc < 1) { - ThrowErr(env, PARAMETER_ERROR, "napi_get_cb_info failed or number of parameter invalid"); + ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Must be greater than 1"); return nullptr; } if (!IsMatchArrayType(env, args[0])) { - ThrowErr(env, PARAMETER_ERROR, "Wrong argument type, should be array"); + ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Wrong argument type"); return nullptr; } sptr asyncCallbackInfo = @@ -752,14 +752,14 @@ static napi_value CreateQuaternion(napi_env env, napi_callback_info info) CHKPP(asyncCallbackInfo); std::vector rotationVector; if (!GetFloatArray(env, args[0], rotationVector)) { - ThrowErr(env, PARAMETER_ERROR, "Get rotationVector failed"); + ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Get rotationVector failed"); return nullptr; } std::vector quaternion(QUATERNION_LENGTH); SensorAlgorithm sensorAlgorithm; int32_t ret = sensorAlgorithm.CreateQuaternion(rotationVector, quaternion); if (ret != OHOS::ERR_OK) { - ThrowErr(env, ret, "CreateQuaternion fail"); + ThrowErr(env, ret, "Parameter verification failed", "Must be positive"); return nullptr; } else { asyncCallbackInfo->data.reserveData.length = QUATERNION_LENGTH; @@ -781,11 +781,11 @@ static napi_value GetAltitude(napi_env env, napi_callback_info info) napi_value thisVar = nullptr; napi_status status = napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr); if (status != napi_ok || argc < 2) { - ThrowErr(env, PARAMETER_ERROR, "napi_get_cb_info fail or number of parameter invalid"); + ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Must be greater than 3"); return nullptr; } if ((!IsMatchType(env, args[0], napi_number)) || (!IsMatchType(env, args[1], napi_number))) { - ThrowErr(env, PARAMETER_ERROR, "Wrong argument type"); + ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "args[0] and args[1] is napi_number"); return nullptr; } sptr asyncCallbackInfo = @@ -793,19 +793,19 @@ static napi_value GetAltitude(napi_env env, napi_callback_info info) CHKPP(asyncCallbackInfo); float seaPressure = 0; if (!GetNativeFloat(env, args[0], seaPressure)) { - ThrowErr(env, PARAMETER_ERROR, "Wrong argument type, get seaPressure fail"); + ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Should be of type float"); return nullptr; } float currentPressure = 0; if (!GetNativeFloat(env, args[1], currentPressure)) { - ThrowErr(env, PARAMETER_ERROR, "Wrong argument type, get currentPressure fail"); + ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Should be of type float"); return nullptr; } float altitude = 0; SensorAlgorithm sensorAlgorithm; int32_t ret = sensorAlgorithm.GetAltitude(seaPressure, currentPressure, &altitude); if (ret != OHOS::ERR_OK) { - ThrowErr(env, ret, "Get altitude fail"); + ThrowErr(env, ret, "Parameter verification failed", "Must be positive"); return nullptr; } else { asyncCallbackInfo->data.reserveData.reserve[0] = altitude; @@ -824,11 +824,11 @@ static napi_value GetGeomagneticDip(napi_env env, napi_callback_info info) napi_value thisVar = nullptr; napi_status status = napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr); if (status != napi_ok || argc < 1) { - ThrowErr(env, PARAMETER_ERROR, "napi_get_cb_info fail or number of parameter invalid"); + ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Must be greater than 2"); return nullptr; } if (!IsMatchArrayType(env, args[0])) { - ThrowErr(env, PARAMETER_ERROR, "Wrong argument type, should be array"); + ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Wrong argument type"); return nullptr; } sptr asyncCallbackInfo = @@ -836,14 +836,14 @@ static napi_value GetGeomagneticDip(napi_env env, napi_callback_info info) CHKPP(asyncCallbackInfo); std::vector inclinationMatrix; if (!GetFloatArray(env, args[0], inclinationMatrix)) { - ThrowErr(env, PARAMETER_ERROR, "Get inclinationMatrix fail"); + ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Get inclinationMatrix fail"); return nullptr; } float geomagneticDip = 0; SensorAlgorithm sensorAlgorithm; int32_t ret = sensorAlgorithm.GetGeomagneticDip(inclinationMatrix, &geomagneticDip); if (ret != OHOS::ERR_OK) { - ThrowErr(env, ret, "Get geomagnetic dip fail"); + ThrowErr(env, ret, "Parameter verification failed", "Must be positive"); return nullptr; } else { asyncCallbackInfo->data.reserveData.reserve[0] = geomagneticDip; @@ -858,17 +858,17 @@ static napi_value CreateRotationAndInclination(const napi_env &env, napi_value a { CALL_LOG_ENTER; if (argc < 2) { - ThrowErr(env, PARAMETER_ERROR, "The number of parameters is not valid"); + ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Must be greater than 2"); return nullptr; } std::vector gravity; if (!GetFloatArray(env, args[0], gravity)) { - ThrowErr(env, PARAMETER_ERROR, "Get gravity fail"); + ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Get gravity fail"); return nullptr; } std::vector geomagnetic; if (!GetFloatArray(env, args[1], geomagnetic)) { - ThrowErr(env, PARAMETER_ERROR, "Get geomagnetic fail"); + ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Get geomagnetic fail"); return nullptr; } std::vector rotation(THREE_DIMENSIONAL_MATRIX_LENGTH); @@ -879,7 +879,7 @@ static napi_value CreateRotationAndInclination(const napi_env &env, napi_value a SensorAlgorithm sensorAlgorithm; int32_t ret = sensorAlgorithm.CreateRotationAndInclination(gravity, geomagnetic, rotation, inclination); if (ret != OHOS::ERR_OK) { - ThrowErr(env, ret, "Create rotation and inclination matrix fail"); + ThrowErr(env, ret, "Create rotation and inclination matrix fail", "Must be positive"); return nullptr; } else { asyncCallbackInfo->data.reserveData.length = THREE_DIMENSIONAL_MATRIX_LENGTH; @@ -900,12 +900,12 @@ static napi_value GetRotationMatrix(const napi_env &env, napi_value args[], size { CALL_LOG_ENTER; if (argc < 1) { - ThrowErr(env, PARAMETER_ERROR, "The number of parameters is not valid"); + ThrowErr(env, PARAMETER_ERROR, "The number of parameters is not valid", "Must be greater than 1"); return nullptr; } std::vector rotationVector; if (!GetFloatArray(env, args[0], rotationVector)) { - ThrowErr(env, PARAMETER_ERROR, "Get rotationVector fail"); + ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Get rotationVector fail"); return nullptr; } sptr asyncCallbackInfo = @@ -915,7 +915,7 @@ static napi_value GetRotationMatrix(const napi_env &env, napi_value args[], size SensorAlgorithm sensorAlgorithm; int32_t ret = sensorAlgorithm.CreateRotationMatrix(rotationVector, rotationMatrix); if (ret != OHOS::ERR_OK) { - ThrowErr(env, ret, "Create rotation matrix fail"); + ThrowErr(env, ret, "Parameter verification failed", "Must be positive"); return nullptr; } else { asyncCallbackInfo->data.reserveData.length = THREE_DIMENSIONAL_MATRIX_LENGTH; @@ -937,11 +937,11 @@ static napi_value CreateRotationMatrix(napi_env env, napi_callback_info info) napi_value thisVar = nullptr; napi_status status = napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr); if (status != napi_ok || argc < 1) { - ThrowErr(env, PARAMETER_ERROR, "napi_get_cb_info fail or number of parameter invalid"); + ThrowErr(env, PARAMETER_ERROR, "napi_get_cb_info fail or number of parameter invalid", "Must be greater than 1"); return nullptr; } if (!IsMatchArrayType(env, args[0])) { - ThrowErr(env, PARAMETER_ERROR, "Wrong argument type, should be array"); + ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Wrong argument type"); return nullptr; } if (argc >= 2 && IsMatchArrayType(env, args[1])) { @@ -959,7 +959,7 @@ static napi_value GetSensorList(napi_env env, napi_callback_info info) napi_value thisVar = nullptr; napi_status status = napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr); if (status != napi_ok) { - ThrowErr(env, PARAMETER_ERROR, "napi_get_cb_info fail"); + ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "napi_get_cb_info fail"); return nullptr; } sptr asyncCallbackInfo = @@ -997,14 +997,14 @@ static napi_value GetSensorListSync(napi_env env, napi_callback_info info) napi_value thisVar = nullptr; napi_status status = napi_get_cb_info(env, info, &argc, nullptr, &thisVar, nullptr); if (status != napi_ok) { - ThrowErr(env, PARAMETER_ERROR, "napi_get_cb_info fail"); + ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "napi_get_cb_info fail"); return result; } SensorInfo *sensorInfos = nullptr; int32_t count = 0; int32_t ret = GetAllSensors(&sensorInfos, &count); if (ret != OHOS::ERR_OK) { - ThrowErr(env, ret, "Get sensor list fail"); + ThrowErr(env, ret, "Parameter verification failed", "Must be positive"); return result; } vector sensorInfoVec; @@ -1017,17 +1017,17 @@ static napi_value GetSensorListSync(napi_env env, napi_callback_info info) sensorInfoVec.push_back(*(sensorInfos + i)); } if (napi_create_array(env, &result) != napi_ok) { - ThrowErr(env, PARAMETER_ERROR, "napi_create_array fail"); + ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "napi_create_array fail"); return result; } for (uint32_t i = 0; i < sensorInfoVec.size(); ++i) { napi_value value = nullptr; if (!ConvertToSensorInfo(env, sensorInfoVec[i], value)) { - ThrowErr(env, PARAMETER_ERROR, "Convert sensor info fail"); + ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Convert sensor info fail"); return result; } if (napi_set_element(env, result, i, value) != napi_ok) { - ThrowErr(env, PARAMETER_ERROR, "napi_set_element fail"); + ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "napi_set_element fail"); } } return result; @@ -1041,12 +1041,12 @@ static napi_value GetSingleSensor(napi_env env, napi_callback_info info) napi_value thisVar = nullptr; napi_status status = napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr); if (status != napi_ok || argc < 1) { - ThrowErr(env, PARAMETER_ERROR, "napi_get_cb_info fail or number of parameter invalid"); + ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Must be greater than 1"); return nullptr; } int32_t sensorTypeId = INVALID_SENSOR_ID; if (!GetNativeInt32(env, args[0], sensorTypeId)) { - ThrowErr(env, PARAMETER_ERROR, "Wrong argument type, get number fail"); + ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "args[0] is sensorTypeId"); return nullptr; } sptr asyncCallbackInfo = @@ -1072,7 +1072,7 @@ static napi_value GetSingleSensor(napi_env env, napi_callback_info info) } } if (asyncCallbackInfo->sensorInfos.empty()) { - ThrowErr(env, SENSOR_NO_SUPPORT, "The sensor is not supported by the device"); + ThrowErr(env, SENSOR_NO_SUPPORT, "Parameter verification failed", "The sensor is not supported by the device"); return nullptr; } } @@ -1092,19 +1092,19 @@ static napi_value GetSingleSensorSync(napi_env env, napi_callback_info info) napi_value thisVar = nullptr; napi_status status = napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr); if (status != napi_ok || argc == 0) { - ThrowErr(env, PARAMETER_ERROR, "napi_get_cb_info fail or number of parameter invalid"); + ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Must be greater than 1"); return result; } int32_t sensorTypeId = INVALID_SENSOR_ID; if (!GetNativeInt32(env, args[0], sensorTypeId)) { - ThrowErr(env, PARAMETER_ERROR, "Wrong argument type, get number fail"); + ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Wrong argument type, get number fail"); return result; } SensorInfo *sensorInfos = nullptr; int32_t count = 0; int32_t ret = GetAllSensors(&sensorInfos, &count); if (ret != OHOS::ERR_OK) { - ThrowErr(env, ret, "Get sensor list fail"); + ThrowErr(env, ret, "Parameter verification failed", "Must be positive"); return result; } vector sensorInfoVec; @@ -1120,11 +1120,11 @@ static napi_value GetSingleSensorSync(napi_env env, napi_callback_info info) } } if (sensorInfoVec.empty()) { - ThrowErr(env, SENSOR_NO_SUPPORT, "The sensor is not supported by the device"); + ThrowErr(env, SENSOR_NO_SUPPORT, "Parameter verification failed", "The sensor is not supported by the device"); return result; } if (!ConvertToSensorInfo(env, sensorInfoVec[0], result)) { - ThrowErr(env, PARAMETER_ERROR, "Convert sensor info fail"); + ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Convert sensor info fail"); } return result; } @@ -1137,11 +1137,11 @@ napi_value Subscribe(napi_env env, napi_callback_info info, int32_t sensorTypeId napi_value thisVar = nullptr; napi_status status = napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr); if (status != napi_ok || argc < 1) { - ThrowErr(env, PARAMETER_ERROR, "napi_get_cb_info fail or number of parameter invalid"); + ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Must be greater than 1"); return nullptr; } if (!IsMatchType(env, args[0], napi_object)) { - ThrowErr(env, PARAMETER_ERROR, "Wrong argument type, should be object"); + ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "args[0] is napi_object"); return nullptr; } string interval = "normal"; @@ -1209,7 +1209,7 @@ napi_value Unsubscribe(napi_env env, napi_callback_info info, int32_t sensorType napi_value thisVar = nullptr; napi_status status = napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr); if (status != napi_ok) { - ThrowErr(env, PARAMETER_ERROR, "napi_get_cb_info fail"); + ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "napi_get_cb_info fail"); return nullptr; } if (!RemoveSubscribeCallback(env, sensorTypeId) || CheckSubscribe(sensorTypeId)) { @@ -1231,11 +1231,11 @@ napi_value GetBodyState(napi_env env, napi_callback_info info) napi_value thisVar = nullptr; napi_status status = napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr); if (status != napi_ok || argc < 1) { - ThrowErr(env, PARAMETER_ERROR, "napi_get_cb_info fail or number of parameter invalid"); + ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Must be greater than 1"); return nullptr; } if (!IsMatchType(env, args[0], napi_object)) { - ThrowErr(env, PARAMETER_ERROR, "Wrong argument type, should be object"); + ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "args[0] is napi_object"); return nullptr; } sptr asyncCallbackInfo = new (std::nothrow) AsyncCallbackInfo(env, GET_BODY_STATE); diff --git a/frameworks/js/napi/src/sensor_napi_error.cpp b/frameworks/js/napi/src/sensor_napi_error.cpp index fa3c948e..89fa03a5 100644 --- a/frameworks/js/napi/src/sensor_napi_error.cpp +++ b/frameworks/js/napi/src/sensor_napi_error.cpp @@ -17,6 +17,7 @@ #include +#include "securec.h" #undef LOG_TAG #define LOG_TAG "SensorJsAPI" @@ -34,7 +35,7 @@ napi_value CreateBusinessError(const napi_env &env, const int32_t errCode, const return businessError; } -std::optional GetNapiError(int32_t errorCode) +std::optional GetNapiError(int32_t errorCode, const std::string &codeMsg) { auto iter = ERROR_MESSAGES.find(errorCode); if (iter != ERROR_MESSAGES.end()) { @@ -43,12 +44,13 @@ std::optional GetNapiError(int32_t errorCode) return std::nullopt; } -void ThrowErr(const napi_env &env, const int32_t errCode, const std::string &printMsg) +void ThrowErr(const napi_env &env, const int32_t errCode, const std::string &printMsg, const std::string &correctMsg) { - SEN_HILOGE("Message:%{public}s, code:%{public}d", printMsg.c_str(), errCode); - auto msg = GetNapiError(errCode); + SEN_HILOGE("printMsg:%{public}s, correctMsg:%{public}s, code:%{public}d", printMsg.c_str(), correctMsg.c_str(), errCode); + std::string codeMsg; + auto msg = GetNapiError(errCode, codeMsg); if (!msg) { - SEN_HILOGE("ErrCode:%{public}d is invalid", errCode); + SEN_HILOGE("ErrCode:%{public}d is invalid, codeMsg:%{public}s", errCode, codeMsg.c_str()); return; } napi_handle_scope scope = nullptr; diff --git a/frameworks/js/napi/src/sensor_napi_utils.cpp b/frameworks/js/napi/src/sensor_napi_utils.cpp index dbc0cc98..3e33ceb2 100644 --- a/frameworks/js/napi/src/sensor_napi_utils.cpp +++ b/frameworks/js/napi/src/sensor_napi_utils.cpp @@ -303,7 +303,8 @@ bool ConvertToFailData(const napi_env &env, sptr asyncCallbac CALL_LOG_ENTER; CHKPF(asyncCallbackInfo); int32_t code = asyncCallbackInfo->error.code; - auto msg = GetNapiError(code); + std::string codeMsg = asyncCallbackInfo->error.message; + auto msg = GetNapiError(code, codeMsg); if (!msg) { SEN_HILOGE("ErrCode:%{public}d is invalid", code); return false; -- Gitee From a191a48422f40dd024261edd5e2b179bd7333593 Mon Sep 17 00:00:00 2001 From: cff-gite Date: Tue, 28 May 2024 15:41:53 +0800 Subject: [PATCH 2/8] sensor error code rectification Signed-off-by: cff-gite Change-Id: I9406b7f90102c875daad2a8e5daa678705e040fd --- .../js/napi/include/sensor_napi_error.h | 8 +- frameworks/js/napi/src/sensor_js.cpp | 166 +++++++++--------- frameworks/js/napi/src/sensor_napi_error.cpp | 27 +-- frameworks/js/napi/src/sensor_napi_utils.cpp | 2 +- 4 files changed, 102 insertions(+), 101 deletions(-) mode change 100755 => 100644 frameworks/js/napi/include/sensor_napi_error.h diff --git a/frameworks/js/napi/include/sensor_napi_error.h b/frameworks/js/napi/include/sensor_napi_error.h old mode 100755 new mode 100644 index 7a8bbb26..a7e1aa70 --- a/frameworks/js/napi/include/sensor_napi_error.h +++ b/frameworks/js/napi/include/sensor_napi_error.h @@ -26,15 +26,15 @@ namespace OHOS { namespace Sensors { const std::map ERROR_MESSAGES = { {SERVICE_EXCEPTION, "Service exception."}, - {PERMISSION_DENIED, "Permission denied."}, - {PARAMETER_ERROR, "The parameter invalid."}, + {PERMISSION_DENIED, "Permission denied. An attempt was made to %s forbidden by permission:%s."}, + {PARAMETER_ERROR, "Parameter error. The type of %s must be %s."}, {SENSOR_NO_SUPPORT, "The sensor is not supported by the device."}, {NON_SYSTEM_API, "Non-system api."}, }; napi_value CreateBusinessError(const napi_env &env, const int32_t errCode, const std::string &errMessage); -void ThrowErr(const napi_env &env, const int32_t errCode, const std::string &printMsg); -std::optional GetNapiError(int32_t errorCode); +void ThrowErr(const napi_env &env, const int32_t errCode, const std::string &printMsg, const std::string &correctMsg); +bool GetNapiError(int32_t errorCode, std::string &codeMsg); } // namespace Sensors } // namespace OHOS #endif // SENSOR_NAPI_ERROR_H \ No newline at end of file diff --git a/frameworks/js/napi/src/sensor_js.cpp b/frameworks/js/napi/src/sensor_js.cpp index ac5bc5c6..b829c887 100644 --- a/frameworks/js/napi/src/sensor_js.cpp +++ b/frameworks/js/napi/src/sensor_js.cpp @@ -237,7 +237,7 @@ static void UpdateOnceCallback(napi_env env, int32_t sensorTypeId, napi_value ca CHKPV(asyncCallbackInfo); napi_status status = napi_create_reference(env, callback, 1, &asyncCallbackInfo->callback[0]); if (status != napi_ok) { - ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "napi_create_reference fail"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", "create reference"); return; } std::vector> callbackInfos = g_onceCallbackInfos[sensorTypeId]; @@ -253,23 +253,23 @@ static napi_value Once(napi_env env, napi_callback_info info) napi_value thisVar = nullptr; napi_status status = napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr); if (status != napi_ok || argc < 2) { - ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Must be greater than 2"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", "greater than 2"); return nullptr; } if ((!IsMatchType(env, args[0], napi_number)) || (!IsMatchType(env, args[1], napi_function))) { - ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "args[0] is napi_number and args[1] is napi_function"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", "napi_function"); return nullptr; } int32_t sensorTypeId = INVALID_SENSOR_ID; if (!GetNativeInt32(env, args[0], sensorTypeId)) { - ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "args[0] should be sensorTypeId"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", "sensorTypeId"); return nullptr; } if (!CheckSubscribe(sensorTypeId)) { SEN_HILOGD("No subscription to change sensor data, registration is required"); int32_t ret = SubscribeSensor(sensorTypeId, REPORTING_INTERVAL, DataCallbackImpl); if (ret != ERR_OK) { - ThrowErr(env, ret, "Parameter verification failed", "ret must be positive"); + ThrowErr(env, ret, "parameter verification failed", "must be positive"); return nullptr; } } @@ -309,7 +309,7 @@ static void UpdateCallbackInfos(napi_env env, int32_t sensorTypeId, napi_value c CHKPV(asyncCallbackInfo); napi_status status = napi_create_reference(env, callback, 1, &asyncCallbackInfo->callback[0]); if (status != napi_ok) { - ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "napi_create_reference fail"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", "create reference"); return; } std::vector> callbackInfos = g_onCallbackInfos[sensorTypeId]; @@ -353,16 +353,16 @@ static napi_value On(napi_env env, napi_callback_info info) napi_value thisVar = nullptr; napi_status status = napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr); if (status != napi_ok || argc < 2) { - ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "must be greater than 2"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", "greater than 2"); return nullptr; } if ((!IsMatchType(env, args[0], napi_number)) || (!IsMatchType(env, args[1], napi_function))) { - ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "args[0] is napi_number and args[1] is napi_function"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", "args[0] is napi_number and args[1] is napi_function"); return nullptr; } int32_t sensorTypeId = INVALID_SENSOR_ID; if (!GetNativeInt32(env, args[0], sensorTypeId)) { - ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "args[0] should is sensorTypeId"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", "sensorTypeId"); return nullptr; } int64_t interval = REPORTING_INTERVAL; @@ -374,7 +374,7 @@ static napi_value On(napi_env env, napi_callback_info info) SEN_HILOGD("Interval is %{public}" PRId64, interval); int32_t ret = SubscribeSensor(sensorTypeId, interval, DataCallbackImpl); if (ret != ERR_OK) { - ThrowErr(env, ret, "Parameter verification failed", "Must be positive"); + ThrowErr(env, ret, "parameter verification failed", "positive"); return nullptr; } UpdateCallbackInfos(env, sensorTypeId, args[1]); @@ -443,12 +443,12 @@ static napi_value Off(napi_env env, napi_callback_info info) napi_value thisVar = nullptr; napi_status status = napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr); if (status != napi_ok || argc < 1) { - ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Must be greater than 1"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", "greater than 1"); return nullptr; } int32_t sensorTypeId = INVALID_SENSOR_ID; if ((!IsMatchType(env, args[0], napi_number)) || (!GetNativeInt32(env, args[0], sensorTypeId))) { - ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "args[0] should be sensorTypeId"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", "napi_number or sensorTypeId"); return nullptr; } int32_t subscribeSize = -1; @@ -459,7 +459,7 @@ static napi_value Off(napi_env env, napi_callback_info info) } else if (IsMatchType(env, args[1], napi_function)) { subscribeSize = RemoveCallback(env, sensorTypeId, args[1]); } else { - ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "args[1] should be napi_function"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", "napi_function"); return nullptr; } if (CheckSystemSubscribe(sensorTypeId) || (subscribeSize > 0)) { @@ -468,7 +468,7 @@ static napi_value Off(napi_env env, napi_callback_info info) } int32_t ret = UnsubscribeSensor(sensorTypeId); if (ret == PARAMETER_ERROR || ret == PERMISSION_DENIED) { - ThrowErr(env, ret, "Parameter verification failed", "UnsubscribeSensor fail"); + ThrowErr(env, ret, "parameter verification failed", "UnsubscribeSensor success"); } return nullptr; } @@ -504,46 +504,46 @@ static napi_value GetGeomagneticField(napi_env env, napi_callback_info info) napi_value thisVar = nullptr; napi_status status = napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr); if (status != napi_ok || argc < 2) { - ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Must be greater than 2"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", "greater than 2"); return nullptr; } if ((!IsMatchType(env, args[0], napi_object)) || (!IsMatchType(env, args[1], napi_number))) { - ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "args[0] is args[0] and args[1] is napi_number"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", "napi_object or napi_number"); return nullptr; } napi_value napiLatitude = GetNamedProperty(env, args[0], "latitude"); if (napiLatitude == nullptr) { - ThrowErr(env, PARAMETER_ERROR, "napiLatitude is null", "Must be positive"); + ThrowErr(env, PARAMETER_ERROR, "napiLatitude is null", "positive"); return nullptr; } double latitude = 0; if (!GetNativeDouble(env, napiLatitude, latitude)) { - ThrowErr(env, PARAMETER_ERROR, "Get latitude fail", "The positive value of type double"); + ThrowErr(env, PARAMETER_ERROR, "get latitude fail", "double"); return nullptr; } napi_value napiLongitude = GetNamedProperty(env, args[0], "longitude"); if (napiLongitude == nullptr) { - ThrowErr(env, PARAMETER_ERROR, "napiLongitude is null", "Must be positive"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", "napiLongitude is positive"); return nullptr; } double longitude = 0; if (!GetNativeDouble(env, napiLongitude, longitude)) { - ThrowErr(env, PARAMETER_ERROR, "Get longitude fail", "The positive value of type double"); + ThrowErr(env, PARAMETER_ERROR, "get longitude fail", "double"); return nullptr; } napi_value napiAltitude = GetNamedProperty(env, args[0], "altitude"); if (napiAltitude == nullptr) { - ThrowErr(env, PARAMETER_ERROR, "napiAltitude is null", "Must be positive"); + ThrowErr(env, PARAMETER_ERROR, "napiAltitude is null", "napiAltitude is positive"); return nullptr; } double altitude = 0; if (!GetNativeDouble(env, napiAltitude, altitude)) { - ThrowErr(env, PARAMETER_ERROR, "Get altitude fail", "The positive value of type double"); + ThrowErr(env, PARAMETER_ERROR, "get altitude fail", "double"); return nullptr; } int64_t timeMillis = 0; if (!GetNativeInt64(env, args[1], timeMillis)) { - ThrowErr(env, PARAMETER_ERROR, "Get timeMillis fail", "The positive value of type int64_t"); + ThrowErr(env, PARAMETER_ERROR, "get timeMillis fail", "int64_t"); return nullptr; } GeomagneticField geomagneticField(latitude, longitude, altitude, timeMillis); @@ -583,41 +583,41 @@ static napi_value TransformCoordinateSystem(napi_env env, napi_callback_info inf napi_value thisVar = nullptr; napi_status status = napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr); if (status != napi_ok || argc < 2) { - ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Must be greater than 2"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", "greater than 2"); return nullptr; } if ((!IsMatchArrayType(env, args[0])) || (!IsMatchType(env, args[1], napi_object))) { - ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "args[0] is array and args[1] is napi_object"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", "array or napi_object"); return nullptr; } std::vector inRotationVector; if (!GetFloatArray(env, args[0], inRotationVector)) { - ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Get inRotationVector fail"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", "get inRotationVector success"); return nullptr; } size_t length = inRotationVector.size(); if ((length != DATA_LENGTH) && (length != THREE_DIMENSIONAL_MATRIX_LENGTH)) { - ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Wrong inRotationVector length"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", "correct inRotationVector length"); return nullptr; } napi_value napiAxisX = GetAxisX(env, args[1]); if (napiAxisX == nullptr) { - ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "napiAxisX must be positive"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", "napi_value"); return nullptr; } int32_t axisX = 0; if (!GetNativeInt32(env, napiAxisX, axisX)) { - ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Parameter verification failed"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", "int32_t"); return nullptr; } napi_value napiAxisY = GetAxisY(env, args[1]); if (napiAxisY == nullptr) { - ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "napiAxisY must be positive"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", "napi_value"); return nullptr; } int32_t axisY = 0; if (!GetNativeInt32(env, napiAxisY, axisY)) { - ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Parameter verification failed"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", "int32_t"); return nullptr; } sptr asyncCallbackInfo = @@ -627,7 +627,7 @@ static napi_value TransformCoordinateSystem(napi_env env, napi_callback_info inf SensorAlgorithm sensorAlgorithm; int32_t ret = sensorAlgorithm.TransformCoordinateSystem(inRotationVector, axisX, axisY, outRotationVector); if (ret != OHOS::ERR_OK) { - ThrowErr(env, ret, "Parameter verification failed", "Must be positive"); + ThrowErr(env, ret, "parameter verification failed", "positive"); return nullptr; } else { for (size_t i = 0; i < length; ++i) { @@ -649,15 +649,15 @@ static napi_value GetAngleModify(napi_env env, napi_callback_info info) napi_value thisVar = nullptr; napi_status status = napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr); if (status != napi_ok || argc < 2) { - ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Must be greater than 2"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", "greater than 2"); return nullptr; } if (!IsMatchArrayType(env, args[0])) { - ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Wrong argument type"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", "correct argument type"); return nullptr; } if (!IsMatchArrayType(env, args[1])) { - ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Wrong argument type"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", "correct argument type"); return nullptr; } sptr asyncCallbackInfo = @@ -665,19 +665,19 @@ static napi_value GetAngleModify(napi_env env, napi_callback_info info) CHKPP(asyncCallbackInfo); std::vector curRotationVector; if (!GetFloatArray(env, args[0], curRotationVector)) { - ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Get curRotationVector fail"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", "float array"); return nullptr; } std::vector preRotationVector; if (!GetFloatArray(env, args[1], preRotationVector)) { - ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Get preRotationVector fail"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", "float array"); return nullptr; } std::vector angleChange(ROTATION_VECTOR_LENGTH); SensorAlgorithm sensorAlgorithm; int32_t ret = sensorAlgorithm.GetAngleModify(curRotationVector, preRotationVector, angleChange); if (ret != OHOS::ERR_OK) { - ThrowErr(env, ret, "Parameter verification failed", "Must be positive"); + ThrowErr(env, ret, "parameter verification failed", "positive"); return nullptr; } else { asyncCallbackInfo->data.reserveData.length = ROTATION_VECTOR_LENGTH; @@ -699,11 +699,11 @@ static napi_value GetDirection(napi_env env, napi_callback_info info) napi_value thisVar = nullptr; napi_status status = napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr); if (status != napi_ok || argc < 1) { - ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Must be greater than 1"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", "greater than 1"); return nullptr; } if (!IsMatchArrayType(env, args[0])) { - ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Wrong argument type"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", "correct argument type"); return nullptr; } sptr asyncCallbackInfo = @@ -711,14 +711,14 @@ static napi_value GetDirection(napi_env env, napi_callback_info info) CHKPP(asyncCallbackInfo); std::vector rotationMatrix; if (!GetFloatArray(env, args[0], rotationMatrix)) { - ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Get rotationMatrix fail"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", "float array"); return nullptr; } std::vector rotationAngle(ROTATION_VECTOR_LENGTH); SensorAlgorithm sensorAlgorithm; int32_t ret = sensorAlgorithm.GetDirection(rotationMatrix, rotationAngle); if (ret != OHOS::ERR_OK) { - ThrowErr(env, ret, "Parameter verification failed", "Must be positive"); + ThrowErr(env, ret, "parameter verification failed", "positive"); return nullptr; } else { asyncCallbackInfo->data.reserveData.length = ROTATION_VECTOR_LENGTH; @@ -740,11 +740,11 @@ static napi_value CreateQuaternion(napi_env env, napi_callback_info info) napi_value thisVar = nullptr; napi_status status = napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr); if (status != napi_ok || argc < 1) { - ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Must be greater than 1"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", "greater than 1"); return nullptr; } if (!IsMatchArrayType(env, args[0])) { - ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Wrong argument type"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", "correct argument type"); return nullptr; } sptr asyncCallbackInfo = @@ -752,14 +752,14 @@ static napi_value CreateQuaternion(napi_env env, napi_callback_info info) CHKPP(asyncCallbackInfo); std::vector rotationVector; if (!GetFloatArray(env, args[0], rotationVector)) { - ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Get rotationVector failed"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", "float array"); return nullptr; } std::vector quaternion(QUATERNION_LENGTH); SensorAlgorithm sensorAlgorithm; int32_t ret = sensorAlgorithm.CreateQuaternion(rotationVector, quaternion); if (ret != OHOS::ERR_OK) { - ThrowErr(env, ret, "Parameter verification failed", "Must be positive"); + ThrowErr(env, ret, "parameter verification failed", "positive"); return nullptr; } else { asyncCallbackInfo->data.reserveData.length = QUATERNION_LENGTH; @@ -781,11 +781,11 @@ static napi_value GetAltitude(napi_env env, napi_callback_info info) napi_value thisVar = nullptr; napi_status status = napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr); if (status != napi_ok || argc < 2) { - ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Must be greater than 3"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", "greater than 3"); return nullptr; } if ((!IsMatchType(env, args[0], napi_number)) || (!IsMatchType(env, args[1], napi_number))) { - ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "args[0] and args[1] is napi_number"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", "napi_number"); return nullptr; } sptr asyncCallbackInfo = @@ -793,19 +793,19 @@ static napi_value GetAltitude(napi_env env, napi_callback_info info) CHKPP(asyncCallbackInfo); float seaPressure = 0; if (!GetNativeFloat(env, args[0], seaPressure)) { - ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Should be of type float"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", "float"); return nullptr; } float currentPressure = 0; if (!GetNativeFloat(env, args[1], currentPressure)) { - ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Should be of type float"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", "float"); return nullptr; } float altitude = 0; SensorAlgorithm sensorAlgorithm; int32_t ret = sensorAlgorithm.GetAltitude(seaPressure, currentPressure, &altitude); if (ret != OHOS::ERR_OK) { - ThrowErr(env, ret, "Parameter verification failed", "Must be positive"); + ThrowErr(env, ret, "parameter verification failed", "positive"); return nullptr; } else { asyncCallbackInfo->data.reserveData.reserve[0] = altitude; @@ -824,11 +824,11 @@ static napi_value GetGeomagneticDip(napi_env env, napi_callback_info info) napi_value thisVar = nullptr; napi_status status = napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr); if (status != napi_ok || argc < 1) { - ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Must be greater than 2"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", "greater than 2"); return nullptr; } if (!IsMatchArrayType(env, args[0])) { - ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Wrong argument type"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", "correct argument type"); return nullptr; } sptr asyncCallbackInfo = @@ -836,14 +836,14 @@ static napi_value GetGeomagneticDip(napi_env env, napi_callback_info info) CHKPP(asyncCallbackInfo); std::vector inclinationMatrix; if (!GetFloatArray(env, args[0], inclinationMatrix)) { - ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Get inclinationMatrix fail"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", "float array"); return nullptr; } float geomagneticDip = 0; SensorAlgorithm sensorAlgorithm; int32_t ret = sensorAlgorithm.GetGeomagneticDip(inclinationMatrix, &geomagneticDip); if (ret != OHOS::ERR_OK) { - ThrowErr(env, ret, "Parameter verification failed", "Must be positive"); + ThrowErr(env, ret, "parameter verification failed", "positive"); return nullptr; } else { asyncCallbackInfo->data.reserveData.reserve[0] = geomagneticDip; @@ -858,17 +858,17 @@ static napi_value CreateRotationAndInclination(const napi_env &env, napi_value a { CALL_LOG_ENTER; if (argc < 2) { - ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Must be greater than 2"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", "greater than 2"); return nullptr; } std::vector gravity; if (!GetFloatArray(env, args[0], gravity)) { - ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Get gravity fail"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", "float array"); return nullptr; } std::vector geomagnetic; if (!GetFloatArray(env, args[1], geomagnetic)) { - ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Get geomagnetic fail"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", "float array"); return nullptr; } std::vector rotation(THREE_DIMENSIONAL_MATRIX_LENGTH); @@ -879,7 +879,7 @@ static napi_value CreateRotationAndInclination(const napi_env &env, napi_value a SensorAlgorithm sensorAlgorithm; int32_t ret = sensorAlgorithm.CreateRotationAndInclination(gravity, geomagnetic, rotation, inclination); if (ret != OHOS::ERR_OK) { - ThrowErr(env, ret, "Create rotation and inclination matrix fail", "Must be positive"); + ThrowErr(env, ret, "Create rotation and inclination matrix fail", "positive"); return nullptr; } else { asyncCallbackInfo->data.reserveData.length = THREE_DIMENSIONAL_MATRIX_LENGTH; @@ -900,12 +900,12 @@ static napi_value GetRotationMatrix(const napi_env &env, napi_value args[], size { CALL_LOG_ENTER; if (argc < 1) { - ThrowErr(env, PARAMETER_ERROR, "The number of parameters is not valid", "Must be greater than 1"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", "greater than 1"); return nullptr; } std::vector rotationVector; if (!GetFloatArray(env, args[0], rotationVector)) { - ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Get rotationVector fail"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", "float array"); return nullptr; } sptr asyncCallbackInfo = @@ -915,7 +915,7 @@ static napi_value GetRotationMatrix(const napi_env &env, napi_value args[], size SensorAlgorithm sensorAlgorithm; int32_t ret = sensorAlgorithm.CreateRotationMatrix(rotationVector, rotationMatrix); if (ret != OHOS::ERR_OK) { - ThrowErr(env, ret, "Parameter verification failed", "Must be positive"); + ThrowErr(env, ret, "parameter verification failed", "positive"); return nullptr; } else { asyncCallbackInfo->data.reserveData.length = THREE_DIMENSIONAL_MATRIX_LENGTH; @@ -937,11 +937,11 @@ static napi_value CreateRotationMatrix(napi_env env, napi_callback_info info) napi_value thisVar = nullptr; napi_status status = napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr); if (status != napi_ok || argc < 1) { - ThrowErr(env, PARAMETER_ERROR, "napi_get_cb_info fail or number of parameter invalid", "Must be greater than 1"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", "greater than 1"); return nullptr; } if (!IsMatchArrayType(env, args[0])) { - ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Wrong argument type"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", "correct argument type"); return nullptr; } if (argc >= 2 && IsMatchArrayType(env, args[1])) { @@ -959,7 +959,7 @@ static napi_value GetSensorList(napi_env env, napi_callback_info info) napi_value thisVar = nullptr; napi_status status = napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr); if (status != napi_ok) { - ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "napi_get_cb_info fail"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", "enter the correct parameters"); return nullptr; } sptr asyncCallbackInfo = @@ -997,14 +997,14 @@ static napi_value GetSensorListSync(napi_env env, napi_callback_info info) napi_value thisVar = nullptr; napi_status status = napi_get_cb_info(env, info, &argc, nullptr, &thisVar, nullptr); if (status != napi_ok) { - ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "napi_get_cb_info fail"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", "enter the correct parameters"); return result; } SensorInfo *sensorInfos = nullptr; int32_t count = 0; int32_t ret = GetAllSensors(&sensorInfos, &count); if (ret != OHOS::ERR_OK) { - ThrowErr(env, ret, "Parameter verification failed", "Must be positive"); + ThrowErr(env, ret, "parameter verification failed", "positive"); return result; } vector sensorInfoVec; @@ -1017,17 +1017,17 @@ static napi_value GetSensorListSync(napi_env env, napi_callback_info info) sensorInfoVec.push_back(*(sensorInfos + i)); } if (napi_create_array(env, &result) != napi_ok) { - ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "napi_create_array fail"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", "napi_create_array success"); return result; } for (uint32_t i = 0; i < sensorInfoVec.size(); ++i) { napi_value value = nullptr; if (!ConvertToSensorInfo(env, sensorInfoVec[i], value)) { - ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Convert sensor info fail"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", "Convert sensor info success"); return result; } if (napi_set_element(env, result, i, value) != napi_ok) { - ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "napi_set_element fail"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", "napi_set_element success"); } } return result; @@ -1041,12 +1041,12 @@ static napi_value GetSingleSensor(napi_env env, napi_callback_info info) napi_value thisVar = nullptr; napi_status status = napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr); if (status != napi_ok || argc < 1) { - ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Must be greater than 1"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", "greater than 1"); return nullptr; } int32_t sensorTypeId = INVALID_SENSOR_ID; if (!GetNativeInt32(env, args[0], sensorTypeId)) { - ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "args[0] is sensorTypeId"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", "sensorTypeId"); return nullptr; } sptr asyncCallbackInfo = @@ -1072,7 +1072,7 @@ static napi_value GetSingleSensor(napi_env env, napi_callback_info info) } } if (asyncCallbackInfo->sensorInfos.empty()) { - ThrowErr(env, SENSOR_NO_SUPPORT, "Parameter verification failed", "The sensor is not supported by the device"); + ThrowErr(env, SENSOR_NO_SUPPORT, "parameter verification failed", "The sensor is supported by the device"); return nullptr; } } @@ -1092,19 +1092,19 @@ static napi_value GetSingleSensorSync(napi_env env, napi_callback_info info) napi_value thisVar = nullptr; napi_status status = napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr); if (status != napi_ok || argc == 0) { - ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Must be greater than 1"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", "greater than 1"); return result; } int32_t sensorTypeId = INVALID_SENSOR_ID; if (!GetNativeInt32(env, args[0], sensorTypeId)) { - ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Wrong argument type, get number fail"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", "correct argument type"); return result; } SensorInfo *sensorInfos = nullptr; int32_t count = 0; int32_t ret = GetAllSensors(&sensorInfos, &count); if (ret != OHOS::ERR_OK) { - ThrowErr(env, ret, "Parameter verification failed", "Must be positive"); + ThrowErr(env, ret, "parameter verification failed", "positive"); return result; } vector sensorInfoVec; @@ -1120,11 +1120,11 @@ static napi_value GetSingleSensorSync(napi_env env, napi_callback_info info) } } if (sensorInfoVec.empty()) { - ThrowErr(env, SENSOR_NO_SUPPORT, "Parameter verification failed", "The sensor is not supported by the device"); + ThrowErr(env, SENSOR_NO_SUPPORT, "parameter verification failed", "The sensor is supported by the device"); return result; } if (!ConvertToSensorInfo(env, sensorInfoVec[0], result)) { - ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Convert sensor info fail"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", "Convert sensor info success"); } return result; } @@ -1137,11 +1137,11 @@ napi_value Subscribe(napi_env env, napi_callback_info info, int32_t sensorTypeId napi_value thisVar = nullptr; napi_status status = napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr); if (status != napi_ok || argc < 1) { - ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Must be greater than 1"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", "greater than 1"); return nullptr; } if (!IsMatchType(env, args[0], napi_object)) { - ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "args[0] is napi_object"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", "napi_object"); return nullptr; } string interval = "normal"; @@ -1209,7 +1209,7 @@ napi_value Unsubscribe(napi_env env, napi_callback_info info, int32_t sensorType napi_value thisVar = nullptr; napi_status status = napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr); if (status != napi_ok) { - ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "napi_get_cb_info fail"); + ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "napi_get_cb_info success"); return nullptr; } if (!RemoveSubscribeCallback(env, sensorTypeId) || CheckSubscribe(sensorTypeId)) { @@ -1231,11 +1231,11 @@ napi_value GetBodyState(napi_env env, napi_callback_info info) napi_value thisVar = nullptr; napi_status status = napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr); if (status != napi_ok || argc < 1) { - ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Must be greater than 1"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", "greater than 1"); return nullptr; } if (!IsMatchType(env, args[0], napi_object)) { - ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "args[0] is napi_object"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", "napi_object"); return nullptr; } sptr asyncCallbackInfo = new (std::nothrow) AsyncCallbackInfo(env, GET_BODY_STATE); diff --git a/frameworks/js/napi/src/sensor_napi_error.cpp b/frameworks/js/napi/src/sensor_napi_error.cpp index 89fa03a5..bd426ea0 100644 --- a/frameworks/js/napi/src/sensor_napi_error.cpp +++ b/frameworks/js/napi/src/sensor_napi_error.cpp @@ -35,29 +35,30 @@ napi_value CreateBusinessError(const napi_env &env, const int32_t errCode, const return businessError; } -std::optional GetNapiError(int32_t errorCode, const std::string &codeMsg) +bool GetNapiError(int32_t errorCode, std::string &codeMsg) { auto iter = ERROR_MESSAGES.find(errorCode); - if (iter != ERROR_MESSAGES.end()) { - return iter->second; + if (iter == ERROR_MESSAGES.end()) { + SEN_HILOGE("errorCode %{public}d not found", errorCode); + return false; } - return std::nullopt; + codeMsg = iter->second; + return true; } void ThrowErr(const napi_env &env, const int32_t errCode, const std::string &printMsg, const std::string &correctMsg) { SEN_HILOGE("printMsg:%{public}s, correctMsg:%{public}s, code:%{public}d", printMsg.c_str(), correctMsg.c_str(), errCode); std::string codeMsg; - auto msg = GetNapiError(errCode, codeMsg); - if (!msg) { - SEN_HILOGE("ErrCode:%{public}d is invalid, codeMsg:%{public}s", errCode, codeMsg.c_str()); - return; + if (GetNapiError(errCode, codeMsg)) { + char buf[300]; + if (sprintf_s(buf, sizeof(buf), codeMsg.c_str(), printMsg.c_str(), correctMsg.c_str()) > 0 ) { + CreateBusinessError(env, errCode, buf); + SEN_HILOGE("cff Message:%{public}s", buf); + } else { + SEN_HILOGE("Failed to convert string type to char type"); + } } - napi_handle_scope scope = nullptr; - napi_open_handle_scope(env, &scope); - napi_value error = CreateBusinessError(env, errCode, msg.value()); - napi_throw(env, error); - napi_close_handle_scope(env, scope); } } // namespace Sensors } // namespace OHOS \ No newline at end of file diff --git a/frameworks/js/napi/src/sensor_napi_utils.cpp b/frameworks/js/napi/src/sensor_napi_utils.cpp index 3e33ceb2..2230069f 100644 --- a/frameworks/js/napi/src/sensor_napi_utils.cpp +++ b/frameworks/js/napi/src/sensor_napi_utils.cpp @@ -309,7 +309,7 @@ bool ConvertToFailData(const napi_env &env, sptr asyncCallbac SEN_HILOGE("ErrCode:%{public}d is invalid", code); return false; } - result[0] = CreateBusinessError(env, code, msg.value()); + result[0] = CreateBusinessError(env, code, codeMsg); return (result[0] != nullptr); } -- Gitee From be3a1897bba94c868c775c3e02005c2c27e4c23c Mon Sep 17 00:00:00 2001 From: cff-gite Date: Tue, 28 May 2024 15:50:27 +0800 Subject: [PATCH 3/8] sensor error code rectification Signed-off-by: cff-gite Change-Id: Id26187b6976cc2e052ed1f9fc6ec4bbd5b399ef9 --- frameworks/js/napi/src/sensor_napi_error.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frameworks/js/napi/src/sensor_napi_error.cpp b/frameworks/js/napi/src/sensor_napi_error.cpp index bd426ea0..77806368 100644 --- a/frameworks/js/napi/src/sensor_napi_error.cpp +++ b/frameworks/js/napi/src/sensor_napi_error.cpp @@ -53,8 +53,8 @@ void ThrowErr(const napi_env &env, const int32_t errCode, const std::string &pri if (GetNapiError(errCode, codeMsg)) { char buf[300]; if (sprintf_s(buf, sizeof(buf), codeMsg.c_str(), printMsg.c_str(), correctMsg.c_str()) > 0 ) { + SEN_HILOGE("Message buf:%{public}s", buf); CreateBusinessError(env, errCode, buf); - SEN_HILOGE("cff Message:%{public}s", buf); } else { SEN_HILOGE("Failed to convert string type to char type"); } -- Gitee From edc2325c053ba618e86ab0dad8d772b55f57c2e9 Mon Sep 17 00:00:00 2001 From: cff-gite Date: Thu, 30 May 2024 16:08:05 +0800 Subject: [PATCH 4/8] sensor error code rectification Signed-off-by: cff-gite Change-Id: I0514a2fdeed8bb9984f1a0c5a4387f8b445b9b96 --- frameworks/js/napi/src/sensor_js.cpp | 47 +++++++++++++++++++++------- 1 file changed, 35 insertions(+), 12 deletions(-) diff --git a/frameworks/js/napi/src/sensor_js.cpp b/frameworks/js/napi/src/sensor_js.cpp index b829c887..c61b048c 100644 --- a/frameworks/js/napi/src/sensor_js.cpp +++ b/frameworks/js/napi/src/sensor_js.cpp @@ -268,8 +268,11 @@ static napi_value Once(napi_env env, napi_callback_info info) if (!CheckSubscribe(sensorTypeId)) { SEN_HILOGD("No subscription to change sensor data, registration is required"); int32_t ret = SubscribeSensor(sensorTypeId, REPORTING_INTERVAL, DataCallbackImpl); - if (ret != ERR_OK) { - ThrowErr(env, ret, "parameter verification failed", "must be positive"); + if (ret == PARAMETER_ERROR) { + ThrowErr(env, ret, "parameter verification failed", "enter the correct parameters"); + return nullptr; + } else if (ret != ERR_OK) { + ThrowErr(env, ret, "SubscribeSensor fail", "Must be positive"); return nullptr; } } @@ -373,9 +376,15 @@ static napi_value On(napi_env env, napi_callback_info info) } SEN_HILOGD("Interval is %{public}" PRId64, interval); int32_t ret = SubscribeSensor(sensorTypeId, interval, DataCallbackImpl); - if (ret != ERR_OK) { + if (ret == PARAMETER_ERROR) { + ThrowErr(env, ret, "parameter verification failed", "positive"); + return nullptr; + } else if (ret == SERVICE_EXCEPTION) { ThrowErr(env, ret, "parameter verification failed", "positive"); return nullptr; + } else if (ret != ERR_OK) { + ThrowErr(env, ret, "SubscribeSensor fail", "positive"); + return nullptr; } UpdateCallbackInfos(env, sensorTypeId, args[1]); return nullptr; @@ -467,8 +476,10 @@ static napi_value Off(napi_env env, napi_callback_info info) return nullptr; } int32_t ret = UnsubscribeSensor(sensorTypeId); - if (ret == PARAMETER_ERROR || ret == PERMISSION_DENIED) { + if (ret == PARAMETER_ERROR) { ThrowErr(env, ret, "parameter verification failed", "UnsubscribeSensor success"); + } else if (ret == PERMISSION_DENIED) { + ThrowErr(env, ret, "unsubscribe sensor", "permission"); } return nullptr; } @@ -626,9 +637,12 @@ static napi_value TransformCoordinateSystem(napi_env env, napi_callback_info inf std::vector outRotationVector(length); SensorAlgorithm sensorAlgorithm; int32_t ret = sensorAlgorithm.TransformCoordinateSystem(inRotationVector, axisX, axisY, outRotationVector); - if (ret != OHOS::ERR_OK) { + if (ret == PARAMETER_ERROR) { ThrowErr(env, ret, "parameter verification failed", "positive"); return nullptr; + } else if (ret != OHOS::ERR_OK) { + ThrowErr(env, ret, "Transform coordinate system fail", "Must be positive"); + return nullptr; } else { for (size_t i = 0; i < length; ++i) { asyncCallbackInfo->data.reserveData.reserve[i] = outRotationVector[i]; @@ -677,7 +691,7 @@ static napi_value GetAngleModify(napi_env env, napi_callback_info info) SensorAlgorithm sensorAlgorithm; int32_t ret = sensorAlgorithm.GetAngleModify(curRotationVector, preRotationVector, angleChange); if (ret != OHOS::ERR_OK) { - ThrowErr(env, ret, "parameter verification failed", "positive"); + ThrowErr(env, ret, "Get angle modify fail", "Must be positive"); return nullptr; } else { asyncCallbackInfo->data.reserveData.length = ROTATION_VECTOR_LENGTH; @@ -717,9 +731,12 @@ static napi_value GetDirection(napi_env env, napi_callback_info info) std::vector rotationAngle(ROTATION_VECTOR_LENGTH); SensorAlgorithm sensorAlgorithm; int32_t ret = sensorAlgorithm.GetDirection(rotationMatrix, rotationAngle); - if (ret != OHOS::ERR_OK) { + if (ret == PARAMETER_ERROR) { ThrowErr(env, ret, "parameter verification failed", "positive"); return nullptr; + } else if (ret != OHOS::ERR_OK) { + ThrowErr(env, ret, "Get direction fail", "Must be positive"); + return nullptr; } else { asyncCallbackInfo->data.reserveData.length = ROTATION_VECTOR_LENGTH; for (int32_t i = 0; i < ROTATION_VECTOR_LENGTH; ++i) { @@ -758,9 +775,12 @@ static napi_value CreateQuaternion(napi_env env, napi_callback_info info) std::vector quaternion(QUATERNION_LENGTH); SensorAlgorithm sensorAlgorithm; int32_t ret = sensorAlgorithm.CreateQuaternion(rotationVector, quaternion); - if (ret != OHOS::ERR_OK) { + if (ret == PARAMETER_ERROR) { ThrowErr(env, ret, "parameter verification failed", "positive"); return nullptr; + } else if (ret != OHOS::ERR_OK) { + ThrowErr(env, ret, "Get createQuaternion fail", "Must be positive"); + return nullptr; } else { asyncCallbackInfo->data.reserveData.length = QUATERNION_LENGTH; for (int32_t i = 0; i < QUATERNION_LENGTH; ++i) { @@ -805,7 +825,7 @@ static napi_value GetAltitude(napi_env env, napi_callback_info info) SensorAlgorithm sensorAlgorithm; int32_t ret = sensorAlgorithm.GetAltitude(seaPressure, currentPressure, &altitude); if (ret != OHOS::ERR_OK) { - ThrowErr(env, ret, "parameter verification failed", "positive"); + ThrowErr(env, ret, "Get altitude fail", "Must be positive"); return nullptr; } else { asyncCallbackInfo->data.reserveData.reserve[0] = altitude; @@ -842,9 +862,12 @@ static napi_value GetGeomagneticDip(napi_env env, napi_callback_info info) float geomagneticDip = 0; SensorAlgorithm sensorAlgorithm; int32_t ret = sensorAlgorithm.GetGeomagneticDip(inclinationMatrix, &geomagneticDip); - if (ret != OHOS::ERR_OK) { + if (ret == PARAMETER_ERROR) { ThrowErr(env, ret, "parameter verification failed", "positive"); return nullptr; + } else if (ret != OHOS::ERR_OK) { + ThrowErr(env, ret, "Get geomagnetic dip fail", "Must be positive"); + return nullptr; } else { asyncCallbackInfo->data.reserveData.reserve[0] = geomagneticDip; } @@ -879,7 +902,7 @@ static napi_value CreateRotationAndInclination(const napi_env &env, napi_value a SensorAlgorithm sensorAlgorithm; int32_t ret = sensorAlgorithm.CreateRotationAndInclination(gravity, geomagnetic, rotation, inclination); if (ret != OHOS::ERR_OK) { - ThrowErr(env, ret, "Create rotation and inclination matrix fail", "positive"); + ThrowErr(env, ret, "Create rotation and inclination matrix fail", "Must be positive"); return nullptr; } else { asyncCallbackInfo->data.reserveData.length = THREE_DIMENSIONAL_MATRIX_LENGTH; @@ -1104,7 +1127,7 @@ static napi_value GetSingleSensorSync(napi_env env, napi_callback_info info) int32_t count = 0; int32_t ret = GetAllSensors(&sensorInfos, &count); if (ret != OHOS::ERR_OK) { - ThrowErr(env, ret, "parameter verification failed", "positive"); + ThrowErr(env, ret, "Get sensor list fail", "Must be positive"); return result; } vector sensorInfoVec; -- Gitee From b8aedcdee27e835cda9b3f3ba3d43870722e8842 Mon Sep 17 00:00:00 2001 From: cff-gite Date: Thu, 30 May 2024 16:40:10 +0800 Subject: [PATCH 5/8] sensor error code rectification Signed-off-by: cff-gite Change-Id: I42bc4bccd02710d84ea4c16ee5a1d586facb6e8f --- frameworks/js/napi/src/sensor_napi_error.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/frameworks/js/napi/src/sensor_napi_error.cpp b/frameworks/js/napi/src/sensor_napi_error.cpp index 77806368..be05c915 100644 --- a/frameworks/js/napi/src/sensor_napi_error.cpp +++ b/frameworks/js/napi/src/sensor_napi_error.cpp @@ -18,11 +18,16 @@ #include #include "securec.h" + #undef LOG_TAG #define LOG_TAG "SensorJsAPI" namespace OHOS { namespace Sensors { +namespace { +constexpr int32_t SENSOR_BUFF = 300; +} // namespace + napi_value CreateBusinessError(const napi_env &env, const int32_t errCode, const std::string &errMessage) { napi_value businessError = nullptr; @@ -51,7 +56,7 @@ void ThrowErr(const napi_env &env, const int32_t errCode, const std::string &pri SEN_HILOGE("printMsg:%{public}s, correctMsg:%{public}s, code:%{public}d", printMsg.c_str(), correctMsg.c_str(), errCode); std::string codeMsg; if (GetNapiError(errCode, codeMsg)) { - char buf[300]; + char buf[SENSOR_BUFF]; if (sprintf_s(buf, sizeof(buf), codeMsg.c_str(), printMsg.c_str(), correctMsg.c_str()) > 0 ) { SEN_HILOGE("Message buf:%{public}s", buf); CreateBusinessError(env, errCode, buf); -- Gitee From 6cc2ccc9091e3cf1897355facb586e52f0740d16 Mon Sep 17 00:00:00 2001 From: cff-gite Date: Sat, 1 Jun 2024 09:32:50 +0800 Subject: [PATCH 6/8] sensor error code rectification Signed-off-by: cff-gite Change-Id: I361fcdc7eb8ac23a9c887794a19b6a30e8bb6e30 --- .../js/napi/include/sensor_napi_error.h | 12 +++- frameworks/js/napi/src/sensor_js.cpp | 67 +++++++------------ frameworks/js/napi/src/sensor_napi_error.cpp | 28 +++++++- 3 files changed, 60 insertions(+), 47 deletions(-) diff --git a/frameworks/js/napi/include/sensor_napi_error.h b/frameworks/js/napi/include/sensor_napi_error.h index a7e1aa70..d76f322e 100644 --- a/frameworks/js/napi/include/sensor_napi_error.h +++ b/frameworks/js/napi/include/sensor_napi_error.h @@ -24,7 +24,7 @@ namespace OHOS { namespace Sensors { -const std::map ERROR_MESSAGES = { +const std::map ERROR_CODE_MESSAGES = { {SERVICE_EXCEPTION, "Service exception."}, {PERMISSION_DENIED, "Permission denied. An attempt was made to %s forbidden by permission:%s."}, {PARAMETER_ERROR, "Parameter error. The type of %s must be %s."}, @@ -32,8 +32,18 @@ const std::map ERROR_MESSAGES = { {NON_SYSTEM_API, "Non-system api."}, }; +const std::map ERROR_MESSAGES = { + {SERVICE_EXCEPTION, "Service exception."}, + {PERMISSION_DENIED, "Permission denied."}, + {PARAMETER_ERROR, "The parameter invalid."}, + {SENSOR_NO_SUPPORT, "The sensor is not supported by the device."}, + {NON_SYSTEM_API, "Non-system api."}, +}; + napi_value CreateBusinessError(const napi_env &env, const int32_t errCode, const std::string &errMessage); void ThrowErr(const napi_env &env, const int32_t errCode, const std::string &printMsg, const std::string &correctMsg); +void ThrowErr(const napi_env &env, const int32_t errCode, const std::string &printMsg); +std::optional GetNapiError(int32_t errorCode); bool GetNapiError(int32_t errorCode, std::string &codeMsg); } // namespace Sensors } // namespace OHOS diff --git a/frameworks/js/napi/src/sensor_js.cpp b/frameworks/js/napi/src/sensor_js.cpp index c61b048c..68c41755 100644 --- a/frameworks/js/napi/src/sensor_js.cpp +++ b/frameworks/js/napi/src/sensor_js.cpp @@ -268,11 +268,8 @@ static napi_value Once(napi_env env, napi_callback_info info) if (!CheckSubscribe(sensorTypeId)) { SEN_HILOGD("No subscription to change sensor data, registration is required"); int32_t ret = SubscribeSensor(sensorTypeId, REPORTING_INTERVAL, DataCallbackImpl); - if (ret == PARAMETER_ERROR) { - ThrowErr(env, ret, "parameter verification failed", "enter the correct parameters"); - return nullptr; - } else if (ret != ERR_OK) { - ThrowErr(env, ret, "SubscribeSensor fail", "Must be positive"); + if (ret != ERR_OK) { + ThrowErr(env, ret, "SubscribeSensor fail"); return nullptr; } } @@ -376,14 +373,8 @@ static napi_value On(napi_env env, napi_callback_info info) } SEN_HILOGD("Interval is %{public}" PRId64, interval); int32_t ret = SubscribeSensor(sensorTypeId, interval, DataCallbackImpl); - if (ret == PARAMETER_ERROR) { - ThrowErr(env, ret, "parameter verification failed", "positive"); - return nullptr; - } else if (ret == SERVICE_EXCEPTION) { - ThrowErr(env, ret, "parameter verification failed", "positive"); - return nullptr; - } else if (ret != ERR_OK) { - ThrowErr(env, ret, "SubscribeSensor fail", "positive"); + if (ret != ERR_OK) { + ThrowErr(env, ret, "SubscribeSensor fail"); return nullptr; } UpdateCallbackInfos(env, sensorTypeId, args[1]); @@ -524,7 +515,7 @@ static napi_value GetGeomagneticField(napi_env env, napi_callback_info info) } napi_value napiLatitude = GetNamedProperty(env, args[0], "latitude"); if (napiLatitude == nullptr) { - ThrowErr(env, PARAMETER_ERROR, "napiLatitude is null", "positive"); + ThrowErr(env, PARAMETER_ERROR, "napiLatitude is null", "napi_value"); return nullptr; } double latitude = 0; @@ -534,7 +525,7 @@ static napi_value GetGeomagneticField(napi_env env, napi_callback_info info) } napi_value napiLongitude = GetNamedProperty(env, args[0], "longitude"); if (napiLongitude == nullptr) { - ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", "napiLongitude is positive"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", "napi_value"); return nullptr; } double longitude = 0; @@ -544,7 +535,7 @@ static napi_value GetGeomagneticField(napi_env env, napi_callback_info info) } napi_value napiAltitude = GetNamedProperty(env, args[0], "altitude"); if (napiAltitude == nullptr) { - ThrowErr(env, PARAMETER_ERROR, "napiAltitude is null", "napiAltitude is positive"); + ThrowErr(env, PARAMETER_ERROR, "napiAltitude is null", "napi_value"); return nullptr; } double altitude = 0; @@ -637,11 +628,8 @@ static napi_value TransformCoordinateSystem(napi_env env, napi_callback_info inf std::vector outRotationVector(length); SensorAlgorithm sensorAlgorithm; int32_t ret = sensorAlgorithm.TransformCoordinateSystem(inRotationVector, axisX, axisY, outRotationVector); - if (ret == PARAMETER_ERROR) { - ThrowErr(env, ret, "parameter verification failed", "positive"); - return nullptr; - } else if (ret != OHOS::ERR_OK) { - ThrowErr(env, ret, "Transform coordinate system fail", "Must be positive"); + if (ret != OHOS::ERR_OK) { + ThrowErr(env, ret, "Transform coordinate system fail"); return nullptr; } else { for (size_t i = 0; i < length; ++i) { @@ -691,7 +679,7 @@ static napi_value GetAngleModify(napi_env env, napi_callback_info info) SensorAlgorithm sensorAlgorithm; int32_t ret = sensorAlgorithm.GetAngleModify(curRotationVector, preRotationVector, angleChange); if (ret != OHOS::ERR_OK) { - ThrowErr(env, ret, "Get angle modify fail", "Must be positive"); + ThrowErr(env, ret, "Get angle modify fail"); return nullptr; } else { asyncCallbackInfo->data.reserveData.length = ROTATION_VECTOR_LENGTH; @@ -731,11 +719,8 @@ static napi_value GetDirection(napi_env env, napi_callback_info info) std::vector rotationAngle(ROTATION_VECTOR_LENGTH); SensorAlgorithm sensorAlgorithm; int32_t ret = sensorAlgorithm.GetDirection(rotationMatrix, rotationAngle); - if (ret == PARAMETER_ERROR) { - ThrowErr(env, ret, "parameter verification failed", "positive"); - return nullptr; - } else if (ret != OHOS::ERR_OK) { - ThrowErr(env, ret, "Get direction fail", "Must be positive"); + if (ret != OHOS::ERR_OK) { + ThrowErr(env, ret, "Get direction fail"); return nullptr; } else { asyncCallbackInfo->data.reserveData.length = ROTATION_VECTOR_LENGTH; @@ -775,11 +760,8 @@ static napi_value CreateQuaternion(napi_env env, napi_callback_info info) std::vector quaternion(QUATERNION_LENGTH); SensorAlgorithm sensorAlgorithm; int32_t ret = sensorAlgorithm.CreateQuaternion(rotationVector, quaternion); - if (ret == PARAMETER_ERROR) { - ThrowErr(env, ret, "parameter verification failed", "positive"); - return nullptr; - } else if (ret != OHOS::ERR_OK) { - ThrowErr(env, ret, "Get createQuaternion fail", "Must be positive"); + if (ret != OHOS::ERR_OK) { + ThrowErr(env, ret, "Get createQuaternion fail"); return nullptr; } else { asyncCallbackInfo->data.reserveData.length = QUATERNION_LENGTH; @@ -825,7 +807,7 @@ static napi_value GetAltitude(napi_env env, napi_callback_info info) SensorAlgorithm sensorAlgorithm; int32_t ret = sensorAlgorithm.GetAltitude(seaPressure, currentPressure, &altitude); if (ret != OHOS::ERR_OK) { - ThrowErr(env, ret, "Get altitude fail", "Must be positive"); + ThrowErr(env, ret, "Get altitude fail"); return nullptr; } else { asyncCallbackInfo->data.reserveData.reserve[0] = altitude; @@ -862,11 +844,8 @@ static napi_value GetGeomagneticDip(napi_env env, napi_callback_info info) float geomagneticDip = 0; SensorAlgorithm sensorAlgorithm; int32_t ret = sensorAlgorithm.GetGeomagneticDip(inclinationMatrix, &geomagneticDip); - if (ret == PARAMETER_ERROR) { - ThrowErr(env, ret, "parameter verification failed", "positive"); - return nullptr; - } else if (ret != OHOS::ERR_OK) { - ThrowErr(env, ret, "Get geomagnetic dip fail", "Must be positive"); + if (ret != OHOS::ERR_OK) { + ThrowErr(env, ret, "Get geomagnetic dip fail"); return nullptr; } else { asyncCallbackInfo->data.reserveData.reserve[0] = geomagneticDip; @@ -902,7 +881,7 @@ static napi_value CreateRotationAndInclination(const napi_env &env, napi_value a SensorAlgorithm sensorAlgorithm; int32_t ret = sensorAlgorithm.CreateRotationAndInclination(gravity, geomagnetic, rotation, inclination); if (ret != OHOS::ERR_OK) { - ThrowErr(env, ret, "Create rotation and inclination matrix fail", "Must be positive"); + ThrowErr(env, ret, "Create rotation and inclination matrix fail"); return nullptr; } else { asyncCallbackInfo->data.reserveData.length = THREE_DIMENSIONAL_MATRIX_LENGTH; @@ -938,7 +917,7 @@ static napi_value GetRotationMatrix(const napi_env &env, napi_value args[], size SensorAlgorithm sensorAlgorithm; int32_t ret = sensorAlgorithm.CreateRotationMatrix(rotationVector, rotationMatrix); if (ret != OHOS::ERR_OK) { - ThrowErr(env, ret, "parameter verification failed", "positive"); + ThrowErr(env, ret, "Create rotation matrix fail"); return nullptr; } else { asyncCallbackInfo->data.reserveData.length = THREE_DIMENSIONAL_MATRIX_LENGTH; @@ -1027,7 +1006,7 @@ static napi_value GetSensorListSync(napi_env env, napi_callback_info info) int32_t count = 0; int32_t ret = GetAllSensors(&sensorInfos, &count); if (ret != OHOS::ERR_OK) { - ThrowErr(env, ret, "parameter verification failed", "positive"); + ThrowErr(env, ret, "Get sensor list fail"); return result; } vector sensorInfoVec; @@ -1095,7 +1074,7 @@ static napi_value GetSingleSensor(napi_env env, napi_callback_info info) } } if (asyncCallbackInfo->sensorInfos.empty()) { - ThrowErr(env, SENSOR_NO_SUPPORT, "parameter verification failed", "The sensor is supported by the device"); + ThrowErr(env, SENSOR_NO_SUPPORT, "The sensor is supported by the device"); return nullptr; } } @@ -1127,7 +1106,7 @@ static napi_value GetSingleSensorSync(napi_env env, napi_callback_info info) int32_t count = 0; int32_t ret = GetAllSensors(&sensorInfos, &count); if (ret != OHOS::ERR_OK) { - ThrowErr(env, ret, "Get sensor list fail", "Must be positive"); + ThrowErr(env, ret, "Get sensor list fail"); return result; } vector sensorInfoVec; @@ -1143,7 +1122,7 @@ static napi_value GetSingleSensorSync(napi_env env, napi_callback_info info) } } if (sensorInfoVec.empty()) { - ThrowErr(env, SENSOR_NO_SUPPORT, "parameter verification failed", "The sensor is supported by the device"); + ThrowErr(env, SENSOR_NO_SUPPORT, "The sensor is supported by the device"); return result; } if (!ConvertToSensorInfo(env, sensorInfoVec[0], result)) { diff --git a/frameworks/js/napi/src/sensor_napi_error.cpp b/frameworks/js/napi/src/sensor_napi_error.cpp index be05c915..3aac865a 100644 --- a/frameworks/js/napi/src/sensor_napi_error.cpp +++ b/frameworks/js/napi/src/sensor_napi_error.cpp @@ -42,8 +42,8 @@ napi_value CreateBusinessError(const napi_env &env, const int32_t errCode, const bool GetNapiError(int32_t errorCode, std::string &codeMsg) { - auto iter = ERROR_MESSAGES.find(errorCode); - if (iter == ERROR_MESSAGES.end()) { + auto iter = ERROR_CODE_MESSAGES.find(errorCode); + if (iter == ERROR_CODE_MESSAGES.end()) { SEN_HILOGE("errorCode %{public}d not found", errorCode); return false; } @@ -51,6 +51,30 @@ bool GetNapiError(int32_t errorCode, std::string &codeMsg) return true; } +std::optional GetNapiError(int32_t errorCode) +{ + auto iter = ERROR_MESSAGES.find(errorCode); + if (iter != ERROR_MESSAGES.end()) { + return iter->second; + } + return std::nullopt; +} + +void ThrowErr(const napi_env &env, const int32_t errCode, const std::string &printMsg) +{ + SEN_HILOGE("Message:%{public}s, code:%{public}d", printMsg.c_str(), errCode); + auto msg = GetNapiError(errCode); + if (!msg) { + SEN_HILOGE("ErrCode:%{public}d is invalid", errCode); + return; + } + napi_handle_scope scope = nullptr; + napi_open_handle_scope(env, &scope); + napi_value error = CreateBusinessError(env, errCode, msg.value()); + napi_throw(env, error); + napi_close_handle_scope(env, scope); +} + void ThrowErr(const napi_env &env, const int32_t errCode, const std::string &printMsg, const std::string &correctMsg) { SEN_HILOGE("printMsg:%{public}s, correctMsg:%{public}s, code:%{public}d", printMsg.c_str(), correctMsg.c_str(), errCode); -- Gitee From bcb874a4808f289e5e7e042fff3ff30a9ba44d78 Mon Sep 17 00:00:00 2001 From: cff-gite Date: Thu, 6 Jun 2024 18:14:49 +0800 Subject: [PATCH 7/8] sensor error code rectification Signed-off-by: cff-gite Change-Id: I715b6038de637621bcef9063371c1182ef97e5a8 --- frameworks/js/napi/src/sensor_napi_error.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/frameworks/js/napi/src/sensor_napi_error.cpp b/frameworks/js/napi/src/sensor_napi_error.cpp index 3aac865a..99f933e1 100644 --- a/frameworks/js/napi/src/sensor_napi_error.cpp +++ b/frameworks/js/napi/src/sensor_napi_error.cpp @@ -82,8 +82,12 @@ void ThrowErr(const napi_env &env, const int32_t errCode, const std::string &pri if (GetNapiError(errCode, codeMsg)) { char buf[SENSOR_BUFF]; if (sprintf_s(buf, sizeof(buf), codeMsg.c_str(), printMsg.c_str(), correctMsg.c_str()) > 0 ) { + napi_handle_scope scope = nullptr; + napi_open_handle_scope(env, &scope); + napi_value error =CreateBusinessError(env, errCode, buf); SEN_HILOGE("Message buf:%{public}s", buf); - CreateBusinessError(env, errCode, buf); + napi_throw(env, error); + napi_close_handle_scope(env, scope); } else { SEN_HILOGE("Failed to convert string type to char type"); } -- Gitee From e063263172d8bd1c1cd6e75d4d9fc959054d0e31 Mon Sep 17 00:00:00 2001 From: cff-gite Date: Tue, 11 Jun 2024 16:12:52 +0800 Subject: [PATCH 8/8] sensor error code rectification Signed-off-by: cff-gite Change-Id: I59931ef1f62fda2bd4df7c3dc83d7a87b990df10 --- frameworks/js/napi/include/sensor_napi_error.h | 2 +- frameworks/js/napi/src/sensor_napi_error.cpp | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/frameworks/js/napi/include/sensor_napi_error.h b/frameworks/js/napi/include/sensor_napi_error.h index d76f322e..a1e0f7b3 100644 --- a/frameworks/js/napi/include/sensor_napi_error.h +++ b/frameworks/js/napi/include/sensor_napi_error.h @@ -24,7 +24,7 @@ namespace OHOS { namespace Sensors { -const std::map ERROR_CODE_MESSAGES = { +const std::map ACCURATE_MESSAGES = { {SERVICE_EXCEPTION, "Service exception."}, {PERMISSION_DENIED, "Permission denied. An attempt was made to %s forbidden by permission:%s."}, {PARAMETER_ERROR, "Parameter error. The type of %s must be %s."}, diff --git a/frameworks/js/napi/src/sensor_napi_error.cpp b/frameworks/js/napi/src/sensor_napi_error.cpp index 99f933e1..1150e0e5 100644 --- a/frameworks/js/napi/src/sensor_napi_error.cpp +++ b/frameworks/js/napi/src/sensor_napi_error.cpp @@ -42,8 +42,8 @@ napi_value CreateBusinessError(const napi_env &env, const int32_t errCode, const bool GetNapiError(int32_t errorCode, std::string &codeMsg) { - auto iter = ERROR_CODE_MESSAGES.find(errorCode); - if (iter == ERROR_CODE_MESSAGES.end()) { + auto iter = ACCURATE_MESSAGES.find(errorCode); + if (iter == ACCURATE_MESSAGES.end()) { SEN_HILOGE("errorCode %{public}d not found", errorCode); return false; } -- Gitee