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