• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 #ifndef DFSU_EXCEPTION_H
17 #define DFSU_EXCEPTION_H
18 
19 #include <exception>
20 #include <sstream>
21 #include <string>
22 
23 #include "utils_log.h"
24 
25 namespace OHOS {
26 namespace Storage {
27 namespace DistributedFile {
28 enum {
29     ERR_DEFAULT,
30     ERR_UTILS_ACTOR_QUEUE_STOP,
31     ERR_UTILS_ACTOR_INVALID_CMD,
32     ERR_NETWORK_AGENT_TEMPLATE_OPEN_SESSION_FAIL,
33     ERR_SOFTBUS_AGENT_ON_SESSION_OPENED_FAIL,
34     ERR_DEVICE_CID_UN_INIT,
35     ERR_CONNECT_LINK_TYPE
36 };
37 
38 class DfsuException : public std::exception {
39 public:
DfsuException(int code,const std::string & msg)40     DfsuException(int code, const std::string &msg) : code_(code), msg_(msg) {};
41 
code()42     uint32_t code() const noexcept
43     {
44         return code_;
45     }
46 
what()47     virtual const char *what() const noexcept
48     {
49         return msg_.c_str();
50     }
51 
52 private:
53     int code_ {ERR_DEFAULT};
54     std::string msg_;
55 };
56 
57 #define THROW_EXCEPTION(code, msg)                           \
58     do {                                                    \
59         std::stringstream __ss;                             \
60         __ss << '[' << (code) << ']' << (msg) << std::endl; \
61         LOGE("%{public}s", __ss.str().c_str());             \
62         throw DfsuException((code), __ss.str());            \
63     } while (0)
64 } // namespace DistributedFile
65 } // namespace Storage
66 } // namespace OHOS
67 #endif // DFSU_EXCEPTION_H