blob: 50513d35d77355ab3fb267e6d075b2e9858052bf [file] [log] [blame]
Jake Farrell27274222011-11-10 20:32:44 +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.
18 *)
19
20unit Thrift;
21
22interface
23
24uses
25 SysUtils, Thrift.Protocol;
26
27const
28 Version = '0.8.0-dev';
29
30type
31 IProcessor = interface
32 ['{B1538A07-6CAC-4406-8A4C-AFED07C70A89}']
33 function Process( iprot :IProtocol; oprot: IProtocol): Boolean;
34 end;
35
36 TApplicationException = class( SysUtils.Exception )
37 public
38 type
39{$SCOPEDENUMS ON}
Jake Farrell7ae13e12011-10-18 14:35:26 +000040 TExceptionType = (
Jake Farrell27274222011-11-10 20:32:44 +000041 Unknown,
42 UnknownMethod,
43 InvalidMessageType,
44 WrongMethodName,
45 BadSequenceID,
46 MissingResult
47 );
48{$SCOPEDENUMS OFF}
49 private
50 FType : TExceptionType;
51 public
52 constructor Create; overload;
53 constructor Create( AType: TExceptionType); overload;
54 constructor Create( AType: TExceptionType; const msg: string); overload;
55
56 class function Read( iprot: IProtocol): TApplicationException;
57 procedure Write( oprot: IProtocol );
58 end;
59
60 // base class for IDL-generated exceptions
61 TException = class( SysUtils.Exception)
62 public
63 procedure Message; // hide inherited property to prevent accidental read/write
64 end;
65
66implementation
67
68{ TException }
69
70procedure TException.Message;
71// hide inherited property to prevent accidental read/write
72begin
73 ASSERT( FALSE, 'Unexpected call to '+ClassName+'.message. Forgot the underscore?');
74end;
75
76{ TApplicationException }
77
78constructor TApplicationException.Create;
79begin
80 inherited Create( '' );
81end;
82
83constructor TApplicationException.Create(AType: TExceptionType;
84 const msg: string);
85begin
86 inherited Create( msg );
87 FType := AType;
88end;
89
90constructor TApplicationException.Create(AType: TExceptionType);
91begin
92 inherited Create('');
93 FType := AType;
94end;
95
96class function TApplicationException.Read(
97 iprot: IProtocol): TApplicationException;
98var
99 field : IField;
100 msg : string;
101 typ : TExceptionType;
102begin
103 msg := '';
104 typ := TExceptionType.Unknown;
105 while ( True ) do
106 begin
107 field := iprot.ReadFieldBegin;
108 if ( field.Type_ = TType.Stop) then
109 begin
110 Break;
111 end;
112
113 case field.Id of
114 1 : begin
115 if ( field.Type_ = TType.String_) then
116 begin
117 msg := iprot.ReadString;
118 end else
119 begin
120 TProtocolUtil.Skip( iprot, field.Type_ );
121 end;
122 end;
123
124 2 : begin
125 if ( field.Type_ = TType.I32) then
126 begin
127 typ := TExceptionType( iprot.ReadI32 );
128 end else
129 begin
130 TProtocolUtil.Skip( iprot, field.Type_ );
131 end;
132 end else
133 begin
134 TProtocolUtil.Skip( iprot, field.Type_);
135 end;
136 end;
137 iprot.ReadFieldEnd;
138 end;
139 iprot.ReadStructEnd;
140 Result := TApplicationException.Create( typ, msg );
141end;
142
143procedure TApplicationException.Write(oprot: IProtocol);
144var
145 struc : IStruct;
146 field : IField;
147
148begin
149 struc := TStructImpl.Create( 'TApplicationException' );
150 field := TFieldImpl.Create;
151
152 oprot.WriteStructBegin( struc );
153 if Message <> '' then
154 begin
155 field.Name := 'message';
156 field.Type_ := TType.String_;
157 field.Id := 1;
158 oprot.WriteFieldBegin( field );
159 oprot.WriteString( Message );
160 oprot.WriteFieldEnd;
161 end;
162
163 field.Name := 'type';
164 field.Type_ := TType.I32;
165 field.Id := 2;
166 oprot.WriteFieldBegin(field);
167 oprot.WriteI32(Integer(FType));
168 oprot.WriteFieldEnd();
169 oprot.WriteFieldStop();
170 oprot.WriteStructEnd();
171end;
172
173end.