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