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 |
|
| 57 | implementation
|
| 58 |
|
| 59 | { TApplicationException }
|
| 60 |
|
| 61 | constructor TApplicationException.Create;
|
| 62 | begin
|
| 63 | inherited Create( '' );
|
| 64 | end;
|
| 65 |
|
| 66 | constructor TApplicationException.Create(AType: TExceptionType;
|
| 67 | const msg: string);
|
| 68 | begin
|
| 69 | inherited Create( msg );
|
| 70 | FType := AType;
|
| 71 | end;
|
| 72 |
|
| 73 | constructor TApplicationException.Create(AType: TExceptionType);
|
| 74 | begin
|
| 75 | inherited Create('');
|
| 76 | FType := AType;
|
| 77 | end;
|
| 78 |
|
| 79 | class function TApplicationException.Read(
|
| 80 | iprot: IProtocol): TApplicationException;
|
| 81 | var
|
| 82 | field : IField;
|
| 83 | msg : string;
|
| 84 | typ : TExceptionType;
|
| 85 | begin
|
| 86 | msg := '';
|
| 87 | typ := TExceptionType.Unknown;
|
| 88 | while ( True ) do
|
| 89 | begin
|
| 90 | field := iprot.ReadFieldBegin;
|
| 91 | if ( field.Type_ = TType.Stop) then
|
| 92 | begin
|
| 93 | Break;
|
| 94 | end;
|
| 95 |
|
| 96 | case field.Id of
|
| 97 | 1 : begin
|
| 98 | if ( field.Type_ = TType.String_) then
|
| 99 | begin
|
| 100 | msg := iprot.ReadString;
|
| 101 | end else
|
| 102 | begin
|
| 103 | TProtocolUtil.Skip( iprot, field.Type_ );
|
| 104 | end;
|
| 105 | end;
|
| 106 |
|
| 107 | 2 : begin
|
| 108 | if ( field.Type_ = TType.I32) then
|
| 109 | begin
|
| 110 | typ := TExceptionType( iprot.ReadI32 );
|
| 111 | end else
|
| 112 | begin
|
| 113 | TProtocolUtil.Skip( iprot, field.Type_ );
|
| 114 | end;
|
| 115 | end else
|
| 116 | begin
|
| 117 | TProtocolUtil.Skip( iprot, field.Type_);
|
| 118 | end;
|
| 119 | end;
|
| 120 | iprot.ReadFieldEnd;
|
| 121 | end;
|
| 122 | iprot.ReadStructEnd;
|
| 123 | Result := TApplicationException.Create( typ, msg );
|
| 124 | end;
|
| 125 |
|
| 126 | procedure TApplicationException.Write(oprot: IProtocol);
|
| 127 | var
|
| 128 | struc : IStruct;
|
| 129 | field : IField;
|
| 130 |
|
| 131 | begin
|
| 132 | struc := TStructImpl.Create( 'TApplicationException' );
|
| 133 | field := TFieldImpl.Create;
|
| 134 |
|
| 135 | oprot.WriteStructBegin( struc );
|
| 136 | if Message <> '' then
|
| 137 | begin
|
| 138 | field.Name := 'message';
|
| 139 | field.Type_ := TType.String_;
|
| 140 | field.Id := 1;
|
| 141 | oprot.WriteFieldBegin( field );
|
| 142 | oprot.WriteString( Message );
|
| 143 | oprot.WriteFieldEnd;
|
| 144 | end;
|
| 145 |
|
| 146 | field.Name := 'type';
|
| 147 | field.Type_ := TType.I32;
|
| 148 | field.Id := 2;
|
| 149 | oprot.WriteFieldBegin(field);
|
| 150 | oprot.WriteI32(Integer(FType));
|
| 151 | oprot.WriteFieldEnd();
|
| 152 | oprot.WriteFieldStop();
|
| 153 | oprot.WriteStructEnd();
|
| 154 | end;
|
| 155 |
|
| 156 | end.
|