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 } // namespace
26
ThrowError(napi_env env,int32_t errCode,const std::string & errorMsg)27 void ThrowError(napi_env env, int32_t errCode, const std::string& errorMsg)
28 {
29 napi_throw(env, CreateJsError(env, errCode, errorMsg));
30 }
31
ThrowError(napi_env env,const AbilityErrorCode & err)32 void ThrowError(napi_env env, const AbilityErrorCode& err)
33 {
34 napi_throw(env, CreateJsError(env, static_cast<int32_t>(err), GetErrorMsg(err)));
35 }
36
ThrowTooFewParametersError(napi_env env)37 void ThrowTooFewParametersError(napi_env env)
38 {
39 napi_throw(env, CreateJsError(env,
40 static_cast<int32_t>(AbilityErrorCode::ERROR_CODE_INVALID_PARAM),
41 ERR_MSG_TOO_FEW_PARAM));
42 }
43
ThrowNoPermissionError(napi_env env,const std::string & permission)44 void ThrowNoPermissionError(napi_env env, const std::string& permission)
45 {
46 napi_throw(env, CreateJsError(env,
47 static_cast<int32_t>(AbilityErrorCode::ERROR_CODE_PERMISSION_DENIED),
48 GetNoPermissionErrorMsg(permission)));
49 }
50
ThrowErrorByNativeErr(napi_env env,int32_t err)51 void ThrowErrorByNativeErr(napi_env env, int32_t err)
52 {
53 napi_throw(env, CreateJsErrorByNativeErr(env, err));
54 }
55
CreateJsError(napi_env env,const AbilityErrorCode & err)56 napi_value CreateJsError(napi_env env, const AbilityErrorCode& err)
57 {
58 return CreateJsError(env, static_cast<int32_t>(err), GetErrorMsg(err));
59 }
60
CreateNoPermissionError(napi_env env,const std::string & permission)61 napi_value CreateNoPermissionError(napi_env env, const std::string& permission)
62 {
63 return CreateJsError(env,
64 static_cast<int32_t>(AbilityErrorCode::ERROR_CODE_PERMISSION_DENIED),
65 GetNoPermissionErrorMsg(permission));
66 }
67
CreateJsErrorByNativeErr(napi_env env,int32_t err,const std::string & permission)68 napi_value CreateJsErrorByNativeErr(napi_env env, int32_t err, const std::string& permission)
69 {
70 auto errCode = GetJsErrorCodeByNativeError(err);
71 auto errMsg = (errCode == AbilityErrorCode::ERROR_CODE_PERMISSION_DENIED && !permission.empty()) ?
72 GetNoPermissionErrorMsg(permission) : GetErrorMsg(errCode);
73 return CreateJsError(env, static_cast<int32_t>(errCode), errMsg);
74 }
75 } // namespace AbilityRuntime
76 } // namespace OHOS