10826e83eSopenharmony_ci/* 20826e83eSopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd. 30826e83eSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 40826e83eSopenharmony_ci * you may not use this file except in compliance with the License. 50826e83eSopenharmony_ci * You may obtain a copy of the License at 60826e83eSopenharmony_ci * 70826e83eSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 80826e83eSopenharmony_ci * 90826e83eSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 100826e83eSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 110826e83eSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 120826e83eSopenharmony_ci * See the License for the specific language governing permissions and 130826e83eSopenharmony_ci * limitations under the License. 140826e83eSopenharmony_ci */ 150826e83eSopenharmony_ci 160826e83eSopenharmony_ci#include "business_error.h" 170826e83eSopenharmony_ci 180826e83eSopenharmony_ci#include "web_errors.h" 190826e83eSopenharmony_ci 200826e83eSopenharmony_cinamespace OHOS { 210826e83eSopenharmony_cinamespace NWebError { 220826e83eSopenharmony_cinapi_value BusinessError::CreateError(napi_env env, int32_t err) 230826e83eSopenharmony_ci{ 240826e83eSopenharmony_ci napi_value businessError = nullptr; 250826e83eSopenharmony_ci NAPI_CALL(env, napi_create_object(env, &businessError)); 260826e83eSopenharmony_ci napi_value errorCode = nullptr; 270826e83eSopenharmony_ci NAPI_CALL(env, napi_create_int32(env, err, &errorCode)); 280826e83eSopenharmony_ci napi_value errorMessage = nullptr; 290826e83eSopenharmony_ci NAPI_CALL(env, napi_create_string_utf8(env, GetErrMsgByErrCode(err).c_str(), NAPI_AUTO_LENGTH, &errorMessage)); 300826e83eSopenharmony_ci NAPI_CALL(env, napi_set_named_property(env, businessError, "code", errorCode)); 310826e83eSopenharmony_ci NAPI_CALL(env, napi_set_named_property(env, businessError, "message", errorMessage)); 320826e83eSopenharmony_ci return businessError; 330826e83eSopenharmony_ci} 340826e83eSopenharmony_ci 350826e83eSopenharmony_civoid BusinessError::ThrowErrorByErrcode(napi_env env, int32_t errCode) 360826e83eSopenharmony_ci{ 370826e83eSopenharmony_ci napi_throw_error(env, std::to_string(errCode).c_str(), GetErrMsgByErrCode(errCode).c_str()); 380826e83eSopenharmony_ci} 390826e83eSopenharmony_ci 400826e83eSopenharmony_civoid BusinessError::ThrowErrorByErrcode(napi_env env, int32_t errCode, const std::string& errorMsg) 410826e83eSopenharmony_ci{ 420826e83eSopenharmony_ci napi_throw_error(env, std::to_string(errCode).c_str(), errorMsg.c_str()); 430826e83eSopenharmony_ci} 440826e83eSopenharmony_ci} 450826e83eSopenharmony_ci}