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 "business_error.h"
17
18#include "web_errors.h"
19
20namespace OHOS {
21namespace NWebError {
22napi_value BusinessError::CreateError(napi_env env, int32_t err)
23{
24    napi_value businessError = nullptr;
25    NAPI_CALL(env, napi_create_object(env, &businessError));
26    napi_value errorCode = nullptr;
27    NAPI_CALL(env, napi_create_int32(env, err, &errorCode));
28    napi_value errorMessage = nullptr;
29    NAPI_CALL(env, napi_create_string_utf8(env, GetErrMsgByErrCode(err).c_str(), NAPI_AUTO_LENGTH, &errorMessage));
30    NAPI_CALL(env, napi_set_named_property(env, businessError, "code", errorCode));
31    NAPI_CALL(env, napi_set_named_property(env, businessError, "message", errorMessage));
32    return businessError;
33}
34
35void BusinessError::ThrowErrorByErrcode(napi_env env, int32_t errCode)
36{
37    napi_throw_error(env, std::to_string(errCode).c_str(), GetErrMsgByErrCode(errCode).c_str());
38}
39
40void BusinessError::ThrowErrorByErrcode(napi_env env, int32_t errCode, const std::string& errorMsg)
41{
42    napi_throw_error(env, std::to_string(errCode).c_str(), errorMsg.c_str());
43}
44}
45}