logging: rename the `DEBUG` log level to `DBG`
[folly.git] / folly / IPAddressException.h
1 /*
2  * Copyright 2017-present Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #pragma once
17
18 #include <exception>
19 #include <string>
20 #include <utility>
21
22 #include <folly/CPortability.h>
23 #include <folly/detail/IPAddress.h>
24
25 namespace folly {
26
27 /**
28  * Error codes for non-throwing interface of IPAddress family of functions.
29  */
30 enum class IPAddressFormatError { INVALID_IP, UNSUPPORTED_ADDR_FAMILY };
31
32 /**
33  * Exception for invalid IP addresses.
34  */
35 class FOLLY_EXPORT IPAddressFormatException : public std::exception {
36  public:
37   explicit IPAddressFormatException(std::string msg) noexcept
38       : msg_(std::move(msg)) {}
39   IPAddressFormatException(const IPAddressFormatException&) = default;
40   IPAddressFormatException(IPAddressFormatException&&) = default;
41   IPAddressFormatException& operator=(const IPAddressFormatException&) =
42       default;
43   IPAddressFormatException& operator=(IPAddressFormatException&&) = default;
44
45   ~IPAddressFormatException() noexcept override {}
46   const char* what() const noexcept override {
47     return msg_.c_str();
48   }
49
50  private:
51   std::string msg_;
52 };
53
54 class FOLLY_EXPORT InvalidAddressFamilyException
55     : public IPAddressFormatException {
56  public:
57   explicit InvalidAddressFamilyException(std::string msg) noexcept
58       : IPAddressFormatException(std::move(msg)) {}
59   explicit InvalidAddressFamilyException(sa_family_t family) noexcept
60       : InvalidAddressFamilyException(
61             "Address family " + detail::familyNameStr(family) +
62             " is not AF_INET or AF_INET6") {}
63   InvalidAddressFamilyException(const InvalidAddressFamilyException&) = default;
64   InvalidAddressFamilyException(InvalidAddressFamilyException&&) = default;
65   InvalidAddressFamilyException& operator=(
66       const InvalidAddressFamilyException&) = default;
67   InvalidAddressFamilyException& operator=(InvalidAddressFamilyException&&) =
68       default;
69 };
70
71 } // namespace folly