14d6c458bSopenharmony_ci/*
24d6c458bSopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
34d6c458bSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
44d6c458bSopenharmony_ci * you may not use this file except in compliance with the License.
54d6c458bSopenharmony_ci * You may obtain a copy of the License at
64d6c458bSopenharmony_ci *
74d6c458bSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
84d6c458bSopenharmony_ci *
94d6c458bSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
104d6c458bSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
114d6c458bSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
124d6c458bSopenharmony_ci * See the License for the specific language governing permissions and
134d6c458bSopenharmony_ci * limitations under the License.
144d6c458bSopenharmony_ci */
154d6c458bSopenharmony_ci
164d6c458bSopenharmony_ci#ifndef COMMONLIBRARY_ETS_UTILS_TOOLS_ETS_ERROR_H
174d6c458bSopenharmony_ci#define COMMONLIBRARY_ETS_UTILS_TOOLS_ETS_ERROR_H
184d6c458bSopenharmony_ci
194d6c458bSopenharmony_ci#include <string>
204d6c458bSopenharmony_ci
214d6c458bSopenharmony_ci#include "napi/native_api.h"
224d6c458bSopenharmony_ci#include "napi/native_node_api.h"
234d6c458bSopenharmony_ci
244d6c458bSopenharmony_cinamespace OHOS::Tools {
254d6c458bSopenharmony_ciclass ErrorHelper {
264d6c458bSopenharmony_cipublic:
274d6c458bSopenharmony_ci    ErrorHelper() = default;
284d6c458bSopenharmony_ci    ~ErrorHelper() = default;
294d6c458bSopenharmony_ci
304d6c458bSopenharmony_ci    static napi_value ThrowError(napi_env env, int32_t errCode, const char* errMessage)
314d6c458bSopenharmony_ci    {
324d6c458bSopenharmony_ci        napi_value errorValue = nullptr;
334d6c458bSopenharmony_ci        // err-name
344d6c458bSopenharmony_ci        std::string errName = "BusinessError";
354d6c458bSopenharmony_ci        napi_value name = nullptr;
364d6c458bSopenharmony_ci        napi_create_string_utf8(env, errName.c_str(), NAPI_AUTO_LENGTH, &name);
374d6c458bSopenharmony_ci        // err-code
384d6c458bSopenharmony_ci        napi_value code = nullptr;
394d6c458bSopenharmony_ci        napi_create_int32(env, errCode, &code);
404d6c458bSopenharmony_ci        // err-message
414d6c458bSopenharmony_ci        napi_value msg = nullptr;
424d6c458bSopenharmony_ci        napi_create_string_utf8(env, errMessage, NAPI_AUTO_LENGTH, &msg);
434d6c458bSopenharmony_ci
444d6c458bSopenharmony_ci        napi_create_error(env, nullptr, msg, &errorValue);
454d6c458bSopenharmony_ci        napi_set_named_property(env, errorValue, "code", code);
464d6c458bSopenharmony_ci        napi_set_named_property(env, errorValue, "name", name);
474d6c458bSopenharmony_ci
484d6c458bSopenharmony_ci        napi_throw(env, errorValue);
494d6c458bSopenharmony_ci        return nullptr;
504d6c458bSopenharmony_ci    }
514d6c458bSopenharmony_ci};
524d6c458bSopenharmony_ci} // namespace OHOS::Tools
534d6c458bSopenharmony_ci#endif /* COMMONLIBRARY_ETS_UTILS_TOOLS_ETS_ERROR_H */
54