Kevin Clark | ab4460d | 2009-03-20 02:28:41 +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. |
Todd Lipcon | 53ae9f3 | 2009-12-07 00:42:38 +0000 | [diff] [blame] | 18 | * |
| 19 | * Contains some contributions under the Thrift Software License. |
| 20 | * Please see doc/old-thrift-license.txt in the Thrift distribution for |
| 21 | * details. |
Kevin Clark | ab4460d | 2009-03-20 02:28:41 +0000 | [diff] [blame] | 22 | */ |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 23 | |
| 24 | using System; |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 25 | using Thrift.Protocol; |
| 26 | |
| 27 | namespace Thrift |
| 28 | { |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 29 | public class TApplicationException : TException |
| 30 | { |
| 31 | protected ExceptionType type; |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 32 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 33 | public TApplicationException() |
| 34 | { |
| 35 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 36 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 37 | public TApplicationException(ExceptionType type) |
| 38 | { |
| 39 | this.type = type; |
| 40 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 41 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 42 | public TApplicationException(ExceptionType type, string message) |
| 43 | : base(message) |
| 44 | { |
| 45 | this.type = type; |
| 46 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 47 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 48 | public static TApplicationException Read(TProtocol iprot) |
| 49 | { |
| 50 | TField field; |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 51 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 52 | string message = null; |
| 53 | ExceptionType type = ExceptionType.Unknown; |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 54 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 55 | iprot.ReadStructBegin(); |
| 56 | while (true) |
| 57 | { |
| 58 | field = iprot.ReadFieldBegin(); |
| 59 | if (field.Type == TType.Stop) |
| 60 | { |
| 61 | break; |
| 62 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 63 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 64 | switch (field.ID) |
| 65 | { |
| 66 | case 1: |
| 67 | if (field.Type == TType.String) |
| 68 | { |
| 69 | message = iprot.ReadString(); |
| 70 | } |
| 71 | else |
| 72 | { |
| 73 | TProtocolUtil.Skip(iprot, field.Type); |
| 74 | } |
| 75 | break; |
| 76 | case 2: |
| 77 | if (field.Type == TType.I32) |
| 78 | { |
| 79 | type = (ExceptionType)iprot.ReadI32(); |
| 80 | } |
| 81 | else |
| 82 | { |
| 83 | TProtocolUtil.Skip(iprot, field.Type); |
| 84 | } |
| 85 | break; |
| 86 | default: |
| 87 | TProtocolUtil.Skip(iprot, field.Type); |
| 88 | break; |
| 89 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 90 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 91 | iprot.ReadFieldEnd(); |
| 92 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 93 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 94 | iprot.ReadStructEnd(); |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 95 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 96 | return new TApplicationException(type, message); |
| 97 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 98 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 99 | public void Write(TProtocol oprot) |
| 100 | { |
| 101 | TStruct struc = new TStruct("TApplicationException"); |
| 102 | TField field = new TField(); |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 103 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 104 | oprot.WriteStructBegin(struc); |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 105 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 106 | if (!String.IsNullOrEmpty(Message)) |
| 107 | { |
| 108 | field.Name = "message"; |
| 109 | field.Type = TType.String; |
| 110 | field.ID = 1; |
| 111 | oprot.WriteFieldBegin(field); |
| 112 | oprot.WriteString(Message); |
| 113 | oprot.WriteFieldEnd(); |
| 114 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 115 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 116 | field.Name = "type"; |
| 117 | field.Type = TType.I32; |
| 118 | field.ID = 2; |
| 119 | oprot.WriteFieldBegin(field); |
| 120 | oprot.WriteI32((int)type); |
| 121 | oprot.WriteFieldEnd(); |
| 122 | oprot.WriteFieldStop(); |
| 123 | oprot.WriteStructEnd(); |
| 124 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 125 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 126 | public enum ExceptionType |
| 127 | { |
| 128 | Unknown, |
| 129 | UnknownMethod, |
| 130 | InvalidMessageType, |
| 131 | WrongMethodName, |
| 132 | BadSequenceID, |
| 133 | MissingResult, |
| 134 | InternalError, |
| 135 | ProtocolError, |
| 136 | InvalidTransform, |
| 137 | InvalidProtocol, |
| 138 | UnsupportedClientType |
| 139 | } |
| 140 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 141 | } |