Add unit test for timeout=0
[folly.git] / folly / io / async / AsyncSocketException.cpp
1 /*
2  * Copyright 2017 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
17 #include "folly/io/async/AsyncSocketException.h"
18
19 #include <folly/Format.h>
20 #include <folly/String.h>
21
22 namespace folly {
23
24 /* static */ StringPiece AsyncSocketException::getExceptionTypeString(
25     AsyncSocketExceptionType type) {
26   switch (type) {
27     case UNKNOWN:
28       return "Unknown async socket exception";
29     case NOT_OPEN:
30       return "Socket not open";
31     case ALREADY_OPEN:
32       return "Socket already open";
33     case TIMED_OUT:
34       return "Timed out";
35     case END_OF_FILE:
36       return "End of file";
37     case INTERRUPTED:
38       return "Interrupted";
39     case BAD_ARGS:
40       return "Invalid arguments";
41     case CORRUPTED_DATA:
42       return "Corrupted Data";
43     case INTERNAL_ERROR:
44       return "Internal error";
45     case NOT_SUPPORTED:
46       return "Not supported";
47     case INVALID_STATE:
48       return "Invalid state";
49     case SSL_ERROR:
50       return "SSL error";
51     case COULD_NOT_BIND:
52       return "Could not bind";
53     case SASL_HANDSHAKE_TIMEOUT:
54       return "SASL handshake timeout";
55     case NETWORK_ERROR:
56       return "Network error";
57     case EARLY_DATA_REJECTED:
58       return "Early data rejected";
59     default:
60       return "(Invalid exception type)";
61   }
62 }
63
64 /* static */ std::string AsyncSocketException::getMessage(
65     AsyncSocketExceptionType type,
66     const std::string& message,
67     int errnoCopy) {
68   if (errnoCopy != 0) {
69     return sformat(
70         "AsyncSocketException: {}, type = {}, errno = {} ({})",
71         message,
72         getExceptionTypeString(type),
73         errnoCopy,
74         errnoStr(errnoCopy));
75   } else {
76     return sformat(
77         "AsyncSocketException: {}, type = {}",
78         message,
79         getExceptionTypeString(type));
80   }
81 }
82
83 } // namespace folly