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