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_DEVICE_EXTRADATA_UN_INIT,
36      ERR_CONNECT_LINK_TYPE
37  };
38  
39  class DfsuException : public std::exception {
40  public:
DfsuException(int code,const std::string & msg)41      DfsuException(int code, const std::string &msg) : code_(code), msg_(msg) {};
42  
code()43      uint32_t code() const noexcept
44      {
45          return code_;
46      }
47  
what()48      virtual const char *what() const noexcept
49      {
50          return msg_.c_str();
51      }
52  
53  private:
54      int code_ {ERR_DEFAULT};
55      std::string msg_;
56  };
57  
58  #define ThrowException(code, msg)                           \
59      do {                                                    \
60          std::stringstream __ss;                             \
61          __ss << '[' << (code) << ']' << (msg) << std::endl; \
62          LOGE("%{public}s", __ss.str().c_str());             \
63          throw DfsuException((code), __ss.str());            \
64      } while (0)
65  } // namespace DistributedFile
66  } // namespace Storage
67  } // namespace OHOS
68  #endif // DFSU_EXCEPTION_H