Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 1 | (* |
| 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 | |
| 20 | unit Thrift; |
| 21 | |
| 22 | interface |
| 23 | |
| 24 | uses |
Jens Geyer | 606f1ef | 2018-04-09 23:09:41 +0200 | [diff] [blame] | 25 | SysUtils, |
| 26 | Thrift.Exception, |
| 27 | Thrift.Protocol; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 28 | |
| 29 | const |
Jens Geyer | e02559f | 2019-10-17 00:11:59 +0200 | [diff] [blame] | 30 | Version = '0.14.0'; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 31 | |
| 32 | type |
Jens Geyer | 606f1ef | 2018-04-09 23:09:41 +0200 | [diff] [blame] | 33 | TException = Thrift.Exception.TException; // compatibility alias |
| 34 | |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 35 | TApplicationExceptionSpecializedClass = class of TApplicationExceptionSpecialized; |
| 36 | |
Jens Geyer | 9f11c1e | 2019-11-09 19:39:20 +0100 | [diff] [blame] | 37 | TApplicationException = class abstract( TException) |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 38 | public |
| 39 | type |
| 40 | {$SCOPEDENUMS ON} |
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 41 | TExceptionType = ( |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 42 | Unknown, |
| 43 | UnknownMethod, |
| 44 | InvalidMessageType, |
| 45 | WrongMethodName, |
| 46 | BadSequenceID, |
Roger Meier | 0193149 | 2012-12-22 21:31:03 +0100 | [diff] [blame] | 47 | MissingResult, |
| 48 | InternalError, |
| 49 | ProtocolError, |
| 50 | InvalidTransform, |
| 51 | InvalidProtocol, |
| 52 | UnsupportedClientType |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 53 | ); |
| 54 | {$SCOPEDENUMS OFF} |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame^] | 55 | strict protected |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 56 | constructor HiddenCreate(const Msg: string); |
Jens Geyer | 9f11c1e | 2019-11-09 19:39:20 +0100 | [diff] [blame] | 57 | class function GetType: TExceptionType; virtual; abstract; |
| 58 | class function GetSpecializedExceptionType(AType: TExceptionType): TApplicationExceptionSpecializedClass; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 59 | public |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 60 | // 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 Geyer | 9f11c1e | 2019-11-09 19:39:20 +0100 | [diff] [blame] | 66 | property Type_: TExceptionType read GetType; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 67 | |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 68 | class function Read( const iprot: IProtocol): TApplicationException; |
| 69 | procedure Write( const oprot: IProtocol ); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 70 | end; |
| 71 | |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 72 | // Needed to remove deprecation warning |
| 73 | TApplicationExceptionSpecialized = class abstract (TApplicationException) |
| 74 | public |
| 75 | constructor Create(const Msg: string); |
| 76 | end; |
| 77 | |
Jens Geyer | 9f11c1e | 2019-11-09 19:39:20 +0100 | [diff] [blame] | 78 | TApplicationExceptionUnknown = class (TApplicationExceptionSpecialized) |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame^] | 79 | strict protected |
Jens Geyer | 9f11c1e | 2019-11-09 19:39:20 +0100 | [diff] [blame] | 80 | class function GetType: TApplicationException.TExceptionType; override; |
| 81 | end; |
| 82 | |
| 83 | TApplicationExceptionUnknownMethod = class (TApplicationExceptionSpecialized) |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame^] | 84 | strict protected |
Jens Geyer | 9f11c1e | 2019-11-09 19:39:20 +0100 | [diff] [blame] | 85 | class function GetType: TApplicationException.TExceptionType; override; |
| 86 | end; |
| 87 | |
| 88 | TApplicationExceptionInvalidMessageType = class (TApplicationExceptionSpecialized) |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame^] | 89 | strict protected |
Jens Geyer | 9f11c1e | 2019-11-09 19:39:20 +0100 | [diff] [blame] | 90 | class function GetType: TApplicationException.TExceptionType; override; |
| 91 | end; |
| 92 | |
| 93 | TApplicationExceptionWrongMethodName = class (TApplicationExceptionSpecialized) |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame^] | 94 | strict protected |
Jens Geyer | 9f11c1e | 2019-11-09 19:39:20 +0100 | [diff] [blame] | 95 | class function GetType: TApplicationException.TExceptionType; override; |
| 96 | end; |
| 97 | |
| 98 | TApplicationExceptionBadSequenceID = class (TApplicationExceptionSpecialized) |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame^] | 99 | strict protected |
Jens Geyer | 9f11c1e | 2019-11-09 19:39:20 +0100 | [diff] [blame] | 100 | class function GetType: TApplicationException.TExceptionType; override; |
| 101 | end; |
| 102 | |
| 103 | TApplicationExceptionMissingResult = class (TApplicationExceptionSpecialized) |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame^] | 104 | strict protected |
Jens Geyer | 9f11c1e | 2019-11-09 19:39:20 +0100 | [diff] [blame] | 105 | class function GetType: TApplicationException.TExceptionType; override; |
| 106 | end; |
| 107 | |
| 108 | TApplicationExceptionInternalError = class (TApplicationExceptionSpecialized) |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame^] | 109 | strict protected |
Jens Geyer | 9f11c1e | 2019-11-09 19:39:20 +0100 | [diff] [blame] | 110 | class function GetType: TApplicationException.TExceptionType; override; |
| 111 | end; |
| 112 | |
| 113 | TApplicationExceptionProtocolError = class (TApplicationExceptionSpecialized) |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame^] | 114 | strict protected |
Jens Geyer | 9f11c1e | 2019-11-09 19:39:20 +0100 | [diff] [blame] | 115 | class function GetType: TApplicationException.TExceptionType; override; |
| 116 | end; |
| 117 | |
| 118 | TApplicationExceptionInvalidTransform = class (TApplicationExceptionSpecialized) |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame^] | 119 | strict protected |
Jens Geyer | 9f11c1e | 2019-11-09 19:39:20 +0100 | [diff] [blame] | 120 | class function GetType: TApplicationException.TExceptionType; override; |
| 121 | end; |
| 122 | |
| 123 | TApplicationExceptionInvalidProtocol = class (TApplicationExceptionSpecialized) |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame^] | 124 | strict protected |
Jens Geyer | 9f11c1e | 2019-11-09 19:39:20 +0100 | [diff] [blame] | 125 | class function GetType: TApplicationException.TExceptionType; override; |
| 126 | end; |
| 127 | |
| 128 | TApplicationExceptionUnsupportedClientType = class (TApplicationExceptionSpecialized) |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame^] | 129 | strict protected |
Jens Geyer | 9f11c1e | 2019-11-09 19:39:20 +0100 | [diff] [blame] | 130 | class function GetType: TApplicationException.TExceptionType; override; |
| 131 | end; |
| 132 | |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 133 | |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 134 | |
| 135 | implementation |
| 136 | |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 137 | { TApplicationException } |
| 138 | |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 139 | constructor TApplicationException.HiddenCreate(const Msg: string); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 140 | begin |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 141 | inherited Create(Msg); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 142 | end; |
| 143 | |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 144 | class function TApplicationException.Create(const Msg: string): TApplicationException; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 145 | begin |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 146 | Result := TApplicationExceptionUnknown.Create(Msg); |
| 147 | end; |
| 148 | |
| 149 | class function TApplicationException.Create: TApplicationException; |
| 150 | begin |
| 151 | Result := TApplicationExceptionUnknown.Create(''); |
| 152 | end; |
| 153 | |
| 154 | class function TApplicationException.Create( AType: TExceptionType): TApplicationException; |
| 155 | begin |
| 156 | {$WARN SYMBOL_DEPRECATED OFF} |
| 157 | Result := Create(AType, ''); |
| 158 | {$WARN SYMBOL_DEPRECATED DEFAULT} |
| 159 | end; |
| 160 | |
| 161 | class function TApplicationException.Create( AType: TExceptionType; const msg: string): TApplicationException; |
| 162 | begin |
| 163 | Result := GetSpecializedExceptionType(AType).Create(msg); |
| 164 | end; |
| 165 | |
| 166 | class function TApplicationException.GetSpecializedExceptionType(AType: TExceptionType): TApplicationExceptionSpecializedClass; |
| 167 | begin |
| 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 Geyer | 9f11c1e | 2019-11-09 19:39:20 +0100 | [diff] [blame] | 180 | ASSERT( TExceptionType.Unknown = aType); |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 181 | Result := TApplicationExceptionUnknown; |
| 182 | end; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 183 | end; |
| 184 | |
Jens Geyer | 9f11c1e | 2019-11-09 19:39:20 +0100 | [diff] [blame] | 185 | |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 186 | class function TApplicationException.Read( const iprot: IProtocol): TApplicationException; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 187 | var |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 188 | field : TThriftField; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 189 | msg : string; |
| 190 | typ : TExceptionType; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 191 | struc : TThriftStruct; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 192 | begin |
| 193 | msg := ''; |
| 194 | typ := TExceptionType.Unknown; |
Jens Geyer | beb9377 | 2014-01-23 19:16:52 +0100 | [diff] [blame] | 195 | struc := iprot.ReadStructBegin; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 196 | 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 Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 231 | Result := GetSpecializedExceptionType(typ).Create(msg); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 232 | end; |
| 233 | |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 234 | procedure TApplicationException.Write( const oprot: IProtocol); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 235 | var |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 236 | struc : TThriftStruct; |
| 237 | field : TThriftField; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 238 | begin |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 239 | Init(struc, 'TApplicationException'); |
| 240 | Init(field); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 241 | |
| 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 Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 257 | oprot.WriteI32(Integer(GetType)); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 258 | oprot.WriteFieldEnd(); |
| 259 | oprot.WriteFieldStop(); |
| 260 | oprot.WriteStructEnd(); |
| 261 | end; |
| 262 | |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 263 | { TApplicationExceptionSpecialized } |
| 264 | |
| 265 | constructor TApplicationExceptionSpecialized.Create(const Msg: string); |
| 266 | begin |
| 267 | inherited HiddenCreate(Msg); |
| 268 | end; |
| 269 | |
Jens Geyer | 9f11c1e | 2019-11-09 19:39:20 +0100 | [diff] [blame] | 270 | { specialized TApplicationExceptions } |
| 271 | |
| 272 | class function TApplicationExceptionUnknownMethod.GetType : TApplicationException.TExceptionType; |
| 273 | begin |
| 274 | result := TExceptionType.UnknownMethod; |
| 275 | end; |
| 276 | |
| 277 | class function TApplicationExceptionInvalidMessageType.GetType : TApplicationException.TExceptionType; |
| 278 | begin |
| 279 | result := TExceptionType.InvalidMessageType; |
| 280 | end; |
| 281 | |
| 282 | class function TApplicationExceptionWrongMethodName.GetType : TApplicationException.TExceptionType; |
| 283 | begin |
| 284 | result := TExceptionType.WrongMethodName; |
| 285 | end; |
| 286 | |
| 287 | class function TApplicationExceptionBadSequenceID.GetType : TApplicationException.TExceptionType; |
| 288 | begin |
| 289 | result := TExceptionType.BadSequenceID; |
| 290 | end; |
| 291 | |
| 292 | class function TApplicationExceptionMissingResult.GetType : TApplicationException.TExceptionType; |
| 293 | begin |
| 294 | result := TExceptionType.MissingResult; |
| 295 | end; |
| 296 | |
| 297 | class function TApplicationExceptionInternalError.GetType : TApplicationException.TExceptionType; |
| 298 | begin |
| 299 | result := TExceptionType.InternalError; |
| 300 | end; |
| 301 | |
| 302 | class function TApplicationExceptionProtocolError.GetType : TApplicationException.TExceptionType; |
| 303 | begin |
| 304 | result := TExceptionType.ProtocolError; |
| 305 | end; |
| 306 | |
| 307 | class function TApplicationExceptionInvalidTransform.GetType : TApplicationException.TExceptionType; |
| 308 | begin |
| 309 | result := TExceptionType.InvalidTransform; |
| 310 | end; |
| 311 | |
| 312 | class function TApplicationExceptionInvalidProtocol.GetType : TApplicationException.TExceptionType; |
| 313 | begin |
| 314 | result := TExceptionType.InvalidProtocol; |
| 315 | end; |
| 316 | |
| 317 | class function TApplicationExceptionUnsupportedClientType.GetType : TApplicationException.TExceptionType; |
| 318 | begin |
| 319 | result := TExceptionType.UnsupportedClientType; |
| 320 | end; |
| 321 | |
| 322 | class function TApplicationExceptionUnknown.GetType : TApplicationException.TExceptionType; |
| 323 | begin |
| 324 | result := TExceptionType.Unknown; |
| 325 | end; |
| 326 | |
| 327 | |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 328 | end. |