blob: 716e4d22c02f938d574359c2a6847469b3a25a54 [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,
26 Thrift.Exception,
27 Thrift.Protocol;
Jake Farrell27274222011-11-10 20:32:44 +000028
29const
Jens Geyere02559f2019-10-17 00:11:59 +020030 Version = '0.14.0';
Jake Farrell27274222011-11-10 20:32:44 +000031
32type
Jens Geyer606f1ef2018-04-09 23:09:41 +020033 TException = Thrift.Exception.TException; // compatibility alias
34
Jens Geyere0e32402016-04-20 21:50:48 +020035 TApplicationExceptionSpecializedClass = class of TApplicationExceptionSpecialized;
36
Jens Geyer9f11c1e2019-11-09 19:39:20 +010037 TApplicationException = class abstract( TException)
Jake Farrell27274222011-11-10 20:32:44 +000038 public
39 type
40{$SCOPEDENUMS ON}
Jake Farrell7ae13e12011-10-18 14:35:26 +000041 TExceptionType = (
Jake Farrell27274222011-11-10 20:32:44 +000042 Unknown,
43 UnknownMethod,
44 InvalidMessageType,
45 WrongMethodName,
46 BadSequenceID,
Roger Meier01931492012-12-22 21:31:03 +010047 MissingResult,
48 InternalError,
49 ProtocolError,
50 InvalidTransform,
51 InvalidProtocol,
52 UnsupportedClientType
Jake Farrell27274222011-11-10 20:32:44 +000053 );
54{$SCOPEDENUMS OFF}
Jens Geyerfad7fd32019-11-09 23:24:52 +010055 strict protected
Jens Geyere0e32402016-04-20 21:50:48 +020056 constructor HiddenCreate(const Msg: string);
Jens Geyer9f11c1e2019-11-09 19:39:20 +010057 class function GetType: TExceptionType; virtual; abstract;
58 class function GetSpecializedExceptionType(AType: TExceptionType): TApplicationExceptionSpecializedClass;
Jake Farrell27274222011-11-10 20:32:44 +000059 public
Jens Geyere0e32402016-04-20 21:50:48 +020060 // purposefully hide inherited constructor
61 class function Create(const Msg: string): TApplicationException; overload; deprecated 'Use specialized TApplicationException types (or regenerate from IDL)';
62 class function Create: TApplicationException; overload; deprecated 'Use specialized TApplicationException types (or regenerate from IDL)';
63 class function Create( AType: TExceptionType): TApplicationException; overload; deprecated 'Use specialized TApplicationException types (or regenerate from IDL)';
64 class function Create( AType: TExceptionType; const msg: string): TApplicationException; overload; deprecated 'Use specialized TApplicationException types (or regenerate from IDL)';
65
Jens Geyer9f11c1e2019-11-09 19:39:20 +010066 property Type_: TExceptionType read GetType;
Jake Farrell27274222011-11-10 20:32:44 +000067
Roger Meier333bbf32012-01-08 21:51:08 +000068 class function Read( const iprot: IProtocol): TApplicationException;
69 procedure Write( const oprot: IProtocol );
Jake Farrell27274222011-11-10 20:32:44 +000070 end;
71
Jens Geyere0e32402016-04-20 21:50:48 +020072 // Needed to remove deprecation warning
73 TApplicationExceptionSpecialized = class abstract (TApplicationException)
74 public
75 constructor Create(const Msg: string);
76 end;
77
Jens Geyer9f11c1e2019-11-09 19:39:20 +010078 TApplicationExceptionUnknown = class (TApplicationExceptionSpecialized)
Jens Geyerfad7fd32019-11-09 23:24:52 +010079 strict protected
Jens Geyer9f11c1e2019-11-09 19:39:20 +010080 class function GetType: TApplicationException.TExceptionType; override;
81 end;
82
83 TApplicationExceptionUnknownMethod = class (TApplicationExceptionSpecialized)
Jens Geyerfad7fd32019-11-09 23:24:52 +010084 strict protected
Jens Geyer9f11c1e2019-11-09 19:39:20 +010085 class function GetType: TApplicationException.TExceptionType; override;
86 end;
87
88 TApplicationExceptionInvalidMessageType = class (TApplicationExceptionSpecialized)
Jens Geyerfad7fd32019-11-09 23:24:52 +010089 strict protected
Jens Geyer9f11c1e2019-11-09 19:39:20 +010090 class function GetType: TApplicationException.TExceptionType; override;
91 end;
92
93 TApplicationExceptionWrongMethodName = 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 TApplicationExceptionBadSequenceID = 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 TApplicationExceptionMissingResult = 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 TApplicationExceptionInternalError = 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 TApplicationExceptionProtocolError = 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 TApplicationExceptionInvalidTransform = 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 TApplicationExceptionInvalidProtocol = 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 TApplicationExceptionUnsupportedClientType = 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
Jens Geyere0e32402016-04-20 21:50:48 +0200133
Jake Farrell27274222011-11-10 20:32:44 +0000134
135implementation
136
Jake Farrell27274222011-11-10 20:32:44 +0000137{ TApplicationException }
138
Jens Geyere0e32402016-04-20 21:50:48 +0200139constructor TApplicationException.HiddenCreate(const Msg: string);
Jake Farrell27274222011-11-10 20:32:44 +0000140begin
Jens Geyere0e32402016-04-20 21:50:48 +0200141 inherited Create(Msg);
Jake Farrell27274222011-11-10 20:32:44 +0000142end;
143
Jens Geyere0e32402016-04-20 21:50:48 +0200144class function TApplicationException.Create(const Msg: string): TApplicationException;
Jake Farrell27274222011-11-10 20:32:44 +0000145begin
Jens Geyere0e32402016-04-20 21:50:48 +0200146 Result := TApplicationExceptionUnknown.Create(Msg);
147end;
148
149class function TApplicationException.Create: TApplicationException;
150begin
151 Result := TApplicationExceptionUnknown.Create('');
152end;
153
154class function TApplicationException.Create( AType: TExceptionType): TApplicationException;
155begin
156{$WARN SYMBOL_DEPRECATED OFF}
157 Result := Create(AType, '');
158{$WARN SYMBOL_DEPRECATED DEFAULT}
159end;
160
161class function TApplicationException.Create( AType: TExceptionType; const msg: string): TApplicationException;
162begin
163 Result := GetSpecializedExceptionType(AType).Create(msg);
164end;
165
166class function TApplicationException.GetSpecializedExceptionType(AType: TExceptionType): TApplicationExceptionSpecializedClass;
167begin
168 case AType of
169 TExceptionType.UnknownMethod: Result := TApplicationExceptionUnknownMethod;
170 TExceptionType.InvalidMessageType: Result := TApplicationExceptionInvalidMessageType;
171 TExceptionType.WrongMethodName: Result := TApplicationExceptionWrongMethodName;
172 TExceptionType.BadSequenceID: Result := TApplicationExceptionBadSequenceID;
173 TExceptionType.MissingResult: Result := TApplicationExceptionMissingResult;
174 TExceptionType.InternalError: Result := TApplicationExceptionInternalError;
175 TExceptionType.ProtocolError: Result := TApplicationExceptionProtocolError;
176 TExceptionType.InvalidTransform: Result := TApplicationExceptionInvalidTransform;
177 TExceptionType.InvalidProtocol: Result := TApplicationExceptionInvalidProtocol;
178 TExceptionType.UnsupportedClientType: Result := TApplicationExceptionUnsupportedClientType;
179 else
Jens Geyer9f11c1e2019-11-09 19:39:20 +0100180 ASSERT( TExceptionType.Unknown = aType);
Jens Geyere0e32402016-04-20 21:50:48 +0200181 Result := TApplicationExceptionUnknown;
182 end;
Jake Farrell27274222011-11-10 20:32:44 +0000183end;
184
Jens Geyer9f11c1e2019-11-09 19:39:20 +0100185
Roger Meier333bbf32012-01-08 21:51:08 +0000186class function TApplicationException.Read( const iprot: IProtocol): TApplicationException;
Jake Farrell27274222011-11-10 20:32:44 +0000187var
Jens Geyer17c3ad92017-09-05 20:31:27 +0200188 field : TThriftField;
Jake Farrell27274222011-11-10 20:32:44 +0000189 msg : string;
190 typ : TExceptionType;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200191 struc : TThriftStruct;
Jake Farrell27274222011-11-10 20:32:44 +0000192begin
193 msg := '';
194 typ := TExceptionType.Unknown;
Jens Geyerbeb93772014-01-23 19:16:52 +0100195 struc := iprot.ReadStructBegin;
Jake Farrell27274222011-11-10 20:32:44 +0000196 while ( True ) do
197 begin
198 field := iprot.ReadFieldBegin;
199 if ( field.Type_ = TType.Stop) then
200 begin
201 Break;
202 end;
203
204 case field.Id of
205 1 : begin
206 if ( field.Type_ = TType.String_) then
207 begin
208 msg := iprot.ReadString;
209 end else
210 begin
211 TProtocolUtil.Skip( iprot, field.Type_ );
212 end;
213 end;
214
215 2 : begin
216 if ( field.Type_ = TType.I32) then
217 begin
218 typ := TExceptionType( iprot.ReadI32 );
219 end else
220 begin
221 TProtocolUtil.Skip( iprot, field.Type_ );
222 end;
223 end else
224 begin
225 TProtocolUtil.Skip( iprot, field.Type_);
226 end;
227 end;
228 iprot.ReadFieldEnd;
229 end;
230 iprot.ReadStructEnd;
Jens Geyere0e32402016-04-20 21:50:48 +0200231 Result := GetSpecializedExceptionType(typ).Create(msg);
Jake Farrell27274222011-11-10 20:32:44 +0000232end;
233
Roger Meier333bbf32012-01-08 21:51:08 +0000234procedure TApplicationException.Write( const oprot: IProtocol);
Jake Farrell27274222011-11-10 20:32:44 +0000235var
Jens Geyer17c3ad92017-09-05 20:31:27 +0200236 struc : TThriftStruct;
237 field : TThriftField;
Jake Farrell27274222011-11-10 20:32:44 +0000238begin
Jens Geyer17c3ad92017-09-05 20:31:27 +0200239 Init(struc, 'TApplicationException');
240 Init(field);
Jake Farrell27274222011-11-10 20:32:44 +0000241
242 oprot.WriteStructBegin( struc );
243 if Message <> '' then
244 begin
245 field.Name := 'message';
246 field.Type_ := TType.String_;
247 field.Id := 1;
248 oprot.WriteFieldBegin( field );
249 oprot.WriteString( Message );
250 oprot.WriteFieldEnd;
251 end;
252
253 field.Name := 'type';
254 field.Type_ := TType.I32;
255 field.Id := 2;
256 oprot.WriteFieldBegin(field);
Jens Geyere0e32402016-04-20 21:50:48 +0200257 oprot.WriteI32(Integer(GetType));
Jake Farrell27274222011-11-10 20:32:44 +0000258 oprot.WriteFieldEnd();
259 oprot.WriteFieldStop();
260 oprot.WriteStructEnd();
261end;
262
Jens Geyere0e32402016-04-20 21:50:48 +0200263{ TApplicationExceptionSpecialized }
264
265constructor TApplicationExceptionSpecialized.Create(const Msg: string);
266begin
267 inherited HiddenCreate(Msg);
268end;
269
Jens Geyer9f11c1e2019-11-09 19:39:20 +0100270{ specialized TApplicationExceptions }
271
272class function TApplicationExceptionUnknownMethod.GetType : TApplicationException.TExceptionType;
273begin
274 result := TExceptionType.UnknownMethod;
275end;
276
277class function TApplicationExceptionInvalidMessageType.GetType : TApplicationException.TExceptionType;
278begin
279 result := TExceptionType.InvalidMessageType;
280end;
281
282class function TApplicationExceptionWrongMethodName.GetType : TApplicationException.TExceptionType;
283begin
284 result := TExceptionType.WrongMethodName;
285end;
286
287class function TApplicationExceptionBadSequenceID.GetType : TApplicationException.TExceptionType;
288begin
289 result := TExceptionType.BadSequenceID;
290end;
291
292class function TApplicationExceptionMissingResult.GetType : TApplicationException.TExceptionType;
293begin
294 result := TExceptionType.MissingResult;
295end;
296
297class function TApplicationExceptionInternalError.GetType : TApplicationException.TExceptionType;
298begin
299 result := TExceptionType.InternalError;
300end;
301
302class function TApplicationExceptionProtocolError.GetType : TApplicationException.TExceptionType;
303begin
304 result := TExceptionType.ProtocolError;
305end;
306
307class function TApplicationExceptionInvalidTransform.GetType : TApplicationException.TExceptionType;
308begin
309 result := TExceptionType.InvalidTransform;
310end;
311
312class function TApplicationExceptionInvalidProtocol.GetType : TApplicationException.TExceptionType;
313begin
314 result := TExceptionType.InvalidProtocol;
315end;
316
317class function TApplicationExceptionUnsupportedClientType.GetType : TApplicationException.TExceptionType;
318begin
319 result := TExceptionType.UnsupportedClientType;
320end;
321
322class function TApplicationExceptionUnknown.GetType : TApplicationException.TExceptionType;
323begin
324 result := TExceptionType.Unknown;
325end;
326
327
Jake Farrell27274222011-11-10 20:32:44 +0000328end.