19596a2c1Sopenharmony_ci/*
29596a2c1Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
39596a2c1Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
49596a2c1Sopenharmony_ci * you may not use this file except in compliance with the License.
59596a2c1Sopenharmony_ci * You may obtain a copy of the License at
69596a2c1Sopenharmony_ci *
79596a2c1Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
89596a2c1Sopenharmony_ci *
99596a2c1Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
109596a2c1Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
119596a2c1Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
129596a2c1Sopenharmony_ci * See the License for the specific language governing permissions and
139596a2c1Sopenharmony_ci * limitations under the License.
149596a2c1Sopenharmony_ci */
159596a2c1Sopenharmony_ci#include "error_util.h"
169596a2c1Sopenharmony_ci
179596a2c1Sopenharmony_ci#include <string>
189596a2c1Sopenharmony_ci#include <unordered_map>
199596a2c1Sopenharmony_ci
209596a2c1Sopenharmony_cinamespace OHOS {
219596a2c1Sopenharmony_cinamespace Global {
229596a2c1Sopenharmony_cinamespace I18n {
239596a2c1Sopenharmony_cistatic const std::unordered_map<int32_t, std::string> ErrorCodeToMsg {
249596a2c1Sopenharmony_ci    {I18N_NO_PERMISSION,
259596a2c1Sopenharmony_ci        "Permission verification failed. The application does not have the permission required to call the API."},
269596a2c1Sopenharmony_ci    {I18N_NOT_SYSTEM_APP,
279596a2c1Sopenharmony_ci        "Permission verification failed. A non-system application calls a system API."},
289596a2c1Sopenharmony_ci    {I18N_NOT_VALID, "Invalid parameter"},
299596a2c1Sopenharmony_ci    {I18N_NOT_FOUND, "Parameter error"},
309596a2c1Sopenharmony_ci    {I18N_OPTION_NOT_VALID, "Invalid option name"}
319596a2c1Sopenharmony_ci};
329596a2c1Sopenharmony_ci
339596a2c1Sopenharmony_civoid ErrorUtil::NapiThrow(napi_env env, int32_t errCode, const std::string& valueName,
349596a2c1Sopenharmony_ci    const std::string& valueContent, bool throwError)
359596a2c1Sopenharmony_ci{
369596a2c1Sopenharmony_ci    if (!throwError) {
379596a2c1Sopenharmony_ci        return;
389596a2c1Sopenharmony_ci    }
399596a2c1Sopenharmony_ci    napi_value code = nullptr;
409596a2c1Sopenharmony_ci    napi_create_string_latin1(env, std::to_string(errCode).c_str(), NAPI_AUTO_LENGTH, &code);
419596a2c1Sopenharmony_ci
429596a2c1Sopenharmony_ci    napi_value message = nullptr;
439596a2c1Sopenharmony_ci    auto iter = ErrorCodeToMsg.find(errCode);
449596a2c1Sopenharmony_ci    std::string errMsg = iter != ErrorCodeToMsg.end() ? iter->second : "";
459596a2c1Sopenharmony_ci    std::string allErrMsg;
469596a2c1Sopenharmony_ci
479596a2c1Sopenharmony_ci    if (errCode == I18N_NO_PERMISSION || errCode == I18N_NOT_SYSTEM_APP) {
489596a2c1Sopenharmony_ci        allErrMsg = errMsg;
499596a2c1Sopenharmony_ci    } else if (errCode == I18N_NOT_VALID) {
509596a2c1Sopenharmony_ci        allErrMsg = errMsg + ", the " + valueName + " must be " + valueContent + ".";
519596a2c1Sopenharmony_ci    } else if (valueContent.length() == 0) {
529596a2c1Sopenharmony_ci        allErrMsg = errMsg + ", the " + valueName + " cannot be empty.";
539596a2c1Sopenharmony_ci    } else {
549596a2c1Sopenharmony_ci        allErrMsg = errMsg + ", the type of " + valueName + " must be " + valueContent + ".";
559596a2c1Sopenharmony_ci    }
569596a2c1Sopenharmony_ci
579596a2c1Sopenharmony_ci    napi_create_string_latin1(env, allErrMsg.c_str(), NAPI_AUTO_LENGTH, &message);
589596a2c1Sopenharmony_ci
599596a2c1Sopenharmony_ci    napi_value error = nullptr;
609596a2c1Sopenharmony_ci    napi_create_error(env, code, message, &error);
619596a2c1Sopenharmony_ci    napi_throw(env, error);
629596a2c1Sopenharmony_ci}
639596a2c1Sopenharmony_ci} // namespace I18n
649596a2c1Sopenharmony_ci} // namespace Global
659596a2c1Sopenharmony_ci} // namespace OHOS