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