give all folly exception types default visibility
[folly.git] / folly / io / async / AsyncSocketException.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 <stdexcept>
19 #include <string>
20
21 #include <folly/CPortability.h>
22 #include <folly/Range.h>
23
24 namespace folly {
25
26 class FOLLY_EXPORT AsyncSocketException : public std::runtime_error {
27  public:
28   enum AsyncSocketExceptionType {
29     UNKNOWN = 0,
30     NOT_OPEN = 1,
31     ALREADY_OPEN = 2,
32     TIMED_OUT = 3,
33     END_OF_FILE = 4,
34     INTERRUPTED = 5,
35     BAD_ARGS = 6,
36     CORRUPTED_DATA = 7,
37     INTERNAL_ERROR = 8,
38     NOT_SUPPORTED = 9,
39     INVALID_STATE = 10,
40     SSL_ERROR = 12,
41     COULD_NOT_BIND = 13,
42     SASL_HANDSHAKE_TIMEOUT = 14,
43     NETWORK_ERROR = 15,
44     EARLY_DATA_REJECTED = 16,
45   };
46
47   AsyncSocketException(
48       AsyncSocketExceptionType type,
49       const std::string& message,
50       int errnoCopy = 0)
51       : std::runtime_error(getMessage(type, message, errnoCopy)),
52         type_(type),
53         errno_(errnoCopy) {}
54
55   /** Error code */
56   AsyncSocketExceptionType type_;
57
58   /** A copy of the errno. */
59   int errno_;
60
61   AsyncSocketExceptionType getType() const noexcept {
62     return type_;
63   }
64
65   int getErrno() const noexcept {
66     return errno_;
67   }
68
69  protected:
70   /** get the string of exception type */
71   static folly::StringPiece getExceptionTypeString(
72       AsyncSocketExceptionType type);
73
74   /** Return a message based on the input. */
75   static std::string getMessage(
76       AsyncSocketExceptionType type,
77       const std::string& message,
78       int errnoCopy);
79 };
80
81 } // namespace folly