1 /*
2  * Copyright (c) Facebook, Inc. and its affiliates.
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 
getExceptionTypeString(AsyncSocketExceptionType type)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 NETWORK_ERROR:
54       return "Network error";
55     case EARLY_DATA_REJECTED:
56       return "Early data rejected";
57     case CANCELED:
58       return "IO operation was canceled";
59     default:
60       return "(Invalid exception type)";
61   }
62 }
63 
getMessage(AsyncSocketExceptionType type,const std::string & message,int errnoCopy)64 /* static */ std::string AsyncSocketException::getMessage(
65     AsyncSocketExceptionType type, const std::string& message, int errnoCopy) {
66   if (errnoCopy != 0) {
67     return sformat(
68         "AsyncSocketException: {}, type = {}, errno = {} ({})",
69         message,
70         getExceptionTypeString(type),
71         errnoCopy,
72         errnoStr(errnoCopy));
73   } else {
74     return sformat(
75         "AsyncSocketException: {}, type = {}",
76         message,
77         getExceptionTypeString(type));
78   }
79 }
80 
81 } // namespace folly
82