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,
46 AsyncSocketException(AsyncSocketExceptionType type,
47 const std::string& message,
50 AsyncSocketException::getMessage(type, message, errno_copy)),
55 AsyncSocketExceptionType type_;
57 /** A copy of the errno. */
60 AsyncSocketExceptionType getType() const noexcept { return type_; }
61 int getErrno() const noexcept { return errno_; }
64 /** Just like strerror_r but returns a C++ string object. */
65 static std::string strerror_s(int errno_copy) {
66 return folly::sformat("errno = {} ({})", errno_copy, strerror(errno_copy));
69 /** get the string of exception type */
70 static folly::StringPiece getExceptionTypeString(
71 AsyncSocketExceptionType type) {
74 return "Unknown async socket exception";
76 return "Socket not open";
78 return "Socket already open";
86 return "Invalid arguments";
88 return "Corrupted Data";
90 return "Internal error";
92 return "Not supported";
94 return "Invalid state";
98 return "Could not bind";
99 case SASL_HANDSHAKE_TIMEOUT:
100 return "SASL handshake timeout";
102 return "Network error";
104 return "(Invalid exception type)";
108 /** Return a message based on the input. */
109 static std::string getMessage(AsyncSocketExceptionType type,
110 const std::string& message,
112 if (errno_copy != 0) {
113 return folly::sformat(
114 "AsyncSocketException: {}, type = {}, errno = {} ({})",
116 AsyncSocketException::getExceptionTypeString(type),
118 strerror(errno_copy));
120 return folly::sformat("AsyncSocketException: {}, type = {}",
122 AsyncSocketException::getExceptionTypeString(type));