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 |
Jake Farrell | 6fcecd4 | 2012-10-11 20:34:25 +0000 | [diff] [blame] | 30 | Version = '1.0.0-dev'; |
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 | 606f1ef | 2018-04-09 23:09:41 +0200 | [diff] [blame^] | 37 | TApplicationException = class( 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} |
| 55 | private |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 56 | function GetType: TExceptionType; |
| 57 | protected |
| 58 | constructor HiddenCreate(const Msg: string); |
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 | |
| 66 | class function GetSpecializedExceptionType(AType: TExceptionType): TApplicationExceptionSpecializedClass; |
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 | |
| 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 Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 90 | |
| 91 | implementation |
| 92 | |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 93 | { TApplicationException } |
| 94 | |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 95 | function TApplicationException.GetType: TExceptionType; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 96 | begin |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 97 | 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 Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 108 | end; |
| 109 | |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 110 | constructor TApplicationException.HiddenCreate(const Msg: string); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 111 | begin |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 112 | inherited Create(Msg); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 113 | end; |
| 114 | |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 115 | class function TApplicationException.Create(const Msg: string): TApplicationException; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 116 | begin |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 117 | Result := TApplicationExceptionUnknown.Create(Msg); |
| 118 | end; |
| 119 | |
| 120 | class function TApplicationException.Create: TApplicationException; |
| 121 | begin |
| 122 | Result := TApplicationExceptionUnknown.Create(''); |
| 123 | end; |
| 124 | |
| 125 | class function TApplicationException.Create( AType: TExceptionType): TApplicationException; |
| 126 | begin |
| 127 | {$WARN SYMBOL_DEPRECATED OFF} |
| 128 | Result := Create(AType, ''); |
| 129 | {$WARN SYMBOL_DEPRECATED DEFAULT} |
| 130 | end; |
| 131 | |
| 132 | class function TApplicationException.Create( AType: TExceptionType; const msg: string): TApplicationException; |
| 133 | begin |
| 134 | Result := GetSpecializedExceptionType(AType).Create(msg); |
| 135 | end; |
| 136 | |
| 137 | class function TApplicationException.GetSpecializedExceptionType(AType: TExceptionType): TApplicationExceptionSpecializedClass; |
| 138 | begin |
| 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 Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 153 | end; |
| 154 | |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 155 | class function TApplicationException.Read( const iprot: IProtocol): TApplicationException; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 156 | var |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 157 | field : TThriftField; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 158 | msg : string; |
| 159 | typ : TExceptionType; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 160 | struc : TThriftStruct; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 161 | begin |
| 162 | msg := ''; |
| 163 | typ := TExceptionType.Unknown; |
Jens Geyer | beb9377 | 2014-01-23 19:16:52 +0100 | [diff] [blame] | 164 | struc := iprot.ReadStructBegin; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 165 | 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 Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 200 | Result := GetSpecializedExceptionType(typ).Create(msg); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 201 | end; |
| 202 | |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 203 | procedure TApplicationException.Write( const oprot: IProtocol); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 204 | var |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 205 | struc : TThriftStruct; |
| 206 | field : TThriftField; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 207 | begin |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 208 | Init(struc, 'TApplicationException'); |
| 209 | Init(field); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 210 | |
| 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 Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 226 | oprot.WriteI32(Integer(GetType)); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 227 | oprot.WriteFieldEnd(); |
| 228 | oprot.WriteFieldStop(); |
| 229 | oprot.WriteStructEnd(); |
| 230 | end; |
| 231 | |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 232 | { TApplicationExceptionSpecialized } |
| 233 | |
| 234 | constructor TApplicationExceptionSpecialized.Create(const Msg: string); |
| 235 | begin |
| 236 | inherited HiddenCreate(Msg); |
| 237 | end; |
| 238 | |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 239 | end. |