• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2023 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 #include "n_error.h"
17 
18 #include <cstring>
19 
20 #include "filemgmt_libhilog.h"
21 #include "n_val.h"
22 
23 namespace OHOS {
24 namespace FileManagement {
25 namespace LibN {
26 using namespace std;
27 
GenerateBusinessError(napi_env env,int32_t errCode,string errMsg)28 static napi_value GenerateBusinessError(napi_env env, int32_t errCode, string errMsg)
29 {
30     napi_value businessError = nullptr;
31     napi_value code = nullptr;
32     napi_value msg = nullptr;
33     napi_status status;
34     code = NVal::CreateInt32(env, errCode).val_;
35     msg = NVal::CreateUTF8String(env, errMsg).val_;
36     status = napi_create_error(env, nullptr, msg, &businessError);
37     if (status != napi_ok) {
38         HILOGE("Failed to create a BusinessError, error message is %{public}s", errMsg.c_str());
39         return nullptr;
40     }
41     status = napi_set_named_property(env, businessError, FILEIO_TAG_ERR_CODE.c_str(), code);
42     if (status != napi_ok) {
43         HILOGE("Failed to set code property on Error, error message is %{public}s", errMsg.c_str());
44         return nullptr;
45     }
46     return businessError;
47 }
48 
NError()49 NError::NError() {}
50 
NError(int errCode)51 NError::NError(int errCode)
52 {
53     if (errCodeTable.find(errCode) != errCodeTable.end()) {
54         errno_ = errCodeTable.at(errCode).first;
55         errMsg_ = errCodeTable.at(errCode).second;
56     } else {
57         errno_ = errCode;
58         errMsg_ = "Unknown error";
59     }
60 }
61 
NError(std::function<std::tuple<uint32_t,std::string> ()> errGen)62 NError::NError(std::function<std::tuple<uint32_t, std::string>()> errGen)
63 {
64     tie(errno_, errMsg_) = errGen();
65 }
66 
operator bool() const67 NError::operator bool() const
68 {
69     return errno_ != ERRNO_NOERR;
70 }
71 
GetNapiErr(napi_env env)72 napi_value NError::GetNapiErr(napi_env env)
73 {
74     if (errno_ == ERRNO_NOERR) {
75         return nullptr;
76     }
77     return GenerateBusinessError(env, errno_, errMsg_);
78 }
79 
GetNapiErr(napi_env env,int errCode)80 napi_value NError::GetNapiErr(napi_env env, int errCode)
81 {
82     if (errCode == ERRNO_NOERR) {
83         return nullptr;
84     }
85     int32_t code;
86     string msg;
87     if (errCodeTable.find(errCode) != errCodeTable.end()) {
88         code = errCodeTable.at(errCode).first;
89         msg = errCodeTable.at(errCode).second;
90     } else {
91         code = errCodeTable.at(UNKROWN_ERR).first;
92         msg = errCodeTable.at(UNKROWN_ERR).second;
93     }
94     errno_ = code;
95     errMsg_ = msg;
96     return GenerateBusinessError(env, code, msg);
97 }
98 
ThrowErr(napi_env env,int errCode)99 void NError::ThrowErr(napi_env env, int errCode)
100 {
101     int32_t code;
102     string msg;
103     if (errCodeTable.find(errCode) != errCodeTable.end()) {
104         code = errCodeTable.at(errCode).first;
105         msg = errCodeTable.at(errCode).second;
106     } else {
107         code = errCodeTable.at(UNKROWN_ERR).first;
108         msg = errCodeTable.at(UNKROWN_ERR).second;
109     }
110     errno_ = code;
111     errMsg_ = msg;
112     napi_status status = napi_throw(env, GenerateBusinessError(env, code, msg));
113     if (status != napi_ok) {
114         HILOGE("Failed to throw a BusinessError, error message is %{public}s", msg.c_str());
115     }
116 }
117 
ThrowErr(napi_env env,string errMsg)118 void NError::ThrowErr(napi_env env, string errMsg)
119 {
120     napi_value tmp = nullptr;
121     napi_get_and_clear_last_exception(env, &tmp);
122     // Note that ace engine cannot thow errors created by napi_create_error so far
123     napi_status throwStatus = napi_throw_error(env, nullptr, errMsg.c_str());
124     if (throwStatus != napi_ok) {
125         HILOGE("Failed to throw an Error, error message is %{public}s", errMsg.c_str());
126     }
127 }
128 
ThrowErr(napi_env env)129 void NError::ThrowErr(napi_env env)
130 {
131     napi_value tmp = nullptr;
132     napi_get_and_clear_last_exception(env, &tmp);
133     napi_status status = napi_throw(env, GenerateBusinessError(env, errno_, errMsg_));
134     if (status != napi_ok) {
135         HILOGE("Failed to throw a BusinessError, error message is %{public}s", errMsg_.c_str());
136     }
137 }
138 } // namespace LibN
139 } // namespace FileManagement
140 } // namespace OHOS