2 * Copyright 2016 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.
19 #include <folly/Format.h>
20 #include <folly/io/async/DelayedDestruction.h>
24 class AsyncSocketException : public std::runtime_error {
26 enum AsyncSocketExceptionType {
40 SASL_HANDSHAKE_TIMEOUT = 14,
44 AsyncSocketException(AsyncSocketExceptionType type,
45 const std::string& message,
48 AsyncSocketException::getMessage(type, message, errno_copy)),
53 AsyncSocketExceptionType type_;
55 /** A copy of the errno. */
58 AsyncSocketExceptionType getType() const noexcept { return type_; }
59 int getErrno() const noexcept { return errno_; }
62 /** Just like strerror_r but returns a C++ string object. */
63 static std::string strerror_s(int errno_copy) {
64 return folly::sformat("errno = {} ({})", errno_copy, strerror(errno_copy));
67 /** get the string of exception type */
68 static folly::StringPiece getExceptionTypeString(
69 AsyncSocketExceptionType type) {
72 return "Unknown async socket exception";
74 return "Socket not open";
76 return "Socket already open";
84 return "Invalid arguments";
86 return "Corrupted Data";
88 return "Internal error";
90 return "Not supported";
92 return "Invalid state";
96 return "Could not bind";
97 case SASL_HANDSHAKE_TIMEOUT:
98 return "SASL handshake timeout";
100 return "Network error";
102 return "(Invalid exception type)";
106 /** Return a message based on the input. */
107 static std::string getMessage(AsyncSocketExceptionType type,
108 const std::string& message,
110 if (errno_copy != 0) {
111 return folly::sformat(
112 "AsyncSocketException: {}, type = {}, errno = {} ({})",
114 AsyncSocketException::getExceptionTypeString(type),
116 strerror(errno_copy));
118 return folly::sformat("AsyncSocketException: {}, type = {}",
120 AsyncSocketException::getExceptionTypeString(type));