Add a default timeout parameter to HHWheelTimer.
[folly.git] / folly / io / async / AsyncSocketException.h
1 /*
2  * Copyright 2015 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 <folly/Format.h>
20 #include <folly/io/async/DelayedDestruction.h>
21
22 namespace folly {
23
24 class AsyncSocketException : public std::runtime_error {
25  public:
26   enum AsyncSocketExceptionType
27   { UNKNOWN = 0
28   , NOT_OPEN = 1
29   , ALREADY_OPEN = 2
30   , TIMED_OUT = 3
31   , END_OF_FILE = 4
32   , INTERRUPTED = 5
33   , BAD_ARGS = 6
34   , CORRUPTED_DATA = 7
35   , INTERNAL_ERROR = 8
36   , NOT_SUPPORTED = 9
37   , INVALID_STATE = 10
38   , SSL_ERROR = 12
39   , COULD_NOT_BIND = 13
40   , SASL_HANDSHAKE_TIMEOUT = 14
41   };
42
43   AsyncSocketException(
44     AsyncSocketExceptionType type, const std::string& message) :
45       std::runtime_error(message),
46       type_(type), errno_(0) {}
47
48   /** Error code */
49   AsyncSocketExceptionType type_;
50
51   /** A copy of the errno. */
52   int errno_;
53
54   AsyncSocketException(AsyncSocketExceptionType type,
55                       const std::string& message,
56                       int errno_copy) :
57       std::runtime_error(getMessage(message, errno_copy)),
58       type_(type), errno_(errno_copy) {}
59
60   AsyncSocketExceptionType getType() const noexcept { return type_; }
61   int getErrno() const noexcept { return errno_; }
62
63  protected:
64   /** Just like strerror_r but returns a C++ string object. */
65   std::string strerror_s(int errno_copy) {
66     return "errno = " + folly::to<std::string>(errno_copy);
67   }
68
69   /** Return a message based on the input. */
70   std::string getMessage(const std::string &message,
71                                 int errno_copy) {
72     if (errno_copy != 0) {
73       return message + ": " + strerror_s(errno_copy);
74     } else {
75       return message;
76     }
77   }
78 };
79
80 } // folly