]>
Commit | Line | Data |
---|---|---|
f67539c2 TL |
1 | /* |
2 | * Licensed to the Apache Software Foundation (ASF) under one | |
3 | * or more contributor license agreements. See the NOTICE file | |
4 | * distributed with this work for additional information | |
5 | * regarding copyright ownership. The ASF licenses this file | |
6 | * to you under the Apache License, Version 2.0 (the | |
7 | * "License"); you may not use this file except in compliance | |
8 | * with the License. You may obtain a copy of the License at | |
9 | * | |
10 | * http://www.apache.org/licenses/LICENSE-2.0 | |
11 | * | |
12 | * Unless required by applicable law or agreed to in writing, | |
13 | * software distributed under the License is distributed on an | |
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
15 | * KIND, either express or implied. See the License for the | |
16 | * specific language governing permissions and limitations | |
17 | * under the License. | |
18 | */ | |
19 | ||
20 | #ifndef _THRIFT_TRANSPORT_TTRANSPORTEXCEPTION_H_ | |
21 | #define _THRIFT_TRANSPORT_TTRANSPORTEXCEPTION_H_ 1 | |
22 | ||
23 | #include <boost/numeric/conversion/cast.hpp> | |
24 | #include <string> | |
25 | #include <thrift/Thrift.h> | |
26 | ||
27 | namespace apache { | |
28 | namespace thrift { | |
29 | namespace transport { | |
30 | ||
31 | /** | |
32 | * Class to encapsulate all the possible types of transport errors that may | |
33 | * occur in various transport systems. This provides a sort of generic | |
34 | * wrapper around the vague UNIX E_ error codes that lets a common code | |
35 | * base of error handling to be used for various types of transports, i.e. | |
36 | * pipes etc. | |
37 | * | |
38 | */ | |
39 | class TTransportException : public apache::thrift::TException { | |
40 | public: | |
41 | /** | |
42 | * Error codes for the various types of exceptions. | |
43 | */ | |
44 | enum TTransportExceptionType { | |
45 | UNKNOWN = 0, | |
46 | NOT_OPEN = 1, | |
47 | TIMED_OUT = 2, | |
48 | END_OF_FILE = 3, | |
49 | INTERRUPTED = 4, | |
50 | BAD_ARGS = 5, | |
51 | CORRUPTED_DATA = 6, | |
52 | INTERNAL_ERROR = 7 | |
53 | }; | |
54 | ||
55 | TTransportException() : apache::thrift::TException(), type_(UNKNOWN) {} | |
56 | ||
57 | TTransportException(TTransportExceptionType type) : apache::thrift::TException(), type_(type) {} | |
58 | ||
59 | TTransportException(const std::string& message) | |
60 | : apache::thrift::TException(message), type_(UNKNOWN) {} | |
61 | ||
62 | TTransportException(TTransportExceptionType type, const std::string& message) | |
63 | : apache::thrift::TException(message), type_(type) {} | |
64 | ||
65 | TTransportException(TTransportExceptionType type, const std::string& message, int errno_copy) | |
66 | : apache::thrift::TException(message + ": " + TOutput::strerror_s(errno_copy)), type_(type) {} | |
67 | ||
68 | ~TTransportException() noexcept override = default; | |
69 | ||
70 | /** | |
71 | * Returns an error code that provides information about the type of error | |
72 | * that has occurred. | |
73 | * | |
74 | * @return Error code | |
75 | */ | |
76 | TTransportExceptionType getType() const noexcept { return type_; } | |
77 | ||
78 | const char* what() const noexcept override; | |
79 | ||
80 | protected: | |
81 | /** Just like strerror_r but returns a C++ string object. */ | |
82 | std::string strerror_s(int errno_copy); | |
83 | ||
84 | /** Error code */ | |
85 | TTransportExceptionType type_; | |
86 | }; | |
87 | ||
88 | /** | |
89 | * Legacy code in transport implementations have overflow issues | |
90 | * that need to be enforced. | |
91 | */ | |
92 | template <typename To, typename From> To safe_numeric_cast(From i) { | |
93 | try { | |
94 | return boost::numeric_cast<To>(i); | |
95 | } | |
96 | catch (const std::bad_cast& bc) { | |
97 | throw TTransportException(TTransportException::CORRUPTED_DATA, | |
98 | bc.what()); | |
99 | } | |
100 | } | |
101 | ||
102 | } | |
103 | } | |
104 | } // apache::thrift::transport | |
105 | ||
106 | #endif // #ifndef _THRIFT_TRANSPORT_TTRANSPORTEXCEPTION_H_ |