1 /*
2  * Copyright (C) 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 #ifndef OHOS_NAPI_ERROR_H
17 #define OHOS_NAPI_ERROR_H
18 
19 #include<string>
20 #include<map>
21 
22 #include "napi/native_node_api.h"
23 
24 namespace OHOS {
25 enum errorDesc {
26     CHECK_PARAM_ERROR,
27     OS_MMAP_ERROR,
28     OS_IOCTL_ERROR,
29     WRITE_TO_ASHMEM_ERROR,
30     READ_FROM_ASHMEM_ERROR,
31     ONLY_PROXY_OBJECT_PERMITTED_ERROR,
32     ONLY_REMOTE_OBJECT_PERMITTED_ERROR,
33     COMMUNICATION_ERROR,
34     PROXY_OR_REMOTE_OBJECT_INVALID_ERROR,
35     WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR,
36     READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR,
37     PARCEL_MEMORY_ALLOC_ERROR,
38     CALL_JS_METHOD_ERROR,
39     OS_DUP_ERROR
40 };
41 
42 typedef struct errorInfo {
43     int errorCode;
44     std::string errorMsg;
45 } errorInfo;
46 
47 class NapiError {
48 public:
NapiError()49     NapiError() {};
NapiError(int32_t errorCode)50     NapiError(int32_t errorCode) : errorCode_(errorCode) {};
51     napi_value GetError(napi_env& env) const;
52     napi_value ThrowError(napi_env& env, int32_t code = -1);
Error(int32_t errorCode)53     inline void Error(int32_t errorCode)
54     {
55         errorCode_ = (errorCode != -1) ? errorCode : errorCode_;
56     };
IsError()57     inline bool IsError() const
58     {
59         return errorCode_ != -1;
60     };
61 
62     static napi_value NAPIRpcErrorEnumExport(napi_env env, napi_value exports);
63 private:
64     int32_t errorCode_{-1};
65     static std::map<int32_t, errorInfo> napiErrMap_;
66 }; // NapiError
67 } // namespace OHOS
68 #endif // OHOS_NAPI_ERROR_H
69