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 #include "js_error.h"
16
17 namespace OHOS::WallpaperNAPI {
18
ThrowError(napi_env env,int32_t errorCode,const std::string & errorMessage)19 void JsError::ThrowError(napi_env env, int32_t errorCode, const std::string &errorMessage)
20 {
21 HILOG_DEBUG("ThrowError in");
22 napi_value message;
23 NAPI_CALL_RETURN_VOID(env, napi_create_string_utf8(env, errorMessage.c_str(), NAPI_AUTO_LENGTH, &message));
24 napi_value error;
25 NAPI_CALL_RETURN_VOID(env, napi_create_error(env, nullptr, message, &error));
26 napi_value code;
27 NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, errorCode, &code));
28 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, error, "code", code));
29 NAPI_CALL_RETURN_VOID(env, napi_throw(env, error));
30 }
31
ConvertErrorCode(ErrorCode wallpaperErrorCode)32 JsErrorInfo JsError::ConvertErrorCode(ErrorCode wallpaperErrorCode)
33 {
34 HILOG_DEBUG("ConvertErrorCode in");
35 JsErrorInfo errorObject;
36 switch (wallpaperErrorCode) {
37 case E_PARAMETERS_INVALID:
38 errorObject.code = static_cast<int32_t>(ErrorThrowType::PARAMETER_ERROR);
39 errorObject.message = PARAMETERERRORMESSAGE;
40 break;
41 case E_NO_PERMISSION:
42 errorObject.code = static_cast<int32_t>(ErrorThrowType::PERMISSION_ERROR);
43 errorObject.message = PERMISSIONDENIEDMESSAGE;
44 break;
45 case E_NOT_SYSTEM_APP:
46 errorObject.code = static_cast<int32_t>(ErrorThrowType::SYSTEM_APP_PERMISSION_ERROR);
47 errorObject.message = PERMISSIONFAILEDMESSAGE;
48 break;
49 default:
50 HILOG_DEBUG("Non-existent error type!");
51 break;
52 }
53 return errorObject;
54 }
55 } // namespace OHOS::WallpaperNAPI