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 |
| 28 | Version = '0.8.0-dev'; |
| 29 | |
| 30 | type |
| 31 | IProcessor = interface |
| 32 | ['{B1538A07-6CAC-4406-8A4C-AFED07C70A89}'] |
| 33 | function Process( iprot :IProtocol; oprot: IProtocol): Boolean; |
| 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 | |
| 56 | class function Read( iprot: IProtocol): TApplicationException; |
| 57 | procedure Write( oprot: IProtocol ); |
| 58 | end; |
| 59 | |
| 60 | // base class for IDL-generated exceptions |
| 61 | TException = class( SysUtils.Exception) |
| 62 | public |
| 63 | procedure Message; // hide inherited property to prevent accidental read/write |
| 64 | end; |
| 65 | |
| 66 | implementation |
| 67 | |
| 68 | { TException } |
| 69 | |
| 70 | procedure TException.Message; |
| 71 | // hide inherited property to prevent accidental read/write |
| 72 | begin |
| 73 | ASSERT( FALSE, 'Unexpected call to '+ClassName+'.message. Forgot the underscore?'); |
| 74 | end; |
| 75 | |
| 76 | { TApplicationException } |
| 77 | |
| 78 | constructor TApplicationException.Create; |
| 79 | begin |
| 80 | inherited Create( '' ); |
| 81 | end; |
| 82 | |
| 83 | constructor TApplicationException.Create(AType: TExceptionType; |
| 84 | const msg: string); |
| 85 | begin |
| 86 | inherited Create( msg ); |
| 87 | FType := AType; |
| 88 | end; |
| 89 | |
| 90 | constructor TApplicationException.Create(AType: TExceptionType); |
| 91 | begin |
| 92 | inherited Create(''); |
| 93 | FType := AType; |
| 94 | end; |
| 95 | |
| 96 | class function TApplicationException.Read( |
| 97 | iprot: IProtocol): TApplicationException; |
| 98 | var |
| 99 | field : IField; |
| 100 | msg : string; |
| 101 | typ : TExceptionType; |
| 102 | begin |
| 103 | msg := ''; |
| 104 | typ := TExceptionType.Unknown; |
| 105 | while ( True ) do |
| 106 | begin |
| 107 | field := iprot.ReadFieldBegin; |
| 108 | if ( field.Type_ = TType.Stop) then |
| 109 | begin |
| 110 | Break; |
| 111 | end; |
| 112 | |
| 113 | case field.Id of |
| 114 | 1 : begin |
| 115 | if ( field.Type_ = TType.String_) then |
| 116 | begin |
| 117 | msg := iprot.ReadString; |
| 118 | end else |
| 119 | begin |
| 120 | TProtocolUtil.Skip( iprot, field.Type_ ); |
| 121 | end; |
| 122 | end; |
| 123 | |
| 124 | 2 : begin |
| 125 | if ( field.Type_ = TType.I32) then |
| 126 | begin |
| 127 | typ := TExceptionType( iprot.ReadI32 ); |
| 128 | end else |
| 129 | begin |
| 130 | TProtocolUtil.Skip( iprot, field.Type_ ); |
| 131 | end; |
| 132 | end else |
| 133 | begin |
| 134 | TProtocolUtil.Skip( iprot, field.Type_); |
| 135 | end; |
| 136 | end; |
| 137 | iprot.ReadFieldEnd; |
| 138 | end; |
| 139 | iprot.ReadStructEnd; |
| 140 | Result := TApplicationException.Create( typ, msg ); |
| 141 | end; |
| 142 | |
| 143 | procedure TApplicationException.Write(oprot: IProtocol); |
| 144 | var |
| 145 | struc : IStruct; |
| 146 | field : IField; |
| 147 | |
| 148 | begin |
| 149 | struc := TStructImpl.Create( 'TApplicationException' ); |
| 150 | field := TFieldImpl.Create; |
| 151 | |
| 152 | oprot.WriteStructBegin( struc ); |
| 153 | if Message <> '' then |
| 154 | begin |
| 155 | field.Name := 'message'; |
| 156 | field.Type_ := TType.String_; |
| 157 | field.Id := 1; |
| 158 | oprot.WriteFieldBegin( field ); |
| 159 | oprot.WriteString( Message ); |
| 160 | oprot.WriteFieldEnd; |
| 161 | end; |
| 162 | |
| 163 | field.Name := 'type'; |
| 164 | field.Type_ := TType.I32; |
| 165 | field.Id := 2; |
| 166 | oprot.WriteFieldBegin(field); |
| 167 | oprot.WriteI32(Integer(FType)); |
| 168 | oprot.WriteFieldEnd(); |
| 169 | oprot.WriteFieldStop(); |
| 170 | oprot.WriteStructEnd(); |
| 171 | end; |
| 172 | |
| 173 | end. |