137a09cd7Sopenharmony_ci/*
237a09cd7Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
337a09cd7Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
437a09cd7Sopenharmony_ci * you may not use this file except in compliance with the License.
537a09cd7Sopenharmony_ci * You may obtain a copy of the License at
637a09cd7Sopenharmony_ci *
737a09cd7Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
837a09cd7Sopenharmony_ci *
937a09cd7Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1037a09cd7Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1137a09cd7Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1237a09cd7Sopenharmony_ci * See the License for the specific language governing permissions and
1337a09cd7Sopenharmony_ci * limitations under the License.
1437a09cd7Sopenharmony_ci */
1537a09cd7Sopenharmony_ci
1637a09cd7Sopenharmony_ci#include "napi_errors.h"
1737a09cd7Sopenharmony_ci#include "thermal_log.h"
1837a09cd7Sopenharmony_ci
1937a09cd7Sopenharmony_cinamespace OHOS {
2037a09cd7Sopenharmony_cinamespace PowerMgr {
2137a09cd7Sopenharmony_cistd::map<ThermalErrors, std::string> NapiErrors::errorTable_ = {
2237a09cd7Sopenharmony_ci    {ThermalErrors::ERR_CONNECTION_FAIL,   "Failed to connect to the service."},
2337a09cd7Sopenharmony_ci    {ThermalErrors::ERR_PERMISSION_DENIED, "Permission is denied"             },
2437a09cd7Sopenharmony_ci    {ThermalErrors::ERR_PARAM_INVALID,     "Invalid input parameter."         }
2537a09cd7Sopenharmony_ci};
2637a09cd7Sopenharmony_ci
2737a09cd7Sopenharmony_cinapi_value NapiErrors::GetNapiError(napi_env& env) const
2837a09cd7Sopenharmony_ci{
2937a09cd7Sopenharmony_ci    napi_value napiError;
3037a09cd7Sopenharmony_ci    if (!IsError()) {
3137a09cd7Sopenharmony_ci        napi_get_undefined(env, &napiError);
3237a09cd7Sopenharmony_ci        return napiError;
3337a09cd7Sopenharmony_ci    }
3437a09cd7Sopenharmony_ci
3537a09cd7Sopenharmony_ci    std::string msg;
3637a09cd7Sopenharmony_ci    auto item = errorTable_.find(code_);
3737a09cd7Sopenharmony_ci    if (item != errorTable_.end()) {
3837a09cd7Sopenharmony_ci        msg = item->second;
3937a09cd7Sopenharmony_ci    }
4037a09cd7Sopenharmony_ci    napi_value napiMsg;
4137a09cd7Sopenharmony_ci    NAPI_CALL(env, napi_create_string_utf8(env, msg.c_str(), msg.size(), &napiMsg));
4237a09cd7Sopenharmony_ci    NAPI_CALL(env, napi_create_error(env, nullptr, napiMsg, &napiError));
4337a09cd7Sopenharmony_ci
4437a09cd7Sopenharmony_ci    napi_value napiCode;
4537a09cd7Sopenharmony_ci    NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(code_), &napiCode));
4637a09cd7Sopenharmony_ci
4737a09cd7Sopenharmony_ci    napi_set_named_property(env, napiError, "code", napiCode);
4837a09cd7Sopenharmony_ci    napi_set_named_property(env, napiError, "message", napiMsg);
4937a09cd7Sopenharmony_ci
5037a09cd7Sopenharmony_ci    THERMAL_HILOGW(COMP_FWK, "throw error code: %{public}d, msg: %{public}s,",
5137a09cd7Sopenharmony_ci        static_cast<int32_t>(code_), msg.c_str());
5237a09cd7Sopenharmony_ci    return napiError;
5337a09cd7Sopenharmony_ci}
5437a09cd7Sopenharmony_ci
5537a09cd7Sopenharmony_cinapi_value NapiErrors::ThrowError(napi_env& env, ThermalErrors code)
5637a09cd7Sopenharmony_ci{
5737a09cd7Sopenharmony_ci    Error(code);
5837a09cd7Sopenharmony_ci    napi_value error = GetNapiError(env);
5937a09cd7Sopenharmony_ci    if (error != nullptr) {
6037a09cd7Sopenharmony_ci        napi_throw(env, error);
6137a09cd7Sopenharmony_ci    }
6237a09cd7Sopenharmony_ci    return nullptr;
6337a09cd7Sopenharmony_ci}
6437a09cd7Sopenharmony_ci} // namespace PowerMgr
6537a09cd7Sopenharmony_ci} // namespace OHOS
66