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 |
| 25 | SysUtils, Thrift.Protocol; |
| 26 | |
| 27 | const |
Jake Farrell | 6fcecd4 | 2012-10-11 20:34:25 +0000 | [diff] [blame] | 28 | Version = '1.0.0-dev'; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 29 | |
| 30 | type |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 31 | TApplicationExceptionSpecializedClass = class of TApplicationExceptionSpecialized; |
| 32 | |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 33 | TApplicationException = class( SysUtils.Exception ) |
| 34 | public |
| 35 | type |
| 36 | {$SCOPEDENUMS ON} |
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 37 | TExceptionType = ( |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 38 | Unknown, |
| 39 | UnknownMethod, |
| 40 | InvalidMessageType, |
| 41 | WrongMethodName, |
| 42 | BadSequenceID, |
Roger Meier | 0193149 | 2012-12-22 21:31:03 +0100 | [diff] [blame] | 43 | MissingResult, |
| 44 | InternalError, |
| 45 | ProtocolError, |
| 46 | InvalidTransform, |
| 47 | InvalidProtocol, |
| 48 | UnsupportedClientType |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 49 | ); |
| 50 | {$SCOPEDENUMS OFF} |
| 51 | private |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 52 | function GetType: TExceptionType; |
| 53 | protected |
| 54 | constructor HiddenCreate(const Msg: string); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 55 | public |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 56 | // 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 Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 63 | |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 64 | class function Read( const iprot: IProtocol): TApplicationException; |
| 65 | procedure Write( const oprot: IProtocol ); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 66 | end; |
| 67 | |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 68 | // 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 Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 86 | // base class for IDL-generated exceptions |
| 87 | TException = class( SysUtils.Exception) |
| 88 | public |
Jake Farrell | ac10256 | 2011-11-23 14:30:41 +0000 | [diff] [blame] | 89 | function Message : string; // hide inherited property: allow read, but prevent accidental writes |
| 90 | procedure UpdateMessageProperty; // update inherited message property with toString() |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 91 | end; |
| 92 | |
| 93 | implementation |
| 94 | |
| 95 | { TException } |
| 96 | |
Jake Farrell | ac10256 | 2011-11-23 14:30:41 +0000 | [diff] [blame] | 97 | function TException.Message; |
| 98 | // allow read (exception summary), but prevent accidental writes |
| 99 | // read will return the exception summary |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 100 | begin |
Jake Farrell | ac10256 | 2011-11-23 14:30:41 +0000 | [diff] [blame] | 101 | result := Self.ToString; |
| 102 | end; |
| 103 | |
| 104 | procedure 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. |
| 107 | begin |
| 108 | inherited Message := Self.ToString; // produces a summary text |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 109 | end; |
| 110 | |
| 111 | { TApplicationException } |
| 112 | |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 113 | function TApplicationException.GetType: TExceptionType; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 114 | begin |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 115 | 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 Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 126 | end; |
| 127 | |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 128 | constructor TApplicationException.HiddenCreate(const Msg: string); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 129 | begin |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 130 | inherited Create(Msg); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 131 | end; |
| 132 | |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 133 | class function TApplicationException.Create(const Msg: string): TApplicationException; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 134 | begin |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 135 | Result := TApplicationExceptionUnknown.Create(Msg); |
| 136 | end; |
| 137 | |
| 138 | class function TApplicationException.Create: TApplicationException; |
| 139 | begin |
| 140 | Result := TApplicationExceptionUnknown.Create(''); |
| 141 | end; |
| 142 | |
| 143 | class function TApplicationException.Create( AType: TExceptionType): TApplicationException; |
| 144 | begin |
| 145 | {$WARN SYMBOL_DEPRECATED OFF} |
| 146 | Result := Create(AType, ''); |
| 147 | {$WARN SYMBOL_DEPRECATED DEFAULT} |
| 148 | end; |
| 149 | |
| 150 | class function TApplicationException.Create( AType: TExceptionType; const msg: string): TApplicationException; |
| 151 | begin |
| 152 | Result := GetSpecializedExceptionType(AType).Create(msg); |
| 153 | end; |
| 154 | |
| 155 | class function TApplicationException.GetSpecializedExceptionType(AType: TExceptionType): TApplicationExceptionSpecializedClass; |
| 156 | begin |
| 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 Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 171 | end; |
| 172 | |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 173 | class function TApplicationException.Read( const iprot: IProtocol): TApplicationException; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 174 | var |
| 175 | field : IField; |
| 176 | msg : string; |
| 177 | typ : TExceptionType; |
Jens Geyer | beb9377 | 2014-01-23 19:16:52 +0100 | [diff] [blame] | 178 | struc : IStruct; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 179 | begin |
| 180 | msg := ''; |
| 181 | typ := TExceptionType.Unknown; |
Jens Geyer | beb9377 | 2014-01-23 19:16:52 +0100 | [diff] [blame] | 182 | struc := iprot.ReadStructBegin; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 183 | 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 Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 218 | Result := GetSpecializedExceptionType(typ).Create(msg); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 219 | end; |
| 220 | |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 221 | procedure TApplicationException.Write( const oprot: IProtocol); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 222 | var |
| 223 | struc : IStruct; |
| 224 | field : IField; |
| 225 | |
| 226 | begin |
| 227 | struc := TStructImpl.Create( 'TApplicationException' ); |
| 228 | field := TFieldImpl.Create; |
| 229 | |
| 230 | oprot.WriteStructBegin( struc ); |
| 231 | if Message <> '' then |
| 232 | begin |
| 233 | field.Name := 'message'; |
| 234 | field.Type_ := TType.String_; |
| 235 | field.Id := 1; |
| 236 | oprot.WriteFieldBegin( field ); |
| 237 | oprot.WriteString( Message ); |
| 238 | oprot.WriteFieldEnd; |
| 239 | end; |
| 240 | |
| 241 | field.Name := 'type'; |
| 242 | field.Type_ := TType.I32; |
| 243 | field.Id := 2; |
| 244 | oprot.WriteFieldBegin(field); |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 245 | oprot.WriteI32(Integer(GetType)); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 246 | oprot.WriteFieldEnd(); |
| 247 | oprot.WriteFieldStop(); |
| 248 | oprot.WriteStructEnd(); |
| 249 | end; |
| 250 | |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 251 | { TApplicationExceptionSpecialized } |
| 252 | |
| 253 | constructor TApplicationExceptionSpecialized.Create(const Msg: string); |
| 254 | begin |
| 255 | inherited HiddenCreate(Msg); |
| 256 | end; |
| 257 | |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 258 | end. |