1 /* 2 * Copyright (c) 2022 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 JS_CONCURRENT_MODULE_COMMON_HELPER_ERROR_HELPER_H_ 17 #define JS_CONCURRENT_MODULE_COMMON_HELPER_ERROR_HELPER_H_ 18 19 #include "napi/native_api.h" 20 #include "napi/native_node_api.h" 21 #include "native_engine/native_engine.h" 22 23 namespace Commonlibrary::Concurrent::Common::Helper { 24 class ErrorHelper { 25 public: 26 ErrorHelper() = default; 27 ~ErrorHelper() = default; 28 NewError(napi_env env,int32_t errCode,const char * errMessage)29 static napi_value NewError(napi_env env, int32_t errCode, const char* errMessage) 30 { 31 std::string errTitle = ""; 32 napi_value concurrentError = nullptr; 33 34 napi_value code = nullptr; 35 napi_create_uint32(env, errCode, &code); 36 37 napi_value name = nullptr; 38 std::string errName = "BusinessError"; 39 if (errCode == WORKERINITIALIZATION_ERROR) { 40 errTitle = "Worker initialization failure, "; 41 } else if (errCode == WORKENOTRUNNING_ERROR) { 42 errTitle = "Worker instance is not running, "; 43 } else if (errCode == WORKERUNSUPPORTED_ERROR) { 44 errTitle = "The invoked API is not supported in workers, "; 45 } else if (errCode == WORKERSERIALIZATION_ERROR) { 46 errTitle = "Serializing an uncaught exception failed, "; 47 } else if (errCode == WORKERFILEPATH_ERROR) { 48 errTitle = "The worker file path is invalid path, "; 49 } else if (errCode == NOTCONCURRENTFUNCTION_ERROR) { 50 errTitle = "The function is not mark as concurrent, "; 51 } else if (errCode == NOTEXIST_ERROR) { 52 errTitle = "The task does not exist when it is canceled, "; 53 } else if (errCode == RUNNING_ERROR) { 54 errTitle = "The task is executing when it is canceled, "; 55 } 56 napi_create_string_utf8(env, errName.c_str(), NAPI_AUTO_LENGTH, &name); 57 napi_value msg = nullptr; 58 napi_create_string_utf8(env, (errTitle + std::string(errMessage)).c_str(), NAPI_AUTO_LENGTH, &msg); 59 60 napi_create_error(env, nullptr, msg, &concurrentError); 61 napi_set_named_property(env, concurrentError, "code", code); 62 napi_set_named_property(env, concurrentError, "name", name); 63 return concurrentError; 64 } 65 ThrowError(napi_env env,int32_t errCode,const char * errMessage)66 static void ThrowError(napi_env env, int32_t errCode, const char* errMessage) 67 { 68 napi_value concurrentError = NewError(env, errCode, errMessage); 69 napi_throw(env, concurrentError); 70 } 71 72 static const int32_t TYPE_ERROR = 401; // 401 : the parameter type is incorrect 73 static const int32_t WORKERINITIALIZATION_ERROR = 10200003; // 10200003 : worker initialization failure 74 static const int32_t WORKENOTRUNNING_ERROR = 10200004; // 10200004 : worker instance is not running 75 static const int32_t WORKERUNSUPPORTED_ERROR = 10200005; // 10200005 : the invoked API is not supported in workers 76 static const int32_t WORKERSERIALIZATION_ERROR = 10200006; // 10200006 : serialize an uncaught exception failed 77 static const int32_t WORKERFILEPATH_ERROR = 10200007; // 10200007 : the worker file path is invalid path 78 static const int32_t NOTCONCURRENTFUNCTION_ERROR = 10200014; // 10200014 : the function is not mark as concurrent 79 static const int32_t NOTEXIST_ERROR = 10200015; // 10200015 : the task does not exist when it is canceled 80 static const int32_t RUNNING_ERROR = 10200016; // 10200016 : the task is executing when it is canceled 81 }; 82 } // namespace Commonlibrary::Concurrent::Common::Helper 83 #endif // JS_CONCURRENT_MODULE_COMMON_HELPER_ERROR_HELPER_H_