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