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 7a8bbb26595973831ac2c7baf3a00b36d7d0331a..a1e0f7b36b3beda6b1862cf30de9250e0113cdd9 --- a/frameworks/js/napi/include/sensor_napi_error.h +++ b/frameworks/js/napi/include/sensor_napi_error.h @@ -24,6 +24,14 @@ namespace OHOS { namespace Sensors { +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."}, + {SENSOR_NO_SUPPORT, "The sensor is not supported by the device."}, + {NON_SYSTEM_API, "Non-system api."}, +}; + const std::map ERROR_MESSAGES = { {SERVICE_EXCEPTION, "Service exception."}, {PERMISSION_DENIED, "Permission denied."}, @@ -33,8 +41,10 @@ const std::map ERROR_MESSAGES = { }; 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 #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 655b95c14b36f2393f90422dad4d6dae7790e24f..68c41755fe4e5ba35971aa40ea83159985137b27 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", "create reference"); return; } std::vector> callbackInfos = g_onceCallbackInfos[sensorTypeId]; @@ -253,16 +253,16 @@ 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", "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", "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", "sensorTypeId"); return nullptr; } if (!CheckSubscribe(sensorTypeId)) { @@ -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", "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, "napi_get_cb_info fail or number of parameter invalid"); + 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, "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", "sensorTypeId"); return nullptr; } int64_t interval = REPORTING_INTERVAL; @@ -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", "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", "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, "Wrong argument type, args[1] should is napi_function"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", "napi_function"); return nullptr; } if (CheckSystemSubscribe(sensorTypeId) || (subscribeSize > 0)) { @@ -467,8 +467,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) { - ThrowErr(env, ret, "UnsubscribeSensor fail"); + 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; } @@ -504,46 +506,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", "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", "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"); + ThrowErr(env, PARAMETER_ERROR, "napiLatitude is null", "napi_value"); return nullptr; } double latitude = 0; if (!GetNativeDouble(env, napiLatitude, latitude)) { - ThrowErr(env, PARAMETER_ERROR, "Get latitude fail"); + 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"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", "napi_value"); return nullptr; } double longitude = 0; if (!GetNativeDouble(env, napiLongitude, longitude)) { - ThrowErr(env, PARAMETER_ERROR, "Get longitude fail"); + 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"); + ThrowErr(env, PARAMETER_ERROR, "napiAltitude is null", "napi_value"); return nullptr; } double altitude = 0; if (!GetNativeDouble(env, napiAltitude, altitude)) { - ThrowErr(env, PARAMETER_ERROR, "Get altitude fail"); + 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"); + ThrowErr(env, PARAMETER_ERROR, "get timeMillis fail", "int64_t"); return nullptr; } GeomagneticField geomagneticField(latitude, longitude, altitude, timeMillis); @@ -583,41 +585,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", "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", "array or 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 success"); 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", "correct 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", "napi_value"); 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", "int32_t"); 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", "napi_value"); 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", "int32_t"); return nullptr; } sptr asyncCallbackInfo = @@ -649,15 +651,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", "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", "correct 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", "correct argument type"); return nullptr; } sptr asyncCallbackInfo = @@ -665,12 +667,12 @@ 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", "float array"); 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", "float array"); return nullptr; } std::vector angleChange(ROTATION_VECTOR_LENGTH); @@ -699,11 +701,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", "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", "correct argument type"); return nullptr; } sptr asyncCallbackInfo = @@ -711,7 +713,7 @@ 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", "float array"); return nullptr; } std::vector rotationAngle(ROTATION_VECTOR_LENGTH); @@ -740,11 +742,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", "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", "correct argument type"); return nullptr; } sptr asyncCallbackInfo = @@ -752,14 +754,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", "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, "CreateQuaternion fail"); + ThrowErr(env, ret, "Get createQuaternion fail"); return nullptr; } else { asyncCallbackInfo->data.reserveData.length = QUATERNION_LENGTH; @@ -781,11 +783,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", "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", "napi_number"); return nullptr; } sptr asyncCallbackInfo = @@ -793,12 +795,12 @@ 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", "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", "float"); return nullptr; } float altitude = 0; @@ -824,11 +826,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", "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", "correct argument type"); return nullptr; } sptr asyncCallbackInfo = @@ -836,7 +838,7 @@ 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", "float array"); return nullptr; } float geomagneticDip = 0; @@ -858,17 +860,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", "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", "float array"); 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", "float array"); return nullptr; } std::vector rotation(THREE_DIMENSIONAL_MATRIX_LENGTH); @@ -900,12 +902,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, "parameter verification failed", "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", "float array"); return nullptr; } sptr asyncCallbackInfo = @@ -937,11 +939,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, "parameter verification failed", "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", "correct argument type"); return nullptr; } if (argc >= 2 && IsMatchArrayType(env, args[1])) { @@ -959,7 +961,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", "enter the correct parameters"); return nullptr; } sptr asyncCallbackInfo = @@ -997,7 +999,7 @@ 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", "enter the correct parameters"); return result; } SensorInfo *sensorInfos = nullptr; @@ -1017,17 +1019,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 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, "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, "napi_set_element fail"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", "napi_set_element success"); } } return result; @@ -1041,12 +1043,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", "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", "sensorTypeId"); return nullptr; } sptr asyncCallbackInfo = @@ -1072,7 +1074,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, "The sensor is supported by the device"); return nullptr; } } @@ -1092,12 +1094,12 @@ 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", "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", "correct argument type"); return result; } SensorInfo *sensorInfos = nullptr; @@ -1120,11 +1122,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, "The sensor is 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 success"); } return result; } @@ -1137,11 +1139,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", "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", "napi_object"); return nullptr; } string interval = "normal"; @@ -1209,7 +1211,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 success"); return nullptr; } if (!RemoveSubscribeCallback(env, sensorTypeId) || CheckSubscribe(sensorTypeId)) { @@ -1231,11 +1233,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", "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", "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 fa3c948ec489fb7cd8cc8aee93c96668e06312e7..1150e0e5708574ef2b88612efc414f4b4f708537 100644 --- a/frameworks/js/napi/src/sensor_napi_error.cpp +++ b/frameworks/js/napi/src/sensor_napi_error.cpp @@ -17,11 +17,17 @@ #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; @@ -34,6 +40,17 @@ napi_value CreateBusinessError(const napi_env &env, const int32_t errCode, const return businessError; } +bool GetNapiError(int32_t errorCode, std::string &codeMsg) +{ + auto iter = ACCURATE_MESSAGES.find(errorCode); + if (iter == ACCURATE_MESSAGES.end()) { + SEN_HILOGE("errorCode %{public}d not found", errorCode); + return false; + } + codeMsg = iter->second; + return true; +} + std::optional GetNapiError(int32_t errorCode) { auto iter = ERROR_MESSAGES.find(errorCode); @@ -57,5 +74,24 @@ void ThrowErr(const napi_env &env, const int32_t errCode, const std::string &pri 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); + std::string codeMsg; + 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); + napi_throw(env, error); + napi_close_handle_scope(env, scope); + } else { + SEN_HILOGE("Failed to convert string type to char type"); + } + } +} } // 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 dbc0cc982c70aa023be48728870c5af6be4ba491..2230069fb2dc00a9910521d2d7dfc7653b33a5ac 100644 --- a/frameworks/js/napi/src/sensor_napi_utils.cpp +++ b/frameworks/js/napi/src/sensor_napi_utils.cpp @@ -303,12 +303,13 @@ 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; } - result[0] = CreateBusinessError(env, code, msg.value()); + result[0] = CreateBusinessError(env, code, codeMsg); return (result[0] != nullptr); }