1 /*
2 * Copyright (c) 2021-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
17 #include "js_error_utils.h"
18
19 #include "js_runtime_utils.h"
20
21 namespace OHOS {
22 namespace AbilityRuntime {
23 namespace {
24 constexpr const char* ERR_MSG_TOO_FEW_PARAM = "Parameter error. Too few parameters.";
25 constexpr const char* ERR_MSG_NOT_MAINTHREAD = "Caller error. Caller from non-main thread.";
26 constexpr const char* ERR_MSG_INVALID_NUM_PARAMS = "Parameter error. The number of parameters is invalid.";
27 constexpr const char* NOT_SYSTEM_APP = "The application is not system-app, can not use system-api.";
28 } // namespace
29
ThrowError(napi_env env,int32_t errCode,const std::string & errorMsg)30 void ThrowError(napi_env env, int32_t errCode, const std::string& errorMsg)
31 {
32 napi_throw(env, CreateJsError(env, errCode, errorMsg));
33 }
34
ThrowError(napi_env env,const AbilityErrorCode & err)35 void ThrowError(napi_env env, const AbilityErrorCode& err)
36 {
37 napi_throw(env, CreateJsError(env, static_cast<int32_t>(err), GetErrorMsg(err)));
38 }
39
ThrowInvalidCallerError(napi_env env)40 void ThrowInvalidCallerError(napi_env env)
41 {
42 napi_throw(env, CreateJsError(env,
43 static_cast<int32_t>(AbilityErrorCode::ERROR_CODE_INVALID_CALLER),
44 ERR_MSG_NOT_MAINTHREAD));
45 }
46
ThrowTooFewParametersError(napi_env env)47 void ThrowTooFewParametersError(napi_env env)
48 {
49 napi_throw(env, CreateJsError(env,
50 static_cast<int32_t>(AbilityErrorCode::ERROR_CODE_INVALID_PARAM),
51 ERR_MSG_TOO_FEW_PARAM));
52 }
53
ThrowInvalidNumParametersError(napi_env env)54 void ThrowInvalidNumParametersError(napi_env env)
55 {
56 napi_throw(env, CreateJsError(env,
57 static_cast<int32_t>(AbilityErrorCode::ERROR_CODE_INVALID_PARAM),
58 ERR_MSG_INVALID_NUM_PARAMS));
59 }
60
ThrowNoPermissionError(napi_env env,const std::string & permission)61 void ThrowNoPermissionError(napi_env env, const std::string& permission)
62 {
63 napi_throw(env, CreateJsError(env,
64 static_cast<int32_t>(AbilityErrorCode::ERROR_CODE_PERMISSION_DENIED),
65 GetNoPermissionErrorMsg(permission)));
66 }
67
ThrowNotSystemAppError(napi_env env)68 void ThrowNotSystemAppError(napi_env env)
69 {
70 napi_throw(env, CreateJsError(env,
71 static_cast<int32_t>(AbilityErrorCode::ERROR_CODE_NOT_SYSTEM_APP),
72 NOT_SYSTEM_APP));
73 }
74
ThrowInvalidParamError(napi_env env,const std::string & message)75 void ThrowInvalidParamError(napi_env env, const std::string &message)
76 {
77 napi_throw(env, CreateInvalidParamJsError(env, message));
78 }
79
ThrowErrorByNativeErr(napi_env env,int32_t err)80 void ThrowErrorByNativeErr(napi_env env, int32_t err)
81 {
82 napi_throw(env, CreateJsErrorByNativeErr(env, err));
83 }
84
CreateJsError(napi_env env,const AbilityErrorCode & err)85 napi_value CreateJsError(napi_env env, const AbilityErrorCode& err)
86 {
87 return CreateJsError(env, static_cast<int32_t>(err), GetErrorMsg(err));
88 }
89
CreateInvalidParamJsError(napi_env env,const std::string & message)90 napi_value CreateInvalidParamJsError(napi_env env, const std::string &message)
91 {
92 return CreateJsError(env, static_cast<int32_t>(AbilityErrorCode::ERROR_CODE_INVALID_PARAM), message);
93 }
94
CreateNoPermissionError(napi_env env,const std::string & permission)95 napi_value CreateNoPermissionError(napi_env env, const std::string& permission)
96 {
97 return CreateJsError(env,
98 static_cast<int32_t>(AbilityErrorCode::ERROR_CODE_PERMISSION_DENIED),
99 GetNoPermissionErrorMsg(permission));
100 }
101
CreateJsErrorByNativeErr(napi_env env,int32_t err,const std::string & permission)102 napi_value CreateJsErrorByNativeErr(napi_env env, int32_t err, const std::string& permission)
103 {
104 auto errCode = GetJsErrorCodeByNativeError(err);
105 auto errMsg = (errCode == AbilityErrorCode::ERROR_CODE_PERMISSION_DENIED && !permission.empty()) ?
106 GetNoPermissionErrorMsg(permission) : GetErrorMsg(errCode);
107 return CreateJsError(env, static_cast<int32_t>(errCode), errMsg);
108 }
109 } // namespace AbilityRuntime
110 } // namespace OHOS