blob: aa69f93129af531d13ee81283f28d062b930c748 [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
Jake Farrell99010692011-11-30 02:09:46 +000028 Version = '0.9.0-dev';
Jake Farrell27274222011-11-10 20:32:44 +000029
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
Jake Farrellac102562011-11-23 14:30:41 +000063 function Message : string; // hide inherited property: allow read, but prevent accidental writes
64 procedure UpdateMessageProperty; // update inherited message property with toString()
Jake Farrell27274222011-11-10 20:32:44 +000065 end;
66
67implementation
68
69{ TException }
70
Jake Farrellac102562011-11-23 14:30:41 +000071function TException.Message;
72// allow read (exception summary), but prevent accidental writes
73// read will return the exception summary
Jake Farrell27274222011-11-10 20:32:44 +000074begin
Jake Farrellac102562011-11-23 14:30:41 +000075 result := Self.ToString;
76end;
77
78procedure TException.UpdateMessageProperty;
79// Update the inherited Message property to better conform to standard behaviour.
80// Nice benefit: The IDE is now able to show the exception message again.
81begin
82 inherited Message := Self.ToString; // produces a summary text
Jake Farrell27274222011-11-10 20:32:44 +000083end;
84
85{ TApplicationException }
86
87constructor TApplicationException.Create;
88begin
89 inherited Create( '' );
90end;
91
92constructor TApplicationException.Create(AType: TExceptionType;
93 const msg: string);
94begin
95 inherited Create( msg );
96 FType := AType;
97end;
98
99constructor TApplicationException.Create(AType: TExceptionType);
100begin
101 inherited Create('');
102 FType := AType;
103end;
104
105class function TApplicationException.Read(
106 iprot: IProtocol): TApplicationException;
107var
108 field : IField;
109 msg : string;
110 typ : TExceptionType;
111begin
112 msg := '';
113 typ := TExceptionType.Unknown;
114 while ( True ) do
115 begin
116 field := iprot.ReadFieldBegin;
117 if ( field.Type_ = TType.Stop) then
118 begin
119 Break;
120 end;
121
122 case field.Id of
123 1 : begin
124 if ( field.Type_ = TType.String_) then
125 begin
126 msg := iprot.ReadString;
127 end else
128 begin
129 TProtocolUtil.Skip( iprot, field.Type_ );
130 end;
131 end;
132
133 2 : begin
134 if ( field.Type_ = TType.I32) then
135 begin
136 typ := TExceptionType( iprot.ReadI32 );
137 end else
138 begin
139 TProtocolUtil.Skip( iprot, field.Type_ );
140 end;
141 end else
142 begin
143 TProtocolUtil.Skip( iprot, field.Type_);
144 end;
145 end;
146 iprot.ReadFieldEnd;
147 end;
148 iprot.ReadStructEnd;
149 Result := TApplicationException.Create( typ, msg );
150end;
151
152procedure TApplicationException.Write(oprot: IProtocol);
153var
154 struc : IStruct;
155 field : IField;
156
157begin
158 struc := TStructImpl.Create( 'TApplicationException' );
159 field := TFieldImpl.Create;
160
161 oprot.WriteStructBegin( struc );
162 if Message <> '' then
163 begin
164 field.Name := 'message';
165 field.Type_ := TType.String_;
166 field.Id := 1;
167 oprot.WriteFieldBegin( field );
168 oprot.WriteString( Message );
169 oprot.WriteFieldEnd;
170 end;
171
172 field.Name := 'type';
173 field.Type_ := TType.I32;
174 field.Id := 2;
175 oprot.WriteFieldBegin(field);
176 oprot.WriteI32(Integer(FType));
177 oprot.WriteFieldEnd();
178 oprot.WriteFieldStop();
179 oprot.WriteStructEnd();
180end;
181
182end.