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 WINDOW_WINDOW_MANAGER_JS_ERROR_UTILS_H 17 #define WINDOW_WINDOW_MANAGER_JS_ERROR_UTILS_H 18 19 #include "native_engine/native_engine.h" 20 #include "native_engine/native_value.h" 21 22 #include "dm_common.h" 23 #include "wm_common.h" 24 25 namespace OHOS::Rosen { 26 class JsErrUtils { 27 public: 28 static napi_value CreateJsError(napi_env env, const WMError& errorCode, std::string msg = ""); 29 static napi_value CreateJsError(napi_env env, const WmErrorCode& errorCode, std::string msg = ""); 30 static napi_value CreateJsError(napi_env env, const DMError& errorCode, std::string msg = ""); 31 static napi_value CreateJsError(napi_env env, const DmErrorCode& errorCode, std::string msg = ""); 32 private: CreateJsNumber(napi_env env,int32_t value)33 static inline napi_value CreateJsNumber(napi_env env, int32_t value) 34 { 35 napi_value result = nullptr; 36 napi_create_int32(env, value, &result); 37 return result; 38 } 39 CreateJsNumber(napi_env env,uint32_t value)40 static inline napi_value CreateJsNumber(napi_env env, uint32_t value) 41 { 42 napi_value result = nullptr; 43 napi_create_uint32(env, value, &result); 44 return result; 45 } 46 CreateJsNumber(napi_env env,int64_t value)47 static inline napi_value CreateJsNumber(napi_env env, int64_t value) 48 { 49 napi_value result = nullptr; 50 napi_create_int64(env, value, &result); 51 return result; 52 } 53 CreateJsNumber(napi_env env,double value)54 static inline napi_value CreateJsNumber(napi_env env, double value) 55 { 56 napi_value result = nullptr; 57 napi_create_double(env, value, &result); 58 return result; 59 } 60 61 template<class T> 62 static napi_value CreateJsValue(napi_env env, const T& value); 63 64 static std::string GetErrorMsg(const WMError& errorCode); 65 static std::string GetErrorMsg(const WmErrorCode& errorCode); 66 static std::string GetErrorMsg(const DMError& errorCode); 67 static std::string GetErrorMsg(const DmErrorCode& errorCode); 68 }; 69 } // namespace OHOS::Rosen 70 71 #endif //WINDOW_WINDOW_MANAGER_JS_ERROR_UTILS_H 72