• 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 #include "uni_error.h"
17 
18 #include <cerrno>
19 #include <string>
20 
21 #include "js_native_api.h"
22 #include "log.h"
23 #include "napi/n_val.h"
24 
25 namespace OHOS {
26 namespace DistributedFS {
27 using namespace std;
28 
UniError()29 UniError::UniError() {}
30 
UniError(ELegacy eLegacy)31 UniError::UniError(ELegacy eLegacy) : errno_(eLegacy), codingSystem_(ERR_CODE_SYSTEM_LEGACY) {}
32 
UniError(int ePosix)33 UniError::UniError(int ePosix) : errno_(ePosix), codingSystem_(ERR_CODE_SYSTEM_POSIX) {}
34 
operator bool() const35 UniError::operator bool() const
36 {
37     return errno_ != ERRNO_NOERR;
38 }
39 
GetErrno(ErrCodeSystem cs)40 int UniError::GetErrno(ErrCodeSystem cs)
41 {
42     if (errno_ == ERRNO_NOERR) {
43         return ERRNO_NOERR;
44     }
45     if (cs == codingSystem_) {
46         return errno_;
47     }
48 
49     if (cs == ERR_CODE_SYSTEM_POSIX) {
50         // Note that we should support more codes here
51         return EINVAL;
52     }
53 
54     // Note that this shall be done properly
55     return ELEGACY_INVAL;
56 }
57 
SetErrno(ELegacy eLegacy)58 void UniError::SetErrno(ELegacy eLegacy)
59 {
60     errno_ = eLegacy;
61     codingSystem_ = ERR_CODE_SYSTEM_LEGACY;
62 }
63 
SetErrno(int ePosix)64 void UniError::SetErrno(int ePosix)
65 {
66     errno_ = ePosix;
67     codingSystem_ = ERR_CODE_SYSTEM_POSIX;
68 }
69 
GetDefaultErrstr()70 std::string UniError::GetDefaultErrstr()
71 {
72     if (codingSystem_ != ERR_CODE_SYSTEM_POSIX && codingSystem_ != ERR_CODE_SYSTEM_LEGACY) {
73         return "BUG: Curious coding system";
74     }
75     return strerror(GetErrno(ERR_CODE_SYSTEM_POSIX));
76 }
77 
GetNapiErr(napi_env env)78 napi_value UniError::GetNapiErr(napi_env env)
79 {
80     return GetNapiErr(env, GetDefaultErrstr());
81 }
82 
GetNapiErr(napi_env env,string errMsg)83 napi_value UniError::GetNapiErr(napi_env env, string errMsg)
84 {
85     int errCode = GetErrno(codingSystem_);
86     if (errCode == ERRNO_NOERR) {
87         return nullptr;
88     }
89     napi_value code = NVal::CreateUTF8String(env, to_string(errCode)).val_;
90     napi_value msg = NVal::CreateUTF8String(env, errMsg).val_;
91 
92     napi_value res = nullptr;
93     napi_status createRes = napi_create_error(env, code, msg, &res);
94     if (createRes) {
95         HILOGE("Failed to create an exception, msg = %{public}s", errMsg.c_str());
96     }
97     return res;
98 }
99 
ThrowErr(napi_env env)100 void UniError::ThrowErr(napi_env env)
101 {
102     string msg = GetDefaultErrstr();
103     napi_value tmp = nullptr;
104     napi_get_and_clear_last_exception(env, &tmp);
105     // Note that ace engine cannot throw errors created by napi_create_error so far
106     napi_status throwStatus = napi_throw_error(env, nullptr, msg.c_str());
107     if (throwStatus != napi_ok) {
108         HILOGE("Failed to throw an exception, %{public}d, code = %{public}s", throwStatus, msg.c_str());
109     }
110 }
111 
ThrowErr(napi_env env,string errMsg)112 void UniError::ThrowErr(napi_env env, string errMsg)
113 {
114     napi_value tmp = nullptr;
115     napi_get_and_clear_last_exception(env, &tmp);
116     // Note that ace engine cannot throw errors created by napi_create_error so far
117     napi_status throwStatus = napi_throw_error(env, nullptr, errMsg.c_str());
118     if (throwStatus != napi_ok) {
119         HILOGE("Failed to throw an exception, %{public}d, code = %{public}s", throwStatus, errMsg.c_str());
120     }
121 }
122 } // namespace DistributedFS
123 } // namespace OHOS