David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 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 | */ |
Mark Slee | 9f0c651 | 2007-02-28 23:58:26 +0000 | [diff] [blame] | 19 | |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 20 | #ifndef _THRIFT_PROTOCOL_TPROTOCOLEXCEPTION_H_ |
| 21 | #define _THRIFT_PROTOCOL_TPROTOCOLEXCEPTION_H_ 1 |
| 22 | |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 23 | #include <string> |
| 24 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 25 | namespace apache { |
| 26 | namespace thrift { |
| 27 | namespace protocol { |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 28 | |
| 29 | /** |
| 30 | * Class to encapsulate all the possible types of protocol errors that may |
| 31 | * occur in various protocol systems. This provides a sort of generic |
David Reiss | 3bb5e05 | 2010-01-25 19:31:31 +0000 | [diff] [blame] | 32 | * wrapper around the vague UNIX E_ error codes that lets a common code |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 33 | * base of error handling to be used for various types of protocols, i.e. |
| 34 | * pipes etc. |
| 35 | * |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 36 | */ |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 37 | class TProtocolException : public apache::thrift::TException { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 38 | public: |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 39 | /** |
| 40 | * Error codes for the various types of exceptions. |
| 41 | */ |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 42 | enum TProtocolExceptionType { |
| 43 | UNKNOWN = 0, |
| 44 | INVALID_DATA = 1, |
| 45 | NEGATIVE_SIZE = 2, |
| 46 | SIZE_LIMIT = 3, |
| 47 | BAD_VERSION = 4, |
| 48 | NOT_IMPLEMENTED = 5, |
| 49 | DEPTH_LIMIT = 6 |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 50 | }; |
| 51 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 52 | TProtocolException() : apache::thrift::TException(), type_(UNKNOWN) {} |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 53 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 54 | TProtocolException(TProtocolExceptionType type) : apache::thrift::TException(), type_(type) {} |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 55 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 56 | TProtocolException(const std::string& message) |
| 57 | : apache::thrift::TException(message), type_(UNKNOWN) {} |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 58 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 59 | TProtocolException(TProtocolExceptionType type, const std::string& message) |
| 60 | : apache::thrift::TException(message), type_(type) {} |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 61 | |
cyy | 7b93559 | 2019-01-05 10:04:25 +0800 | [diff] [blame] | 62 | virtual ~TProtocolException() noexcept {} |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 63 | |
| 64 | /** |
| 65 | * Returns an error code that provides information about the type of error |
| 66 | * that has occurred. |
| 67 | * |
| 68 | * @return Error code |
| 69 | */ |
tpcwang | d42d8be | 2016-03-24 09:56:10 -0700 | [diff] [blame] | 70 | TProtocolExceptionType getType() const { return type_; } |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 71 | |
cyy | 7b93559 | 2019-01-05 10:04:25 +0800 | [diff] [blame] | 72 | virtual const char* what() const noexcept { |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 73 | if (message_.empty()) { |
David Reiss | adad4ab | 2007-12-14 20:56:04 +0000 | [diff] [blame] | 74 | switch (type_) { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 75 | case UNKNOWN: |
| 76 | return "TProtocolException: Unknown protocol exception"; |
| 77 | case INVALID_DATA: |
| 78 | return "TProtocolException: Invalid data"; |
| 79 | case NEGATIVE_SIZE: |
| 80 | return "TProtocolException: Negative size"; |
| 81 | case SIZE_LIMIT: |
| 82 | return "TProtocolException: Exceeded size limit"; |
| 83 | case BAD_VERSION: |
| 84 | return "TProtocolException: Invalid version"; |
| 85 | case NOT_IMPLEMENTED: |
| 86 | return "TProtocolException: Not implemented"; |
| 87 | default: |
| 88 | return "TProtocolException: (Invalid exception type)"; |
David Reiss | adad4ab | 2007-12-14 20:56:04 +0000 | [diff] [blame] | 89 | } |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 90 | } else { |
| 91 | return message_.c_str(); |
| 92 | } |
| 93 | } |
| 94 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 95 | protected: |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 96 | /** |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 97 | * Error code |
| 98 | */ |
| 99 | TProtocolExceptionType type_; |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 100 | }; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 101 | } |
| 102 | } |
| 103 | } // apache::thrift::protocol |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 104 | |
| 105 | #endif // #ifndef _THRIFT_PROTOCOL_TPROTOCOLEXCEPTION_H_ |