2 * Copyright 2017 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include <folly/Format.h>
22 #include <folly/io/async/DelayedDestruction.h>
26 class AsyncSocketException : public std::runtime_error {
28 enum AsyncSocketExceptionType {
42 SASL_HANDSHAKE_TIMEOUT = 14,
44 EARLY_DATA_REJECTED = 16,
47 AsyncSocketException(AsyncSocketExceptionType type,
48 const std::string& message,
51 AsyncSocketException::getMessage(type, message, errno_copy)),
56 AsyncSocketExceptionType type_;
58 /** A copy of the errno. */
61 AsyncSocketExceptionType getType() const noexcept { return type_; }
62 int getErrno() const noexcept { return errno_; }
65 /** Just like strerror_r but returns a C++ string object. */
66 static std::string strerror_s(int errno_copy) {
67 return folly::sformat("errno = {} ({})", errno_copy, strerror(errno_copy));
70 /** get the string of exception type */
71 static folly::StringPiece getExceptionTypeString(
72 AsyncSocketExceptionType type) {
75 return "Unknown async socket exception";
77 return "Socket not open";
79 return "Socket already open";
87 return "Invalid arguments";
89 return "Corrupted Data";
91 return "Internal error";
93 return "Not supported";
95 return "Invalid state";
99 return "Could not bind";
100 case SASL_HANDSHAKE_TIMEOUT:
101 return "SASL handshake timeout";
103 return "Network error";
104 case EARLY_DATA_REJECTED:
105 return "Early data rejected";
107 return "(Invalid exception type)";
111 /** Return a message based on the input. */
112 static std::string getMessage(AsyncSocketExceptionType type,
113 const std::string& message,
115 if (errno_copy != 0) {
116 return folly::sformat(
117 "AsyncSocketException: {}, type = {}, errno = {} ({})",
119 AsyncSocketException::getExceptionTypeString(type),
121 strerror(errno_copy));
123 return folly::sformat("AsyncSocketException: {}, type = {}",
125 AsyncSocketException::getExceptionTypeString(type));