blob: 9e1b0bbf59bd95defccef890bb7d5a3f5733c82b [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
Jens Geyer606f1ef2018-04-09 23:09:41 +020025 SysUtils,
Jens Geyere7808552019-12-04 21:24:08 +010026 Thrift.Utils,
Jens Geyer606f1ef2018-04-09 23:09:41 +020027 Thrift.Exception,
28 Thrift.Protocol;
Jake Farrell27274222011-11-10 20:32:44 +000029
30const
Jens Geyer75cf93e2024-02-04 14:50:37 +010031 Version = '0.21.0';
Jake Farrell27274222011-11-10 20:32:44 +000032
33type
Jens Geyer606f1ef2018-04-09 23:09:41 +020034 TException = Thrift.Exception.TException; // compatibility alias
35
Jens Geyere0e32402016-04-20 21:50:48 +020036 TApplicationExceptionSpecializedClass = class of TApplicationExceptionSpecialized;
37
Jens Geyere7808552019-12-04 21:24:08 +010038 TApplicationException = class( TException, IBase, ISupportsToString)
Jake Farrell27274222011-11-10 20:32:44 +000039 public
40 type
41{$SCOPEDENUMS ON}
Jake Farrell7ae13e12011-10-18 14:35:26 +000042 TExceptionType = (
Jake Farrell27274222011-11-10 20:32:44 +000043 Unknown,
44 UnknownMethod,
45 InvalidMessageType,
46 WrongMethodName,
47 BadSequenceID,
Roger Meier01931492012-12-22 21:31:03 +010048 MissingResult,
49 InternalError,
50 ProtocolError,
51 InvalidTransform,
52 InvalidProtocol,
53 UnsupportedClientType
Jake Farrell27274222011-11-10 20:32:44 +000054 );
55{$SCOPEDENUMS OFF}
Jens Geyere7808552019-12-04 21:24:08 +010056 strict private
57 FExceptionType : TExceptionType;
58
59 strict protected
60 function QueryInterface(const IID: TGUID; out Obj): HResult; stdcall;
61 function _AddRef: Integer; stdcall;
62 function _Release: Integer; stdcall;
63
Jens Geyerfad7fd32019-11-09 23:24:52 +010064 strict protected
Jens Geyere0e32402016-04-20 21:50:48 +020065 constructor HiddenCreate(const Msg: string);
Jens Geyer9f11c1e2019-11-09 19:39:20 +010066 class function GetSpecializedExceptionType(AType: TExceptionType): TApplicationExceptionSpecializedClass;
Jens Geyere7808552019-12-04 21:24:08 +010067
Jake Farrell27274222011-11-10 20:32:44 +000068 public
Jens Geyere0e32402016-04-20 21:50:48 +020069 // purposefully hide inherited constructor
70 class function Create(const Msg: string): TApplicationException; overload; deprecated 'Use specialized TApplicationException types (or regenerate from IDL)';
71 class function Create: TApplicationException; overload; deprecated 'Use specialized TApplicationException types (or regenerate from IDL)';
72 class function Create( AType: TExceptionType): TApplicationException; overload; deprecated 'Use specialized TApplicationException types (or regenerate from IDL)';
73 class function Create( AType: TExceptionType; const msg: string): TApplicationException; overload; deprecated 'Use specialized TApplicationException types (or regenerate from IDL)';
74
Jens Geyere7808552019-12-04 21:24:08 +010075 function Type_: TExceptionType; virtual;
76
77 procedure IBase_Read( const iprot: IProtocol);
78 procedure IBase.Read = IBase_Read;
Jake Farrell27274222011-11-10 20:32:44 +000079
Roger Meier333bbf32012-01-08 21:51:08 +000080 class function Read( const iprot: IProtocol): TApplicationException;
81 procedure Write( const oprot: IProtocol );
Jake Farrell27274222011-11-10 20:32:44 +000082 end;
83
Jens Geyere0e32402016-04-20 21:50:48 +020084 // Needed to remove deprecation warning
85 TApplicationExceptionSpecialized = class abstract (TApplicationException)
Jens Geyere7808552019-12-04 21:24:08 +010086 strict protected
87 class function GetType: TApplicationException.TExceptionType; virtual; abstract;
Jens Geyere0e32402016-04-20 21:50:48 +020088 public
89 constructor Create(const Msg: string);
Jens Geyere7808552019-12-04 21:24:08 +010090 function Type_: TApplicationException.TExceptionType; override;
Jens Geyere0e32402016-04-20 21:50:48 +020091 end;
92
Jens Geyer9f11c1e2019-11-09 19:39:20 +010093 TApplicationExceptionUnknown = class (TApplicationExceptionSpecialized)
Jens Geyerfad7fd32019-11-09 23:24:52 +010094 strict protected
Jens Geyer9f11c1e2019-11-09 19:39:20 +010095 class function GetType: TApplicationException.TExceptionType; override;
96 end;
97
98 TApplicationExceptionUnknownMethod = class (TApplicationExceptionSpecialized)
Jens Geyerfad7fd32019-11-09 23:24:52 +010099 strict protected
Jens Geyer9f11c1e2019-11-09 19:39:20 +0100100 class function GetType: TApplicationException.TExceptionType; override;
101 end;
102
103 TApplicationExceptionInvalidMessageType = class (TApplicationExceptionSpecialized)
Jens Geyerfad7fd32019-11-09 23:24:52 +0100104 strict protected
Jens Geyer9f11c1e2019-11-09 19:39:20 +0100105 class function GetType: TApplicationException.TExceptionType; override;
106 end;
107
108 TApplicationExceptionWrongMethodName = class (TApplicationExceptionSpecialized)
Jens Geyerfad7fd32019-11-09 23:24:52 +0100109 strict protected
Jens Geyer9f11c1e2019-11-09 19:39:20 +0100110 class function GetType: TApplicationException.TExceptionType; override;
111 end;
112
113 TApplicationExceptionBadSequenceID = class (TApplicationExceptionSpecialized)
Jens Geyerfad7fd32019-11-09 23:24:52 +0100114 strict protected
Jens Geyer9f11c1e2019-11-09 19:39:20 +0100115 class function GetType: TApplicationException.TExceptionType; override;
116 end;
117
118 TApplicationExceptionMissingResult = class (TApplicationExceptionSpecialized)
Jens Geyerfad7fd32019-11-09 23:24:52 +0100119 strict protected
Jens Geyer9f11c1e2019-11-09 19:39:20 +0100120 class function GetType: TApplicationException.TExceptionType; override;
121 end;
122
123 TApplicationExceptionInternalError = class (TApplicationExceptionSpecialized)
Jens Geyerfad7fd32019-11-09 23:24:52 +0100124 strict protected
Jens Geyer9f11c1e2019-11-09 19:39:20 +0100125 class function GetType: TApplicationException.TExceptionType; override;
126 end;
127
128 TApplicationExceptionProtocolError = class (TApplicationExceptionSpecialized)
Jens Geyerfad7fd32019-11-09 23:24:52 +0100129 strict protected
Jens Geyer9f11c1e2019-11-09 19:39:20 +0100130 class function GetType: TApplicationException.TExceptionType; override;
131 end;
132
133 TApplicationExceptionInvalidTransform = class (TApplicationExceptionSpecialized)
Jens Geyerfad7fd32019-11-09 23:24:52 +0100134 strict protected
Jens Geyer9f11c1e2019-11-09 19:39:20 +0100135 class function GetType: TApplicationException.TExceptionType; override;
136 end;
137
138 TApplicationExceptionInvalidProtocol = class (TApplicationExceptionSpecialized)
Jens Geyerfad7fd32019-11-09 23:24:52 +0100139 strict protected
Jens Geyer9f11c1e2019-11-09 19:39:20 +0100140 class function GetType: TApplicationException.TExceptionType; override;
141 end;
142
143 TApplicationExceptionUnsupportedClientType = class (TApplicationExceptionSpecialized)
Jens Geyerfad7fd32019-11-09 23:24:52 +0100144 strict protected
Jens Geyer9f11c1e2019-11-09 19:39:20 +0100145 class function GetType: TApplicationException.TExceptionType; override;
146 end;
147
Jens Geyere0e32402016-04-20 21:50:48 +0200148
Jake Farrell27274222011-11-10 20:32:44 +0000149
150implementation
151
Jake Farrell27274222011-11-10 20:32:44 +0000152{ TApplicationException }
153
Jens Geyere0e32402016-04-20 21:50:48 +0200154constructor TApplicationException.HiddenCreate(const Msg: string);
Jake Farrell27274222011-11-10 20:32:44 +0000155begin
Jens Geyere0e32402016-04-20 21:50:48 +0200156 inherited Create(Msg);
Jake Farrell27274222011-11-10 20:32:44 +0000157end;
158
Jens Geyere0e32402016-04-20 21:50:48 +0200159class function TApplicationException.Create(const Msg: string): TApplicationException;
Jake Farrell27274222011-11-10 20:32:44 +0000160begin
Jens Geyere0e32402016-04-20 21:50:48 +0200161 Result := TApplicationExceptionUnknown.Create(Msg);
162end;
163
164class function TApplicationException.Create: TApplicationException;
165begin
166 Result := TApplicationExceptionUnknown.Create('');
167end;
168
169class function TApplicationException.Create( AType: TExceptionType): TApplicationException;
170begin
171{$WARN SYMBOL_DEPRECATED OFF}
172 Result := Create(AType, '');
173{$WARN SYMBOL_DEPRECATED DEFAULT}
174end;
175
176class function TApplicationException.Create( AType: TExceptionType; const msg: string): TApplicationException;
177begin
178 Result := GetSpecializedExceptionType(AType).Create(msg);
179end;
180
Jens Geyere7808552019-12-04 21:24:08 +0100181
182function TApplicationException.QueryInterface(const IID: TGUID; out Obj): HResult;
183begin
184 if GetInterface(IID, Obj)
185 then result := S_OK
186 else result := E_NOINTERFACE;
187end;
188
189function TApplicationException._AddRef: Integer;
190begin
191 result := -1; // not refcounted
192end;
193
194function TApplicationException._Release: Integer;
195begin
196 result := -1; // not refcounted
197end;
198
199
200function TApplicationException.Type_: TExceptionType;
201begin
202 result := FExceptionType;
203end;
204
205
Jens Geyere0e32402016-04-20 21:50:48 +0200206class function TApplicationException.GetSpecializedExceptionType(AType: TExceptionType): TApplicationExceptionSpecializedClass;
207begin
208 case AType of
209 TExceptionType.UnknownMethod: Result := TApplicationExceptionUnknownMethod;
210 TExceptionType.InvalidMessageType: Result := TApplicationExceptionInvalidMessageType;
211 TExceptionType.WrongMethodName: Result := TApplicationExceptionWrongMethodName;
212 TExceptionType.BadSequenceID: Result := TApplicationExceptionBadSequenceID;
213 TExceptionType.MissingResult: Result := TApplicationExceptionMissingResult;
214 TExceptionType.InternalError: Result := TApplicationExceptionInternalError;
215 TExceptionType.ProtocolError: Result := TApplicationExceptionProtocolError;
216 TExceptionType.InvalidTransform: Result := TApplicationExceptionInvalidTransform;
217 TExceptionType.InvalidProtocol: Result := TApplicationExceptionInvalidProtocol;
218 TExceptionType.UnsupportedClientType: Result := TApplicationExceptionUnsupportedClientType;
219 else
Jens Geyer9f11c1e2019-11-09 19:39:20 +0100220 ASSERT( TExceptionType.Unknown = aType);
Jens Geyere0e32402016-04-20 21:50:48 +0200221 Result := TApplicationExceptionUnknown;
222 end;
Jake Farrell27274222011-11-10 20:32:44 +0000223end;
224
Jens Geyer9f11c1e2019-11-09 19:39:20 +0100225
Jens Geyere7808552019-12-04 21:24:08 +0100226procedure TApplicationException.IBase_Read( const iprot: IProtocol);
Jake Farrell27274222011-11-10 20:32:44 +0000227var
Jens Geyer17c3ad92017-09-05 20:31:27 +0200228 field : TThriftField;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200229 struc : TThriftStruct;
Jake Farrell27274222011-11-10 20:32:44 +0000230begin
Jens Geyerbeb93772014-01-23 19:16:52 +0100231 struc := iprot.ReadStructBegin;
Jake Farrell27274222011-11-10 20:32:44 +0000232 while ( True ) do
233 begin
234 field := iprot.ReadFieldBegin;
Jens Geyere7808552019-12-04 21:24:08 +0100235 if ( field.Type_ = TType.Stop) then begin
Jake Farrell27274222011-11-10 20:32:44 +0000236 Break;
237 end;
238
239 case field.Id of
240 1 : begin
Jens Geyere7808552019-12-04 21:24:08 +0100241 if ( field.Type_ = TType.String_) then begin
242 Exception(Self).Message := iprot.ReadString;
243 end else begin
Jake Farrell27274222011-11-10 20:32:44 +0000244 TProtocolUtil.Skip( iprot, field.Type_ );
245 end;
246 end;
247
248 2 : begin
Jens Geyere7808552019-12-04 21:24:08 +0100249 if ( field.Type_ = TType.I32) then begin
250 FExceptionType := TExceptionType( iprot.ReadI32 );
251 end else begin
Jake Farrell27274222011-11-10 20:32:44 +0000252 TProtocolUtil.Skip( iprot, field.Type_ );
253 end;
Jens Geyere7808552019-12-04 21:24:08 +0100254 end else begin
Jake Farrell27274222011-11-10 20:32:44 +0000255 TProtocolUtil.Skip( iprot, field.Type_);
256 end;
257 end;
258 iprot.ReadFieldEnd;
259 end;
260 iprot.ReadStructEnd;
Jens Geyere7808552019-12-04 21:24:08 +0100261end;
262
263
264class function TApplicationException.Read( const iprot: IProtocol): TApplicationException;
265var instance : TApplicationException;
266 base : IBase;
267begin
268 instance := TApplicationException.CreateFmt('',[]);
269 try
270 if Supports( instance, IBase, base) then try
271 base.Read(iprot);
272 finally
273 base := nil; // clear ref before free
274 end;
275
276 result := GetSpecializedExceptionType(instance.Type_).Create( Exception(instance).Message);
277 finally
278 instance.Free;
279 end;
Jake Farrell27274222011-11-10 20:32:44 +0000280end;
281
Roger Meier333bbf32012-01-08 21:51:08 +0000282procedure TApplicationException.Write( const oprot: IProtocol);
Jake Farrell27274222011-11-10 20:32:44 +0000283var
Jens Geyer17c3ad92017-09-05 20:31:27 +0200284 struc : TThriftStruct;
285 field : TThriftField;
Jake Farrell27274222011-11-10 20:32:44 +0000286begin
Jens Geyer17c3ad92017-09-05 20:31:27 +0200287 Init(struc, 'TApplicationException');
288 Init(field);
Jake Farrell27274222011-11-10 20:32:44 +0000289
290 oprot.WriteStructBegin( struc );
Jens Geyere7808552019-12-04 21:24:08 +0100291 if Message <> '' then begin
Jake Farrell27274222011-11-10 20:32:44 +0000292 field.Name := 'message';
293 field.Type_ := TType.String_;
294 field.Id := 1;
295 oprot.WriteFieldBegin( field );
296 oprot.WriteString( Message );
297 oprot.WriteFieldEnd;
298 end;
299
300 field.Name := 'type';
301 field.Type_ := TType.I32;
302 field.Id := 2;
303 oprot.WriteFieldBegin(field);
Jens Geyere7808552019-12-04 21:24:08 +0100304 oprot.WriteI32(Integer(Type_));
Jake Farrell27274222011-11-10 20:32:44 +0000305 oprot.WriteFieldEnd();
306 oprot.WriteFieldStop();
307 oprot.WriteStructEnd();
308end;
309
Jens Geyere0e32402016-04-20 21:50:48 +0200310{ TApplicationExceptionSpecialized }
311
312constructor TApplicationExceptionSpecialized.Create(const Msg: string);
313begin
314 inherited HiddenCreate(Msg);
315end;
316
Jens Geyere7808552019-12-04 21:24:08 +0100317function TApplicationExceptionSpecialized.Type_: TApplicationException.TExceptionType;
318begin
319 result := GetType;
320end;
321
322
Jens Geyer9f11c1e2019-11-09 19:39:20 +0100323{ specialized TApplicationExceptions }
324
325class function TApplicationExceptionUnknownMethod.GetType : TApplicationException.TExceptionType;
326begin
327 result := TExceptionType.UnknownMethod;
328end;
329
330class function TApplicationExceptionInvalidMessageType.GetType : TApplicationException.TExceptionType;
331begin
332 result := TExceptionType.InvalidMessageType;
333end;
334
335class function TApplicationExceptionWrongMethodName.GetType : TApplicationException.TExceptionType;
336begin
337 result := TExceptionType.WrongMethodName;
338end;
339
340class function TApplicationExceptionBadSequenceID.GetType : TApplicationException.TExceptionType;
341begin
342 result := TExceptionType.BadSequenceID;
343end;
344
345class function TApplicationExceptionMissingResult.GetType : TApplicationException.TExceptionType;
346begin
347 result := TExceptionType.MissingResult;
348end;
349
350class function TApplicationExceptionInternalError.GetType : TApplicationException.TExceptionType;
351begin
352 result := TExceptionType.InternalError;
353end;
354
355class function TApplicationExceptionProtocolError.GetType : TApplicationException.TExceptionType;
356begin
357 result := TExceptionType.ProtocolError;
358end;
359
360class function TApplicationExceptionInvalidTransform.GetType : TApplicationException.TExceptionType;
361begin
362 result := TExceptionType.InvalidTransform;
363end;
364
365class function TApplicationExceptionInvalidProtocol.GetType : TApplicationException.TExceptionType;
366begin
367 result := TExceptionType.InvalidProtocol;
368end;
369
370class function TApplicationExceptionUnsupportedClientType.GetType : TApplicationException.TExceptionType;
371begin
372 result := TExceptionType.UnsupportedClientType;
373end;
374
375class function TApplicationExceptionUnknown.GetType : TApplicationException.TExceptionType;
376begin
377 result := TExceptionType.Unknown;
378end;
379
380
Jake Farrell27274222011-11-10 20:32:44 +0000381end.