blob: ddd4c11f976eb6de47c72d41e61352040cb7f3df [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
Jake Farrell6fcecd42012-10-11 20:34:25 +000030 Version = '1.0.0-dev';
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 Geyer606f1ef2018-04-09 23:09:41 +020037 TApplicationException = class( 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}
55 private
Jens Geyere0e32402016-04-20 21:50:48 +020056 function GetType: TExceptionType;
57 protected
58 constructor HiddenCreate(const Msg: string);
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
66 class function GetSpecializedExceptionType(AType: TExceptionType): TApplicationExceptionSpecializedClass;
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
78 TApplicationExceptionUnknown = class (TApplicationExceptionSpecialized);
79 TApplicationExceptionUnknownMethod = class (TApplicationExceptionSpecialized);
80 TApplicationExceptionInvalidMessageType = class (TApplicationExceptionSpecialized);
81 TApplicationExceptionWrongMethodName = class (TApplicationExceptionSpecialized);
82 TApplicationExceptionBadSequenceID = class (TApplicationExceptionSpecialized);
83 TApplicationExceptionMissingResult = class (TApplicationExceptionSpecialized);
84 TApplicationExceptionInternalError = class (TApplicationExceptionSpecialized);
85 TApplicationExceptionProtocolError = class (TApplicationExceptionSpecialized);
86 TApplicationExceptionInvalidTransform = class (TApplicationExceptionSpecialized);
87 TApplicationExceptionInvalidProtocol = class (TApplicationExceptionSpecialized);
88 TApplicationExceptionUnsupportedClientType = class (TApplicationExceptionSpecialized);
89
Jake Farrell27274222011-11-10 20:32:44 +000090
91implementation
92
Jake Farrell27274222011-11-10 20:32:44 +000093{ TApplicationException }
94
Jens Geyere0e32402016-04-20 21:50:48 +020095function TApplicationException.GetType: TExceptionType;
Jake Farrell27274222011-11-10 20:32:44 +000096begin
Jens Geyere0e32402016-04-20 21:50:48 +020097 if Self is TApplicationExceptionUnknownMethod then Result := TExceptionType.UnknownMethod
98 else if Self is TApplicationExceptionInvalidMessageType then Result := TExceptionType.InvalidMessageType
99 else if Self is TApplicationExceptionWrongMethodName then Result := TExceptionType.WrongMethodName
100 else if Self is TApplicationExceptionBadSequenceID then Result := TExceptionType.BadSequenceID
101 else if Self is TApplicationExceptionMissingResult then Result := TExceptionType.MissingResult
102 else if Self is TApplicationExceptionInternalError then Result := TExceptionType.InternalError
103 else if Self is TApplicationExceptionProtocolError then Result := TExceptionType.ProtocolError
104 else if Self is TApplicationExceptionInvalidTransform then Result := TExceptionType.InvalidTransform
105 else if Self is TApplicationExceptionInvalidProtocol then Result := TExceptionType.InvalidProtocol
106 else if Self is TApplicationExceptionUnsupportedClientType then Result := TExceptionType.UnsupportedClientType
107 else Result := TExceptionType.Unknown;
Jake Farrell27274222011-11-10 20:32:44 +0000108end;
109
Jens Geyere0e32402016-04-20 21:50:48 +0200110constructor TApplicationException.HiddenCreate(const Msg: string);
Jake Farrell27274222011-11-10 20:32:44 +0000111begin
Jens Geyere0e32402016-04-20 21:50:48 +0200112 inherited Create(Msg);
Jake Farrell27274222011-11-10 20:32:44 +0000113end;
114
Jens Geyere0e32402016-04-20 21:50:48 +0200115class function TApplicationException.Create(const Msg: string): TApplicationException;
Jake Farrell27274222011-11-10 20:32:44 +0000116begin
Jens Geyere0e32402016-04-20 21:50:48 +0200117 Result := TApplicationExceptionUnknown.Create(Msg);
118end;
119
120class function TApplicationException.Create: TApplicationException;
121begin
122 Result := TApplicationExceptionUnknown.Create('');
123end;
124
125class function TApplicationException.Create( AType: TExceptionType): TApplicationException;
126begin
127{$WARN SYMBOL_DEPRECATED OFF}
128 Result := Create(AType, '');
129{$WARN SYMBOL_DEPRECATED DEFAULT}
130end;
131
132class function TApplicationException.Create( AType: TExceptionType; const msg: string): TApplicationException;
133begin
134 Result := GetSpecializedExceptionType(AType).Create(msg);
135end;
136
137class function TApplicationException.GetSpecializedExceptionType(AType: TExceptionType): TApplicationExceptionSpecializedClass;
138begin
139 case AType of
140 TExceptionType.UnknownMethod: Result := TApplicationExceptionUnknownMethod;
141 TExceptionType.InvalidMessageType: Result := TApplicationExceptionInvalidMessageType;
142 TExceptionType.WrongMethodName: Result := TApplicationExceptionWrongMethodName;
143 TExceptionType.BadSequenceID: Result := TApplicationExceptionBadSequenceID;
144 TExceptionType.MissingResult: Result := TApplicationExceptionMissingResult;
145 TExceptionType.InternalError: Result := TApplicationExceptionInternalError;
146 TExceptionType.ProtocolError: Result := TApplicationExceptionProtocolError;
147 TExceptionType.InvalidTransform: Result := TApplicationExceptionInvalidTransform;
148 TExceptionType.InvalidProtocol: Result := TApplicationExceptionInvalidProtocol;
149 TExceptionType.UnsupportedClientType: Result := TApplicationExceptionUnsupportedClientType;
150 else
151 Result := TApplicationExceptionUnknown;
152 end;
Jake Farrell27274222011-11-10 20:32:44 +0000153end;
154
Roger Meier333bbf32012-01-08 21:51:08 +0000155class function TApplicationException.Read( const iprot: IProtocol): TApplicationException;
Jake Farrell27274222011-11-10 20:32:44 +0000156var
Jens Geyer17c3ad92017-09-05 20:31:27 +0200157 field : TThriftField;
Jake Farrell27274222011-11-10 20:32:44 +0000158 msg : string;
159 typ : TExceptionType;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200160 struc : TThriftStruct;
Jake Farrell27274222011-11-10 20:32:44 +0000161begin
162 msg := '';
163 typ := TExceptionType.Unknown;
Jens Geyerbeb93772014-01-23 19:16:52 +0100164 struc := iprot.ReadStructBegin;
Jake Farrell27274222011-11-10 20:32:44 +0000165 while ( True ) do
166 begin
167 field := iprot.ReadFieldBegin;
168 if ( field.Type_ = TType.Stop) then
169 begin
170 Break;
171 end;
172
173 case field.Id of
174 1 : begin
175 if ( field.Type_ = TType.String_) then
176 begin
177 msg := iprot.ReadString;
178 end else
179 begin
180 TProtocolUtil.Skip( iprot, field.Type_ );
181 end;
182 end;
183
184 2 : begin
185 if ( field.Type_ = TType.I32) then
186 begin
187 typ := TExceptionType( iprot.ReadI32 );
188 end else
189 begin
190 TProtocolUtil.Skip( iprot, field.Type_ );
191 end;
192 end else
193 begin
194 TProtocolUtil.Skip( iprot, field.Type_);
195 end;
196 end;
197 iprot.ReadFieldEnd;
198 end;
199 iprot.ReadStructEnd;
Jens Geyere0e32402016-04-20 21:50:48 +0200200 Result := GetSpecializedExceptionType(typ).Create(msg);
Jake Farrell27274222011-11-10 20:32:44 +0000201end;
202
Roger Meier333bbf32012-01-08 21:51:08 +0000203procedure TApplicationException.Write( const oprot: IProtocol);
Jake Farrell27274222011-11-10 20:32:44 +0000204var
Jens Geyer17c3ad92017-09-05 20:31:27 +0200205 struc : TThriftStruct;
206 field : TThriftField;
Jake Farrell27274222011-11-10 20:32:44 +0000207begin
Jens Geyer17c3ad92017-09-05 20:31:27 +0200208 Init(struc, 'TApplicationException');
209 Init(field);
Jake Farrell27274222011-11-10 20:32:44 +0000210
211 oprot.WriteStructBegin( struc );
212 if Message <> '' then
213 begin
214 field.Name := 'message';
215 field.Type_ := TType.String_;
216 field.Id := 1;
217 oprot.WriteFieldBegin( field );
218 oprot.WriteString( Message );
219 oprot.WriteFieldEnd;
220 end;
221
222 field.Name := 'type';
223 field.Type_ := TType.I32;
224 field.Id := 2;
225 oprot.WriteFieldBegin(field);
Jens Geyere0e32402016-04-20 21:50:48 +0200226 oprot.WriteI32(Integer(GetType));
Jake Farrell27274222011-11-10 20:32:44 +0000227 oprot.WriteFieldEnd();
228 oprot.WriteFieldStop();
229 oprot.WriteStructEnd();
230end;
231
Jens Geyere0e32402016-04-20 21:50:48 +0200232{ TApplicationExceptionSpecialized }
233
234constructor TApplicationExceptionSpecialized.Create(const Msg: string);
235begin
236 inherited HiddenCreate(Msg);
237end;
238
Jake Farrell27274222011-11-10 20:32:44 +0000239end.