blob: 68e1f7de46cf3b68d199f65e15063bc523b1a7a8 [file] [log] [blame]
Jake Farrell27274222011-11-10 20:32:44 +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
27const
Jake Farrellc6c01f22012-10-11 20:22:24 +000028 Version = '0.9.0';
Jake Farrell27274222011-11-10 20:32:44 +000029
30type
31 IProcessor = interface
32 ['{B1538A07-6CAC-4406-8A4C-AFED07C70A89}']
Roger Meier333bbf32012-01-08 21:51:08 +000033 function Process( const iprot :IProtocol; const oprot: IProtocol): Boolean;
Jake Farrell27274222011-11-10 20:32:44 +000034 end;
35
36 TApplicationException = class( SysUtils.Exception )
37 public
38 type
39{$SCOPEDENUMS ON}
Jake Farrell7ae13e12011-10-18 14:35:26 +000040 TExceptionType = (
Jake Farrell27274222011-11-10 20:32:44 +000041 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 Meier333bbf32012-01-08 21:51:08 +000056 class function Read( const iprot: IProtocol): TApplicationException;
57 procedure Write( const oprot: IProtocol );
Jake Farrell27274222011-11-10 20:32:44 +000058 end;
59
60 // base class for IDL-generated exceptions
61 TException = class( SysUtils.Exception)
62 public
Jake Farrellac102562011-11-23 14:30:41 +000063 function Message : string; // hide inherited property: allow read, but prevent accidental writes
64 procedure UpdateMessageProperty; // update inherited message property with toString()
Jake Farrell27274222011-11-10 20:32:44 +000065 end;
66
67implementation
68
69{ TException }
70
Jake Farrellac102562011-11-23 14:30:41 +000071function TException.Message;
72// allow read (exception summary), but prevent accidental writes
73// read will return the exception summary
Jake Farrell27274222011-11-10 20:32:44 +000074begin
Jake Farrellac102562011-11-23 14:30:41 +000075 result := Self.ToString;
76end;
77
78procedure 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.
81begin
82 inherited Message := Self.ToString; // produces a summary text
Jake Farrell27274222011-11-10 20:32:44 +000083end;
84
85{ TApplicationException }
86
87constructor TApplicationException.Create;
88begin
89 inherited Create( '' );
90end;
91
92constructor TApplicationException.Create(AType: TExceptionType;
93 const msg: string);
94begin
95 inherited Create( msg );
96 FType := AType;
97end;
98
99constructor TApplicationException.Create(AType: TExceptionType);
100begin
101 inherited Create('');
102 FType := AType;
103end;
104
Roger Meier333bbf32012-01-08 21:51:08 +0000105class function TApplicationException.Read( const iprot: IProtocol): TApplicationException;
Jake Farrell27274222011-11-10 20:32:44 +0000106var
107 field : IField;
108 msg : string;
109 typ : TExceptionType;
110begin
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 );
149end;
150
Roger Meier333bbf32012-01-08 21:51:08 +0000151procedure TApplicationException.Write( const oprot: IProtocol);
Jake Farrell27274222011-11-10 20:32:44 +0000152var
153 struc : IStruct;
154 field : IField;
155
156begin
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();
179end;
180
181end.