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 POWER_NAPI_ERRORS_H 17 #define POWER_NAPI_ERRORS_H 18 19 #include <map> 20 #include <string> 21 22 #include "napi/native_node_api.h" 23 #include "power_errors.h" 24 25 namespace OHOS { 26 namespace PowerMgr { 27 class NapiErrors { 28 public: NapiErrors()29 NapiErrors() {} NapiErrors(PowerErrors code)30 explicit NapiErrors(PowerErrors code) : code_(code) {} 31 napi_value GetNapiError(napi_env& env) const; 32 napi_value ThrowError(napi_env& env, PowerErrors code = PowerErrors::ERR_OK); Error(PowerErrors code)33 inline void Error(PowerErrors code) 34 { 35 code_ = (code != PowerErrors::ERR_OK) ? code : code_; 36 } IsError()37 inline bool IsError() const 38 { 39 return code_ != PowerErrors::ERR_OK; 40 } 41 42 private: 43 PowerErrors code_ {PowerErrors::ERR_OK}; 44 static std::map<PowerErrors, std::string> errorTable_; 45 }; 46 } // namespace PowerMgr 47 } // namespace OHOS 48 49 #endif // POWER_NAPI_ERRORS_H 50