• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "napi_errors.h"
17 #include "thermal_log.h"
18 
19 namespace OHOS {
20 namespace PowerMgr {
21 std::map<ThermalErrors, std::string> NapiErrors::errorTable_ = {
22     {ThermalErrors::ERR_CONNECTION_FAIL,   "Connecting to the service failed."},
23     {ThermalErrors::ERR_PERMISSION_DENIED, "Permission is denied"             },
24     {ThermalErrors::ERR_PARAM_INVALID,     "Invalid input parameter."         }
25 };
26 
GetNapiError(napi_env & env) const27 napi_value NapiErrors::GetNapiError(napi_env& env) const
28 {
29     napi_value napiError;
30     if (!IsError()) {
31         napi_get_undefined(env, &napiError);
32         return napiError;
33     }
34 
35     std::string msg;
36     auto item = errorTable_.find(code_);
37     if (item != errorTable_.end()) {
38         msg = item->second;
39     }
40     napi_value napiMsg;
41     NAPI_CALL(env, napi_create_string_utf8(env, msg.c_str(), msg.size(), &napiMsg));
42     NAPI_CALL(env, napi_create_error(env, nullptr, napiMsg, &napiError));
43 
44     napi_value napiCode;
45     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(code_), &napiCode));
46 
47     napi_set_named_property(env, napiError, "code", napiCode);
48     napi_set_named_property(env, napiError, "message", napiMsg);
49 
50     THERMAL_HILOGW(COMP_FWK, "throw error code: %{public}d, msg: %{public}s,",
51         static_cast<int32_t>(code_), msg.c_str());
52     return napiError;
53 }
54 
ThrowError(napi_env & env,ThermalErrors code)55 napi_value NapiErrors::ThrowError(napi_env& env, ThermalErrors code)
56 {
57     Error(code);
58     napi_value error = GetNapiError(env);
59     if (error != nullptr) {
60         napi_throw(env, error);
61     }
62     return nullptr;
63 }
64 } // namespace PowerMgr
65 } // namespace OHOS
66