blob: cc29f0aa7c211516878c60297eeb82453b406f68 [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{
29 public class TApplicationException : Exception
30 {
31 protected ExceptionType type;
32
33 public TApplicationException()
34 {
35 }
36
37 public TApplicationException(ExceptionType type)
38 {
39 this.type = type;
40 }
41
42 public TApplicationException(ExceptionType type, string message)
43 : base(message)
44 {
45 this.type = type;
46 }
47
48 public static TApplicationException Read(TProtocol iprot)
49 {
50 TField field;
David Reiss7f42bcf2008-01-11 20:59:12 +000051
52 string message = null;
53 ExceptionType type = ExceptionType.Unknown;
54
55 while (true)
56 {
57 field = iprot.ReadFieldBegin();
58 if (field.Type == TType.Stop)
59 {
60 break;
61 }
62
63 switch (field.ID)
64 {
65 case 1:
66 if (field.Type == TType.String)
67 {
68 message = iprot.ReadString();
69 }
70 else
71 {
72 TProtocolUtil.Skip(iprot, field.Type);
73 }
74 break;
75 case 2:
76 if (field.Type == TType.I32)
77 {
78 type = (ExceptionType)iprot.ReadI32();
79 }
80 else
81 {
82 TProtocolUtil.Skip(iprot, field.Type);
83 }
84 break;
85 default:
86 TProtocolUtil.Skip(iprot, field.Type);
87 break;
88 }
89
90 iprot.ReadFieldEnd();
91 }
92
93 iprot.ReadStructEnd();
94
95 return new TApplicationException(type, message);
96 }
97
98 public void Write(TProtocol oprot)
99 {
100 TStruct struc = new TStruct("TApplicationException");
101 TField field = new TField();
102
103 oprot.WriteStructBegin(struc);
104
105 if (!String.IsNullOrEmpty(Message))
106 {
107 field.Name = "message";
108 field.Type = TType.String;
109 field.ID = 1;
110 oprot.WriteFieldBegin(field);
111 oprot.WriteString(Message);
112 oprot.WriteFieldEnd();
113 }
114
115 field.Name = "type";
116 field.Type = TType.I32;
117 field.ID = 2;
118 oprot.WriteFieldBegin(field);
119 oprot.WriteI32((int)type);
120 oprot.WriteFieldEnd();
121 oprot.WriteFieldStop();
122 oprot.WriteStructEnd();
123 }
124
125 public enum ExceptionType
126 {
127 Unknown,
128 UnknownMethod,
129 InvalidMessageType,
130 WrongMethodName,
131 BadSequenceID,
132 MissingResult
133 }
134 }
135}