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