blob: 48c3d4727a64e8eb9c83cc57b4912a8e5aa09ae5 [file] [log] [blame]
Jake Farrell7ae13e12011-10-18 14:35:26 +00001(*
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
20unit Thrift;
21
22interface
23
24uses
25 SysUtils, Thrift.Protocol;
26
27type
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 Farrell73a921f2011-10-31 14:07:14 +000057 // 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 Farrell7ae13e12011-10-18 14:35:26 +000063implementation
64
Jake Farrell73a921f2011-10-31 14:07:14 +000065{ TException }
66
67procedure TException.Message;
68// hide inherited property to prevent accidental read/write
69begin
70 ASSERT( FALSE, 'Unexpected call to '+ClassName+'.message. Forgot the underscore?');
71end;
72
Jake Farrell7ae13e12011-10-18 14:35:26 +000073{ TApplicationException }
74
75constructor TApplicationException.Create;
76begin
77 inherited Create( '' );
78end;
79
80constructor TApplicationException.Create(AType: TExceptionType;
81 const msg: string);
82begin
83 inherited Create( msg );
84 FType := AType;
85end;
86
87constructor TApplicationException.Create(AType: TExceptionType);
88begin
89 inherited Create('');
90 FType := AType;
91end;
92
93class function TApplicationException.Read(
94 iprot: IProtocol): TApplicationException;
95var
96 field : IField;
97 msg : string;
98 typ : TExceptionType;
99begin
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 );
138end;
139
140procedure TApplicationException.Write(oprot: IProtocol);
141var
142 struc : IStruct;
143 field : IField;
144
145begin
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();
168end;
169
170end.