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