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 | 9901069 | 2011-11-30 02:09:46 +0000 | [diff] [blame] | 28 | Version = '0.9.0-dev'; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 29 | |
| 30 | type |
| 31 | IProcessor = interface |
| 32 | ['{B1538A07-6CAC-4406-8A4C-AFED07C70A89}'] |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 33 | function Process( const iprot :IProtocol; const oprot: IProtocol): Boolean; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 34 | end; |
| 35 | |
| 36 | TApplicationException = class( SysUtils.Exception ) |
| 37 | public |
| 38 | type |
| 39 | {$SCOPEDENUMS ON} |
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 40 | TExceptionType = ( |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 41 | Unknown, |
| 42 | UnknownMethod, |
| 43 | InvalidMessageType, |
| 44 | WrongMethodName, |
| 45 | BadSequenceID, |
| 46 | MissingResult |
| 47 | ); |
| 48 | {$SCOPEDENUMS OFF} |
| 49 | private |
| 50 | FType : TExceptionType; |
| 51 | public |
| 52 | constructor Create; overload; |
| 53 | constructor Create( AType: TExceptionType); overload; |
| 54 | constructor Create( AType: TExceptionType; const msg: string); overload; |
| 55 | |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 56 | class function Read( const iprot: IProtocol): TApplicationException; |
| 57 | procedure Write( const oprot: IProtocol ); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 58 | end; |
| 59 | |
| 60 | // base class for IDL-generated exceptions |
| 61 | TException = class( SysUtils.Exception) |
| 62 | public |
Jake Farrell | ac10256 | 2011-11-23 14:30:41 +0000 | [diff] [blame] | 63 | function Message : string; // hide inherited property: allow read, but prevent accidental writes |
| 64 | procedure UpdateMessageProperty; // update inherited message property with toString() |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 65 | end; |
| 66 | |
| 67 | implementation |
| 68 | |
| 69 | { TException } |
| 70 | |
Jake Farrell | ac10256 | 2011-11-23 14:30:41 +0000 | [diff] [blame] | 71 | function TException.Message; |
| 72 | // allow read (exception summary), but prevent accidental writes |
| 73 | // read will return the exception summary |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 74 | begin |
Jake Farrell | ac10256 | 2011-11-23 14:30:41 +0000 | [diff] [blame] | 75 | result := Self.ToString; |
| 76 | end; |
| 77 | |
| 78 | procedure TException.UpdateMessageProperty; |
| 79 | // Update the inherited Message property to better conform to standard behaviour. |
| 80 | // Nice benefit: The IDE is now able to show the exception message again. |
| 81 | begin |
| 82 | inherited Message := Self.ToString; // produces a summary text |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 83 | end; |
| 84 | |
| 85 | { TApplicationException } |
| 86 | |
| 87 | constructor TApplicationException.Create; |
| 88 | begin |
| 89 | inherited Create( '' ); |
| 90 | end; |
| 91 | |
| 92 | constructor TApplicationException.Create(AType: TExceptionType; |
| 93 | const msg: string); |
| 94 | begin |
| 95 | inherited Create( msg ); |
| 96 | FType := AType; |
| 97 | end; |
| 98 | |
| 99 | constructor TApplicationException.Create(AType: TExceptionType); |
| 100 | begin |
| 101 | inherited Create(''); |
| 102 | FType := AType; |
| 103 | end; |
| 104 | |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 105 | class function TApplicationException.Read( const iprot: IProtocol): TApplicationException; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 106 | var |
| 107 | field : IField; |
| 108 | msg : string; |
| 109 | typ : TExceptionType; |
| 110 | begin |
| 111 | msg := ''; |
| 112 | typ := TExceptionType.Unknown; |
| 113 | while ( True ) do |
| 114 | begin |
| 115 | field := iprot.ReadFieldBegin; |
| 116 | if ( field.Type_ = TType.Stop) then |
| 117 | begin |
| 118 | Break; |
| 119 | end; |
| 120 | |
| 121 | case field.Id of |
| 122 | 1 : begin |
| 123 | if ( field.Type_ = TType.String_) then |
| 124 | begin |
| 125 | msg := iprot.ReadString; |
| 126 | end else |
| 127 | begin |
| 128 | TProtocolUtil.Skip( iprot, field.Type_ ); |
| 129 | end; |
| 130 | end; |
| 131 | |
| 132 | 2 : begin |
| 133 | if ( field.Type_ = TType.I32) then |
| 134 | begin |
| 135 | typ := TExceptionType( iprot.ReadI32 ); |
| 136 | end else |
| 137 | begin |
| 138 | TProtocolUtil.Skip( iprot, field.Type_ ); |
| 139 | end; |
| 140 | end else |
| 141 | begin |
| 142 | TProtocolUtil.Skip( iprot, field.Type_); |
| 143 | end; |
| 144 | end; |
| 145 | iprot.ReadFieldEnd; |
| 146 | end; |
| 147 | iprot.ReadStructEnd; |
| 148 | Result := TApplicationException.Create( typ, msg ); |
| 149 | end; |
| 150 | |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 151 | procedure TApplicationException.Write( const oprot: IProtocol); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 152 | var |
| 153 | struc : IStruct; |
| 154 | field : IField; |
| 155 | |
| 156 | begin |
| 157 | struc := TStructImpl.Create( 'TApplicationException' ); |
| 158 | field := TFieldImpl.Create; |
| 159 | |
| 160 | oprot.WriteStructBegin( struc ); |
| 161 | if Message <> '' then |
| 162 | begin |
| 163 | field.Name := 'message'; |
| 164 | field.Type_ := TType.String_; |
| 165 | field.Id := 1; |
| 166 | oprot.WriteFieldBegin( field ); |
| 167 | oprot.WriteString( Message ); |
| 168 | oprot.WriteFieldEnd; |
| 169 | end; |
| 170 | |
| 171 | field.Name := 'type'; |
| 172 | field.Type_ := TType.I32; |
| 173 | field.Id := 2; |
| 174 | oprot.WriteFieldBegin(field); |
| 175 | oprot.WriteI32(Integer(FType)); |
| 176 | oprot.WriteFieldEnd(); |
| 177 | oprot.WriteFieldStop(); |
| 178 | oprot.WriteStructEnd(); |
| 179 | end; |
| 180 | |
| 181 | end. |