• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(NativeEngine & engine,int32_t errCode,const std::string & errorMsg)27 void ThrowError(NativeEngine& engine, int32_t errCode, const std::string& errorMsg)
28 {
29     engine.Throw(CreateJsError(engine, errCode, errorMsg));
30 }
31 
ThrowError(NativeEngine & engine,const AbilityErrorCode & err)32 void ThrowError(NativeEngine& engine, const AbilityErrorCode& err)
33 {
34     engine.Throw(CreateJsError(engine, static_cast<int32_t>(err), GetErrorMsg(err)));
35 }
36 
ThrowTooFewParametersError(NativeEngine & engine)37 void ThrowTooFewParametersError(NativeEngine& engine)
38 {
39     engine.Throw(CreateJsError(engine,
40         static_cast<int32_t>(AbilityErrorCode::ERROR_CODE_INVALID_PARAM),
41         ERR_MSG_TOO_FEW_PARAM));
42 }
43 
ThrowNoPermissionError(NativeEngine & engine,const std::string & permission)44 void ThrowNoPermissionError(NativeEngine& engine, const std::string& permission)
45 {
46     engine.Throw(CreateJsError(engine,
47         static_cast<int32_t>(AbilityErrorCode::ERROR_CODE_PERMISSION_DENIED),
48         GetNoPermissionErrorMsg(permission)));
49 }
50 
ThrowErrorByNativeErr(NativeEngine & engine,int32_t err)51 void ThrowErrorByNativeErr(NativeEngine& engine, int32_t err)
52 {
53     engine.Throw(CreateJsErrorByNativeErr(engine, err));
54 }
55 
CreateJsError(NativeEngine & engine,const AbilityErrorCode & err)56 NativeValue* CreateJsError(NativeEngine& engine, const AbilityErrorCode& err)
57 {
58     return CreateJsError(engine, static_cast<int32_t>(err), GetErrorMsg(err));
59 }
60 
CreateNoPermissionError(NativeEngine & engine,const std::string & permission)61 NativeValue* CreateNoPermissionError(NativeEngine& engine, const std::string& permission)
62 {
63     return CreateJsError(engine,
64         static_cast<int32_t>(AbilityErrorCode::ERROR_CODE_PERMISSION_DENIED),
65         GetNoPermissionErrorMsg(permission));
66 }
67 
CreateJsErrorByNativeErr(NativeEngine & engine,int32_t err,const std::string & permission)68 NativeValue* CreateJsErrorByNativeErr(NativeEngine& engine, 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(engine, static_cast<int32_t>(errCode), errMsg);
74 }
75 }  // namespace AbilityRuntime
76 }  // namespace OHOS