blob: 65f23abf2cda99cae6e81f899719a5886e4231fb [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 Farrell6fcecd42012-10-11 20:34:25 +000028 Version = '1.0.0-dev';
Jake Farrell27274222011-11-10 20:32:44 +000029
30type
Jake Farrell27274222011-11-10 20:32:44 +000031 TApplicationException = class( SysUtils.Exception )
32 public
33 type
34{$SCOPEDENUMS ON}
Jake Farrell7ae13e12011-10-18 14:35:26 +000035 TExceptionType = (
Jake Farrell27274222011-11-10 20:32:44 +000036 Unknown,
37 UnknownMethod,
38 InvalidMessageType,
39 WrongMethodName,
40 BadSequenceID,
Roger Meier01931492012-12-22 21:31:03 +010041 MissingResult,
42 InternalError,
43 ProtocolError,
44 InvalidTransform,
45 InvalidProtocol,
46 UnsupportedClientType
Jake Farrell27274222011-11-10 20:32:44 +000047 );
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;
Jens Geyerbeb93772014-01-23 19:16:52 +0100110 struc : IStruct;
Jake Farrell27274222011-11-10 20:32:44 +0000111begin
112 msg := '';
113 typ := TExceptionType.Unknown;
Jens Geyerbeb93772014-01-23 19:16:52 +0100114 struc := iprot.ReadStructBegin;
Jake Farrell27274222011-11-10 20:32:44 +0000115 while ( True ) do
116 begin
117 field := iprot.ReadFieldBegin;
118 if ( field.Type_ = TType.Stop) then
119 begin
120 Break;
121 end;
122
123 case field.Id of
124 1 : begin
125 if ( field.Type_ = TType.String_) then
126 begin
127 msg := iprot.ReadString;
128 end else
129 begin
130 TProtocolUtil.Skip( iprot, field.Type_ );
131 end;
132 end;
133
134 2 : begin
135 if ( field.Type_ = TType.I32) then
136 begin
137 typ := TExceptionType( iprot.ReadI32 );
138 end else
139 begin
140 TProtocolUtil.Skip( iprot, field.Type_ );
141 end;
142 end else
143 begin
144 TProtocolUtil.Skip( iprot, field.Type_);
145 end;
146 end;
147 iprot.ReadFieldEnd;
148 end;
149 iprot.ReadStructEnd;
150 Result := TApplicationException.Create( typ, msg );
151end;
152
Roger Meier333bbf32012-01-08 21:51:08 +0000153procedure TApplicationException.Write( const oprot: IProtocol);
Jake Farrell27274222011-11-10 20:32:44 +0000154var
155 struc : IStruct;
156 field : IField;
157
158begin
159 struc := TStructImpl.Create( 'TApplicationException' );
160 field := TFieldImpl.Create;
161
162 oprot.WriteStructBegin( struc );
163 if Message <> '' then
164 begin
165 field.Name := 'message';
166 field.Type_ := TType.String_;
167 field.Id := 1;
168 oprot.WriteFieldBegin( field );
169 oprot.WriteString( Message );
170 oprot.WriteFieldEnd;
171 end;
172
173 field.Name := 'type';
174 field.Type_ := TType.I32;
175 field.Id := 2;
176 oprot.WriteFieldBegin(field);
177 oprot.WriteI32(Integer(FType));
178 oprot.WriteFieldEnd();
179 oprot.WriteFieldStop();
180 oprot.WriteStructEnd();
181end;
182
183end.