]>
Commit | Line | Data |
---|---|---|
f67539c2 TL |
1 | /** |
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. | |
18 | * | |
19 | * Contains some contributions under the Thrift Software License. | |
20 | * Please see doc/old-thrift-license.txt in the Thrift distribution for | |
21 | * details. | |
22 | */ | |
23 | ||
24 | using System; | |
25 | using Thrift.Protocol; | |
26 | ||
27 | namespace Thrift | |
28 | { | |
29 | public class TApplicationException : TException | |
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, null) // TApplicationException is serializable, but we never serialize InnerException | |
44 | { | |
45 | this.type = type; | |
46 | } | |
47 | ||
48 | public static TApplicationException Read(TProtocol iprot) | |
49 | { | |
50 | TField field; | |
51 | ||
52 | string message = null; | |
53 | ExceptionType type = ExceptionType.Unknown; | |
54 | ||
55 | iprot.ReadStructBegin(); | |
56 | while (true) | |
57 | { | |
58 | field = iprot.ReadFieldBegin(); | |
59 | if (field.Type == TType.Stop) | |
60 | { | |
61 | break; | |
62 | } | |
63 | ||
64 | 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 | } | |
90 | ||
91 | iprot.ReadFieldEnd(); | |
92 | } | |
93 | ||
94 | iprot.ReadStructEnd(); | |
95 | ||
96 | return new TApplicationException(type, message); | |
97 | } | |
98 | ||
99 | public void Write(TProtocol oprot) | |
100 | { | |
101 | TStruct struc = new TStruct("TApplicationException"); | |
102 | TField field = new TField(); | |
103 | ||
104 | oprot.WriteStructBegin(struc); | |
105 | ||
106 | 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 | } | |
115 | ||
116 | 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 | } | |
125 | ||
126 | 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 | ||
141 | public ExceptionType Type | |
142 | { | |
143 | get { return type; } | |
144 | } | |
145 | } | |
146 | } |