blob: 6eca3c9cb304d4d41360b7e2b0ecc492edabc782 [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
Jens Geyere0e32402016-04-20 21:50:48 +020031 TApplicationExceptionSpecializedClass = class of TApplicationExceptionSpecialized;
32
Jake Farrell27274222011-11-10 20:32:44 +000033 TApplicationException = class( SysUtils.Exception )
34 public
35 type
36{$SCOPEDENUMS ON}
Jake Farrell7ae13e12011-10-18 14:35:26 +000037 TExceptionType = (
Jake Farrell27274222011-11-10 20:32:44 +000038 Unknown,
39 UnknownMethod,
40 InvalidMessageType,
41 WrongMethodName,
42 BadSequenceID,
Roger Meier01931492012-12-22 21:31:03 +010043 MissingResult,
44 InternalError,
45 ProtocolError,
46 InvalidTransform,
47 InvalidProtocol,
48 UnsupportedClientType
Jake Farrell27274222011-11-10 20:32:44 +000049 );
50{$SCOPEDENUMS OFF}
51 private
Jens Geyere0e32402016-04-20 21:50:48 +020052 function GetType: TExceptionType;
53 protected
54 constructor HiddenCreate(const Msg: string);
Jake Farrell27274222011-11-10 20:32:44 +000055 public
Jens Geyere0e32402016-04-20 21:50:48 +020056 // purposefully hide inherited constructor
57 class function Create(const Msg: string): TApplicationException; overload; deprecated 'Use specialized TApplicationException types (or regenerate from IDL)';
58 class function Create: TApplicationException; overload; deprecated 'Use specialized TApplicationException types (or regenerate from IDL)';
59 class function Create( AType: TExceptionType): TApplicationException; overload; deprecated 'Use specialized TApplicationException types (or regenerate from IDL)';
60 class function Create( AType: TExceptionType; const msg: string): TApplicationException; overload; deprecated 'Use specialized TApplicationException types (or regenerate from IDL)';
61
62 class function GetSpecializedExceptionType(AType: TExceptionType): TApplicationExceptionSpecializedClass;
Jake Farrell27274222011-11-10 20:32:44 +000063
Roger Meier333bbf32012-01-08 21:51:08 +000064 class function Read( const iprot: IProtocol): TApplicationException;
65 procedure Write( const oprot: IProtocol );
Jake Farrell27274222011-11-10 20:32:44 +000066 end;
67
Jens Geyere0e32402016-04-20 21:50:48 +020068 // Needed to remove deprecation warning
69 TApplicationExceptionSpecialized = class abstract (TApplicationException)
70 public
71 constructor Create(const Msg: string);
72 end;
73
74 TApplicationExceptionUnknown = class (TApplicationExceptionSpecialized);
75 TApplicationExceptionUnknownMethod = class (TApplicationExceptionSpecialized);
76 TApplicationExceptionInvalidMessageType = class (TApplicationExceptionSpecialized);
77 TApplicationExceptionWrongMethodName = class (TApplicationExceptionSpecialized);
78 TApplicationExceptionBadSequenceID = class (TApplicationExceptionSpecialized);
79 TApplicationExceptionMissingResult = class (TApplicationExceptionSpecialized);
80 TApplicationExceptionInternalError = class (TApplicationExceptionSpecialized);
81 TApplicationExceptionProtocolError = class (TApplicationExceptionSpecialized);
82 TApplicationExceptionInvalidTransform = class (TApplicationExceptionSpecialized);
83 TApplicationExceptionInvalidProtocol = class (TApplicationExceptionSpecialized);
84 TApplicationExceptionUnsupportedClientType = class (TApplicationExceptionSpecialized);
85
Jake Farrell27274222011-11-10 20:32:44 +000086 // base class for IDL-generated exceptions
87 TException = class( SysUtils.Exception)
88 public
Jake Farrellac102562011-11-23 14:30:41 +000089 function Message : string; // hide inherited property: allow read, but prevent accidental writes
90 procedure UpdateMessageProperty; // update inherited message property with toString()
Jake Farrell27274222011-11-10 20:32:44 +000091 end;
92
93implementation
94
95{ TException }
96
Jake Farrellac102562011-11-23 14:30:41 +000097function TException.Message;
98// allow read (exception summary), but prevent accidental writes
99// read will return the exception summary
Jake Farrell27274222011-11-10 20:32:44 +0000100begin
Jake Farrellac102562011-11-23 14:30:41 +0000101 result := Self.ToString;
102end;
103
104procedure TException.UpdateMessageProperty;
105// Update the inherited Message property to better conform to standard behaviour.
106// Nice benefit: The IDE is now able to show the exception message again.
107begin
108 inherited Message := Self.ToString; // produces a summary text
Jake Farrell27274222011-11-10 20:32:44 +0000109end;
110
111{ TApplicationException }
112
Jens Geyere0e32402016-04-20 21:50:48 +0200113function TApplicationException.GetType: TExceptionType;
Jake Farrell27274222011-11-10 20:32:44 +0000114begin
Jens Geyere0e32402016-04-20 21:50:48 +0200115 if Self is TApplicationExceptionUnknownMethod then Result := TExceptionType.UnknownMethod
116 else if Self is TApplicationExceptionInvalidMessageType then Result := TExceptionType.InvalidMessageType
117 else if Self is TApplicationExceptionWrongMethodName then Result := TExceptionType.WrongMethodName
118 else if Self is TApplicationExceptionBadSequenceID then Result := TExceptionType.BadSequenceID
119 else if Self is TApplicationExceptionMissingResult then Result := TExceptionType.MissingResult
120 else if Self is TApplicationExceptionInternalError then Result := TExceptionType.InternalError
121 else if Self is TApplicationExceptionProtocolError then Result := TExceptionType.ProtocolError
122 else if Self is TApplicationExceptionInvalidTransform then Result := TExceptionType.InvalidTransform
123 else if Self is TApplicationExceptionInvalidProtocol then Result := TExceptionType.InvalidProtocol
124 else if Self is TApplicationExceptionUnsupportedClientType then Result := TExceptionType.UnsupportedClientType
125 else Result := TExceptionType.Unknown;
Jake Farrell27274222011-11-10 20:32:44 +0000126end;
127
Jens Geyere0e32402016-04-20 21:50:48 +0200128constructor TApplicationException.HiddenCreate(const Msg: string);
Jake Farrell27274222011-11-10 20:32:44 +0000129begin
Jens Geyere0e32402016-04-20 21:50:48 +0200130 inherited Create(Msg);
Jake Farrell27274222011-11-10 20:32:44 +0000131end;
132
Jens Geyere0e32402016-04-20 21:50:48 +0200133class function TApplicationException.Create(const Msg: string): TApplicationException;
Jake Farrell27274222011-11-10 20:32:44 +0000134begin
Jens Geyere0e32402016-04-20 21:50:48 +0200135 Result := TApplicationExceptionUnknown.Create(Msg);
136end;
137
138class function TApplicationException.Create: TApplicationException;
139begin
140 Result := TApplicationExceptionUnknown.Create('');
141end;
142
143class function TApplicationException.Create( AType: TExceptionType): TApplicationException;
144begin
145{$WARN SYMBOL_DEPRECATED OFF}
146 Result := Create(AType, '');
147{$WARN SYMBOL_DEPRECATED DEFAULT}
148end;
149
150class function TApplicationException.Create( AType: TExceptionType; const msg: string): TApplicationException;
151begin
152 Result := GetSpecializedExceptionType(AType).Create(msg);
153end;
154
155class function TApplicationException.GetSpecializedExceptionType(AType: TExceptionType): TApplicationExceptionSpecializedClass;
156begin
157 case AType of
158 TExceptionType.UnknownMethod: Result := TApplicationExceptionUnknownMethod;
159 TExceptionType.InvalidMessageType: Result := TApplicationExceptionInvalidMessageType;
160 TExceptionType.WrongMethodName: Result := TApplicationExceptionWrongMethodName;
161 TExceptionType.BadSequenceID: Result := TApplicationExceptionBadSequenceID;
162 TExceptionType.MissingResult: Result := TApplicationExceptionMissingResult;
163 TExceptionType.InternalError: Result := TApplicationExceptionInternalError;
164 TExceptionType.ProtocolError: Result := TApplicationExceptionProtocolError;
165 TExceptionType.InvalidTransform: Result := TApplicationExceptionInvalidTransform;
166 TExceptionType.InvalidProtocol: Result := TApplicationExceptionInvalidProtocol;
167 TExceptionType.UnsupportedClientType: Result := TApplicationExceptionUnsupportedClientType;
168 else
169 Result := TApplicationExceptionUnknown;
170 end;
Jake Farrell27274222011-11-10 20:32:44 +0000171end;
172
Roger Meier333bbf32012-01-08 21:51:08 +0000173class function TApplicationException.Read( const iprot: IProtocol): TApplicationException;
Jake Farrell27274222011-11-10 20:32:44 +0000174var
Jens Geyer17c3ad92017-09-05 20:31:27 +0200175 field : TThriftField;
Jake Farrell27274222011-11-10 20:32:44 +0000176 msg : string;
177 typ : TExceptionType;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200178 struc : TThriftStruct;
Jake Farrell27274222011-11-10 20:32:44 +0000179begin
180 msg := '';
181 typ := TExceptionType.Unknown;
Jens Geyerbeb93772014-01-23 19:16:52 +0100182 struc := iprot.ReadStructBegin;
Jake Farrell27274222011-11-10 20:32:44 +0000183 while ( True ) do
184 begin
185 field := iprot.ReadFieldBegin;
186 if ( field.Type_ = TType.Stop) then
187 begin
188 Break;
189 end;
190
191 case field.Id of
192 1 : begin
193 if ( field.Type_ = TType.String_) then
194 begin
195 msg := iprot.ReadString;
196 end else
197 begin
198 TProtocolUtil.Skip( iprot, field.Type_ );
199 end;
200 end;
201
202 2 : begin
203 if ( field.Type_ = TType.I32) then
204 begin
205 typ := TExceptionType( iprot.ReadI32 );
206 end else
207 begin
208 TProtocolUtil.Skip( iprot, field.Type_ );
209 end;
210 end else
211 begin
212 TProtocolUtil.Skip( iprot, field.Type_);
213 end;
214 end;
215 iprot.ReadFieldEnd;
216 end;
217 iprot.ReadStructEnd;
Jens Geyere0e32402016-04-20 21:50:48 +0200218 Result := GetSpecializedExceptionType(typ).Create(msg);
Jake Farrell27274222011-11-10 20:32:44 +0000219end;
220
Roger Meier333bbf32012-01-08 21:51:08 +0000221procedure TApplicationException.Write( const oprot: IProtocol);
Jake Farrell27274222011-11-10 20:32:44 +0000222var
Jens Geyer17c3ad92017-09-05 20:31:27 +0200223 struc : TThriftStruct;
224 field : TThriftField;
Jake Farrell27274222011-11-10 20:32:44 +0000225begin
Jens Geyer17c3ad92017-09-05 20:31:27 +0200226 Init(struc, 'TApplicationException');
227 Init(field);
Jake Farrell27274222011-11-10 20:32:44 +0000228
229 oprot.WriteStructBegin( struc );
230 if Message <> '' then
231 begin
232 field.Name := 'message';
233 field.Type_ := TType.String_;
234 field.Id := 1;
235 oprot.WriteFieldBegin( field );
236 oprot.WriteString( Message );
237 oprot.WriteFieldEnd;
238 end;
239
240 field.Name := 'type';
241 field.Type_ := TType.I32;
242 field.Id := 2;
243 oprot.WriteFieldBegin(field);
Jens Geyere0e32402016-04-20 21:50:48 +0200244 oprot.WriteI32(Integer(GetType));
Jake Farrell27274222011-11-10 20:32:44 +0000245 oprot.WriteFieldEnd();
246 oprot.WriteFieldStop();
247 oprot.WriteStructEnd();
248end;
249
Jens Geyere0e32402016-04-20 21:50:48 +0200250{ TApplicationExceptionSpecialized }
251
252constructor TApplicationExceptionSpecialized.Create(const Msg: string);
253begin
254 inherited HiddenCreate(Msg);
255end;
256
Jake Farrell27274222011-11-10 20:32:44 +0000257end.