blob: 44f12d783948138ce6d87cd228b61f94f9fa7fb4 [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 Farrell6fcecd42012-10-11 20:34:25 +000028 Version = '1.0.0-dev';
Jake Farrell27274222011-11-10 20:32:44 +000029
30type
31 IProcessor = interface
32 ['{B1538A07-6CAC-4406-8A4C-AFED07C70A89}']
Roger Meier333bbf32012-01-08 21:51:08 +000033 function Process( const iprot :IProtocol; const oprot: IProtocol): Boolean;
Jake Farrell27274222011-11-10 20:32:44 +000034 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,
Roger Meier01931492012-12-22 21:31:03 +010046 MissingResult,
47 InternalError,
48 ProtocolError,
49 InvalidTransform,
50 InvalidProtocol,
51 UnsupportedClientType
Jake Farrell27274222011-11-10 20:32:44 +000052 );
53{$SCOPEDENUMS OFF}
54 private
55 FType : TExceptionType;
56 public
57 constructor Create; overload;
58 constructor Create( AType: TExceptionType); overload;
59 constructor Create( AType: TExceptionType; const msg: string); overload;
60
Roger Meier333bbf32012-01-08 21:51:08 +000061 class function Read( const iprot: IProtocol): TApplicationException;
62 procedure Write( const oprot: IProtocol );
Jake Farrell27274222011-11-10 20:32:44 +000063 end;
64
65 // base class for IDL-generated exceptions
66 TException = class( SysUtils.Exception)
67 public
Jake Farrellac102562011-11-23 14:30:41 +000068 function Message : string; // hide inherited property: allow read, but prevent accidental writes
69 procedure UpdateMessageProperty; // update inherited message property with toString()
Jake Farrell27274222011-11-10 20:32:44 +000070 end;
71
72implementation
73
74{ TException }
75
Jake Farrellac102562011-11-23 14:30:41 +000076function TException.Message;
77// allow read (exception summary), but prevent accidental writes
78// read will return the exception summary
Jake Farrell27274222011-11-10 20:32:44 +000079begin
Jake Farrellac102562011-11-23 14:30:41 +000080 result := Self.ToString;
81end;
82
83procedure TException.UpdateMessageProperty;
84// Update the inherited Message property to better conform to standard behaviour.
85// Nice benefit: The IDE is now able to show the exception message again.
86begin
87 inherited Message := Self.ToString; // produces a summary text
Jake Farrell27274222011-11-10 20:32:44 +000088end;
89
90{ TApplicationException }
91
92constructor TApplicationException.Create;
93begin
94 inherited Create( '' );
95end;
96
97constructor TApplicationException.Create(AType: TExceptionType;
98 const msg: string);
99begin
100 inherited Create( msg );
101 FType := AType;
102end;
103
104constructor TApplicationException.Create(AType: TExceptionType);
105begin
106 inherited Create('');
107 FType := AType;
108end;
109
Roger Meier333bbf32012-01-08 21:51:08 +0000110class function TApplicationException.Read( const iprot: IProtocol): TApplicationException;
Jake Farrell27274222011-11-10 20:32:44 +0000111var
112 field : IField;
113 msg : string;
114 typ : TExceptionType;
115begin
116 msg := '';
117 typ := TExceptionType.Unknown;
118 while ( True ) do
119 begin
120 field := iprot.ReadFieldBegin;
121 if ( field.Type_ = TType.Stop) then
122 begin
123 Break;
124 end;
125
126 case field.Id of
127 1 : begin
128 if ( field.Type_ = TType.String_) then
129 begin
130 msg := iprot.ReadString;
131 end else
132 begin
133 TProtocolUtil.Skip( iprot, field.Type_ );
134 end;
135 end;
136
137 2 : begin
138 if ( field.Type_ = TType.I32) then
139 begin
140 typ := TExceptionType( iprot.ReadI32 );
141 end else
142 begin
143 TProtocolUtil.Skip( iprot, field.Type_ );
144 end;
145 end else
146 begin
147 TProtocolUtil.Skip( iprot, field.Type_);
148 end;
149 end;
150 iprot.ReadFieldEnd;
151 end;
152 iprot.ReadStructEnd;
153 Result := TApplicationException.Create( typ, msg );
154end;
155
Roger Meier333bbf32012-01-08 21:51:08 +0000156procedure TApplicationException.Write( const oprot: IProtocol);
Jake Farrell27274222011-11-10 20:32:44 +0000157var
158 struc : IStruct;
159 field : IField;
160
161begin
162 struc := TStructImpl.Create( 'TApplicationException' );
163 field := TFieldImpl.Create;
164
165 oprot.WriteStructBegin( struc );
166 if Message <> '' then
167 begin
168 field.Name := 'message';
169 field.Type_ := TType.String_;
170 field.Id := 1;
171 oprot.WriteFieldBegin( field );
172 oprot.WriteString( Message );
173 oprot.WriteFieldEnd;
174 end;
175
176 field.Name := 'type';
177 field.Type_ := TType.I32;
178 field.Id := 2;
179 oprot.WriteFieldBegin(field);
180 oprot.WriteI32(Integer(FType));
181 oprot.WriteFieldEnd();
182 oprot.WriteFieldStop();
183 oprot.WriteStructEnd();
184end;
185
186end.