blob: 4c0d3a32e8b00d59c6e3e28186d214264ef2a68f [file] [log] [blame]
Kevin Clarkab4460d2009-03-20 02:28:41 +00001/**
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 Lipcon53ae9f32009-12-07 00:42:38 +000018 *
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 Clarkab4460d2009-03-20 02:28:41 +000022 */
David Reiss7f42bcf2008-01-11 20:59:12 +000023
24using System;
David Reiss7f42bcf2008-01-11 20:59:12 +000025using Thrift.Protocol;
26
27namespace Thrift
28{
Jens Geyerd5436f52014-10-03 19:50:38 +020029 public class TApplicationException : TException
30 {
31 protected ExceptionType type;
David Reiss7f42bcf2008-01-11 20:59:12 +000032
Jens Geyerd5436f52014-10-03 19:50:38 +020033 public TApplicationException()
34 {
35 }
David Reiss7f42bcf2008-01-11 20:59:12 +000036
Jens Geyerd5436f52014-10-03 19:50:38 +020037 public TApplicationException(ExceptionType type)
38 {
39 this.type = type;
40 }
David Reiss7f42bcf2008-01-11 20:59:12 +000041
Jens Geyerd5436f52014-10-03 19:50:38 +020042 public TApplicationException(ExceptionType type, string message)
43 : base(message)
44 {
45 this.type = type;
46 }
David Reiss7f42bcf2008-01-11 20:59:12 +000047
Jens Geyerd5436f52014-10-03 19:50:38 +020048 public static TApplicationException Read(TProtocol iprot)
49 {
50 TField field;
David Reiss7f42bcf2008-01-11 20:59:12 +000051
Jens Geyerd5436f52014-10-03 19:50:38 +020052 string message = null;
53 ExceptionType type = ExceptionType.Unknown;
David Reiss7f42bcf2008-01-11 20:59:12 +000054
Jens Geyerd5436f52014-10-03 19:50:38 +020055 iprot.ReadStructBegin();
56 while (true)
57 {
58 field = iprot.ReadFieldBegin();
59 if (field.Type == TType.Stop)
60 {
61 break;
62 }
David Reiss7f42bcf2008-01-11 20:59:12 +000063
Jens Geyerd5436f52014-10-03 19:50:38 +020064 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 Reiss7f42bcf2008-01-11 20:59:12 +000090
Jens Geyerd5436f52014-10-03 19:50:38 +020091 iprot.ReadFieldEnd();
92 }
David Reiss7f42bcf2008-01-11 20:59:12 +000093
Jens Geyerd5436f52014-10-03 19:50:38 +020094 iprot.ReadStructEnd();
David Reiss7f42bcf2008-01-11 20:59:12 +000095
Jens Geyerd5436f52014-10-03 19:50:38 +020096 return new TApplicationException(type, message);
97 }
David Reiss7f42bcf2008-01-11 20:59:12 +000098
Jens Geyerd5436f52014-10-03 19:50:38 +020099 public void Write(TProtocol oprot)
100 {
101 TStruct struc = new TStruct("TApplicationException");
102 TField field = new TField();
David Reiss7f42bcf2008-01-11 20:59:12 +0000103
Jens Geyerd5436f52014-10-03 19:50:38 +0200104 oprot.WriteStructBegin(struc);
David Reiss7f42bcf2008-01-11 20:59:12 +0000105
Jens Geyerd5436f52014-10-03 19:50:38 +0200106 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 Reiss7f42bcf2008-01-11 20:59:12 +0000115
Jens Geyerd5436f52014-10-03 19:50:38 +0200116 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 Reiss7f42bcf2008-01-11 20:59:12 +0000125
Jens Geyerd5436f52014-10-03 19:50:38 +0200126 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 Reiss7f42bcf2008-01-11 20:59:12 +0000141}