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
43 AsyncSocketException(AsyncSocketExceptionType type,
44 const std::string& message,
47 AsyncSocketException::getMessage(type, message, errno_copy)),
52 AsyncSocketExceptionType type_;
54 /** A copy of the errno. */
57 AsyncSocketExceptionType getType() const noexcept { return type_; }
58 int getErrno() const noexcept { return errno_; }
61 /** Just like strerror_r but returns a C++ string object. */
62 static std::string strerror_s(int errno_copy) {
63 return folly::sformat("errno = {} ({})", errno_copy, strerror(errno_copy));
66 /** get the string of exception type */
67 static folly::StringPiece getExceptionTypeString(
68 AsyncSocketExceptionType type) {
71 return "Unknown async socket exception";
73 return "Socket not open";
75 return "Socket already open";
83 return "Invalid arguments";
85 return "Corrupted Data";
87 return "Internal error";
89 return "Not supported";
91 return "Invalid state";
95 return "Could not bind";
96 case SASL_HANDSHAKE_TIMEOUT:
97 return "SASL handshake timeout";
99 return "(Invalid exception type)";
103 /** Return a message based on the input. */
104 static std::string getMessage(AsyncSocketExceptionType type,
105 const std::string& message,
107 if (errno_copy != 0) {
108 return folly::sformat(
109 "AsyncSocketException: {}, type = {}, errno = {} ({})",
111 AsyncSocketException::getExceptionTypeString(type),
113 strerror(errno_copy));
115 return folly::sformat("AsyncSocketException: {}, type = {}",
117 AsyncSocketException::getExceptionTypeString(type));