1 /* 2 * Copyright (c) 2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef COMMONLIBRARY_ETS_UTILS_TOOLS_ETS_ERROR_H 17 #define COMMONLIBRARY_ETS_UTILS_TOOLS_ETS_ERROR_H 18 19 #include <string> 20 21 #include "napi/native_api.h" 22 #include "napi/native_node_api.h" 23 24 namespace OHOS::Tools { 25 class ErrorHelper { 26 public: 27 ErrorHelper() = default; 28 ~ErrorHelper() = default; 29 ThrowError(napi_env env,int32_t errCode,const char * errMessage)30 static napi_value ThrowError(napi_env env, int32_t errCode, const char* errMessage) 31 { 32 napi_value errorValue = nullptr; 33 // err-name 34 std::string errName = "BusinessError"; 35 napi_value name = nullptr; 36 napi_create_string_utf8(env, errName.c_str(), NAPI_AUTO_LENGTH, &name); 37 // err-code 38 napi_value code = nullptr; 39 napi_create_int32(env, errCode, &code); 40 // err-message 41 napi_value msg = nullptr; 42 napi_create_string_utf8(env, errMessage, NAPI_AUTO_LENGTH, &msg); 43 44 napi_create_error(env, nullptr, msg, &errorValue); 45 napi_set_named_property(env, errorValue, "code", code); 46 napi_set_named_property(env, errorValue, "name", name); 47 48 napi_throw(env, errorValue); 49 return nullptr; 50 } 51 }; 52 } // namespace OHOS::Tools 53 #endif /* COMMONLIBRARY_ETS_UTILS_TOOLS_ETS_ERROR_H */ 54