blob: cb3353b134969eef21fa594dd125f466b9c72d27 [file] [log] [blame]
David Reiss7f42bcf2008-01-11 20:59:12 +00001//
2// TApplicationException.cs
3//
4// Begin: Aug 19, 2007
David Reiss0c90f6f2008-02-06 22:18:40 +00005// Authors:
David Reiss7f42bcf2008-01-11 20:59:12 +00006// Todd Berman <tberman@imeem.com>
7//
8// Distributed under the Thrift Software License
9//
10// See accompanying file LICENSE or visit the Thrift site at:
11// http://developers.facebook.com/thrift/using
12
13using System;
David Reiss7f42bcf2008-01-11 20:59:12 +000014using Thrift.Protocol;
15
16namespace Thrift
17{
18 public class TApplicationException : Exception
19 {
20 protected ExceptionType type;
21
22 public TApplicationException()
23 {
24 }
25
26 public TApplicationException(ExceptionType type)
27 {
28 this.type = type;
29 }
30
31 public TApplicationException(ExceptionType type, string message)
32 : base(message)
33 {
34 this.type = type;
35 }
36
37 public static TApplicationException Read(TProtocol iprot)
38 {
39 TField field;
David Reiss7f42bcf2008-01-11 20:59:12 +000040
41 string message = null;
42 ExceptionType type = ExceptionType.Unknown;
43
44 while (true)
45 {
46 field = iprot.ReadFieldBegin();
47 if (field.Type == TType.Stop)
48 {
49 break;
50 }
51
52 switch (field.ID)
53 {
54 case 1:
55 if (field.Type == TType.String)
56 {
57 message = iprot.ReadString();
58 }
59 else
60 {
61 TProtocolUtil.Skip(iprot, field.Type);
62 }
63 break;
64 case 2:
65 if (field.Type == TType.I32)
66 {
67 type = (ExceptionType)iprot.ReadI32();
68 }
69 else
70 {
71 TProtocolUtil.Skip(iprot, field.Type);
72 }
73 break;
74 default:
75 TProtocolUtil.Skip(iprot, field.Type);
76 break;
77 }
78
79 iprot.ReadFieldEnd();
80 }
81
82 iprot.ReadStructEnd();
83
84 return new TApplicationException(type, message);
85 }
86
87 public void Write(TProtocol oprot)
88 {
89 TStruct struc = new TStruct("TApplicationException");
90 TField field = new TField();
91
92 oprot.WriteStructBegin(struc);
93
94 if (!String.IsNullOrEmpty(Message))
95 {
96 field.Name = "message";
97 field.Type = TType.String;
98 field.ID = 1;
99 oprot.WriteFieldBegin(field);
100 oprot.WriteString(Message);
101 oprot.WriteFieldEnd();
102 }
103
104 field.Name = "type";
105 field.Type = TType.I32;
106 field.ID = 2;
107 oprot.WriteFieldBegin(field);
108 oprot.WriteI32((int)type);
109 oprot.WriteFieldEnd();
110 oprot.WriteFieldStop();
111 oprot.WriteStructEnd();
112 }
113
114 public enum ExceptionType
115 {
116 Unknown,
117 UnknownMethod,
118 InvalidMessageType,
119 WrongMethodName,
120 BadSequenceID,
121 MissingResult
122 }
123 }
124}