4b15855a0872b9bba2d5720746ad3e0727454967
[folly.git] / folly / IPAddressException.h
1 /*
2  * Copyright 2014 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
17 #pragma once
18
19 #include <exception>
20 #include <string>
21
22 #include <folly/Conv.h>
23 #include <folly/detail/IPAddress.h>
24
25 namespace folly {
26
27 /**
28  * Exception for invalid IP addresses.
29  */
30 class IPAddressFormatException : public std::exception {
31  public:
32   explicit IPAddressFormatException(const std::string& msg)
33       : msg_(msg) {}
34   IPAddressFormatException(
35     const IPAddressFormatException& exception_) = default;
36   template<typename... Args>
37   explicit IPAddressFormatException(Args&&... args)
38       : msg_(to<std::string>(std::forward<Args>(args)...)) {}
39
40   virtual ~IPAddressFormatException() noexcept {}
41   virtual const char *what(void) const noexcept {
42     return msg_.c_str();
43   }
44
45  private:
46   const std::string msg_;
47 };
48
49 class InvalidAddressFamilyException : public IPAddressFormatException {
50  public:
51   explicit InvalidAddressFamilyException(const std::string& msg)
52       : IPAddressFormatException(msg) {}
53   InvalidAddressFamilyException(
54     const InvalidAddressFamilyException& ex) = default;
55   explicit InvalidAddressFamilyException(sa_family_t family)
56       : IPAddressFormatException("Address family " +
57                                  detail::familyNameStr(family) +
58                                  " is not AF_INET or AF_INET6") {}
59   template<typename... Args>
60   explicit InvalidAddressFamilyException(Args&&... args)
61       : IPAddressFormatException(std::forward<Args>(args)...) {}
62 };
63
64 }  // folly