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