Skip to content

Commit

Permalink
THRIFT-4549 Thrift exceptions should derive from TException
Browse files Browse the repository at this point in the history
Client: Delphi
Patch: Jens Geyer
  • Loading branch information
Jens-G committed Apr 9, 2018
1 parent cc935b5 commit 606f1ef
Show file tree
Hide file tree
Showing 16 changed files with 336 additions and 282 deletions.
62 changes: 62 additions & 0 deletions lib/delphi/src/Thrift.Exception.pas
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
(*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*)

{$SCOPEDENUMS ON}

unit Thrift.Exception;

interface

uses
Classes, SysUtils;

type
// base class for all Thrift exceptions
TException = class( SysUtils.Exception)
public
function Message : string; // hide inherited property: allow read, but prevent accidental writes
procedure UpdateMessageProperty; // update inherited message property with toString()
end;




implementation

{ TException }

function TException.Message;
// allow read (exception summary), but prevent accidental writes
// read will return the exception summary
begin
result := Self.ToString;
end;

procedure TException.UpdateMessageProperty;
// Update the inherited Message property to better conform to standard behaviour.
// Nice benefit: The IDE is now able to show the exception message again.
begin
inherited Message := Self.ToString; // produces a summary text
end;




end.

3 changes: 2 additions & 1 deletion lib/delphi/src/Thrift.Protocol.pas
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ interface
Classes,
SysUtils,
Contnrs,
Thrift.Exception,
Thrift.Stream,
Thrift.Collections,
Thrift.Transport;
Expand Down Expand Up @@ -116,7 +117,7 @@ TThriftStringBuilder = class( TStringBuilder)
function Append(const Value: IThriftContainer): TStringBuilder; overload;
end;

TProtocolException = class( Exception )
TProtocolException = class( TException)
public
const // TODO(jensg): change into enum
UNKNOWN = 0;
Expand Down
3 changes: 2 additions & 1 deletion lib/delphi/src/Thrift.Transport.pas
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ interface
{$ENDIF}
{$ENDIF}
Thrift.Collections,
Thrift.Exception,
Thrift.Utils,
Thrift.Stream;

Expand Down Expand Up @@ -79,7 +80,7 @@ TTransportImpl = class( TInterfacedObject, ITransport)
procedure Flush; virtual;
end;

TTransportException = class( Exception )
TTransportException = class( TException)
public
type
TExceptionType = (
Expand Down
30 changes: 6 additions & 24 deletions lib/delphi/src/Thrift.pas
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,19 @@
interface

uses
SysUtils, Thrift.Protocol;
SysUtils,
Thrift.Exception,
Thrift.Protocol;

const
Version = '1.0.0-dev';

type
TException = Thrift.Exception.TException; // compatibility alias

TApplicationExceptionSpecializedClass = class of TApplicationExceptionSpecialized;

TApplicationException = class( SysUtils.Exception )
TApplicationException = class( TException)
public
type
{$SCOPEDENUMS ON}
Expand Down Expand Up @@ -83,31 +87,9 @@ TApplicationExceptionInvalidTransform = class (TApplicationExceptionSpecialize
TApplicationExceptionInvalidProtocol = class (TApplicationExceptionSpecialized);
TApplicationExceptionUnsupportedClientType = class (TApplicationExceptionSpecialized);

// base class for IDL-generated exceptions
TException = class( SysUtils.Exception)
public
function Message : string; // hide inherited property: allow read, but prevent accidental writes
procedure UpdateMessageProperty; // update inherited message property with toString()
end;

implementation

{ TException }

function TException.Message;
// allow read (exception summary), but prevent accidental writes
// read will return the exception summary
begin
result := Self.ToString;
end;

procedure TException.UpdateMessageProperty;
// Update the inherited Message property to better conform to standard behaviour.
// Nice benefit: The IDE is now able to show the exception message again.
begin
inherited Message := Self.ToString; // produces a summary text
end;

{ TApplicationException }

function TApplicationException.GetType: TExceptionType;
Expand Down
1 change: 1 addition & 0 deletions lib/delphi/test/client.dpr
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ uses
Thrift in '..\src\Thrift.pas',
Thrift.Transport in '..\src\Thrift.Transport.pas',
Thrift.Socket in '..\src\Thrift.Socket.pas',
Thrift.Exception in '..\src\Thrift.Exception.pas',
Thrift.Transport.Pipes in '..\src\Thrift.Transport.Pipes.pas',
Thrift.Protocol in '..\src\Thrift.Protocol.pas',
Thrift.Protocol.JSON in '..\src\Thrift.Protocol.JSON.pas',
Expand Down
1 change: 1 addition & 0 deletions lib/delphi/test/multiplexed/Multiplex.Test.Client.dpr
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ uses
Multiplex.Client.Main in 'Multiplex.Client.Main.pas',
Thrift in '..\..\src\Thrift.pas',
Thrift.Socket in '..\..\src\Thrift.Socket.pas',
Thrift.Exception in '..\..\src\Thrift.Exception.pas',
Thrift.Transport in '..\..\src\Thrift.Transport.pas',
Thrift.Transport.Pipes in '..\..\src\Thrift.Transport.Pipes.pas',
Thrift.Protocol in '..\..\src\Thrift.Protocol.pas',
Expand Down
1 change: 1 addition & 0 deletions lib/delphi/test/multiplexed/Multiplex.Test.Server.dpr
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ uses
Multiplex.Server.Main in 'Multiplex.Server.Main.pas',
ConsoleHelper in '..\ConsoleHelper.pas',
Thrift in '..\..\src\Thrift.pas',
Thrift.Exception in '..\..\src\Thrift.Exception.pas',
Thrift.Socket in '..\..\src\Thrift.Socket.pas',
Thrift.Transport in '..\..\src\Thrift.Transport.pas',
Thrift.Transport.Pipes in '..\..\src\Thrift.Transport.Pipes.pas',
Expand Down
1 change: 1 addition & 0 deletions lib/delphi/test/serializer/TestSerializer.dpr
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ program TestSerializer;
uses
Classes, Windows, SysUtils, Generics.Collections,
Thrift in '..\..\src\Thrift.pas',
Thrift.Exception in '..\..\src\Thrift.Exception.pas',
Thrift.Socket in '..\..\src\Thrift.Socket.pas',
Thrift.Transport in '..\..\src\Thrift.Transport.pas',
Thrift.Protocol in '..\..\src\Thrift.Protocol.pas',
Expand Down
1 change: 1 addition & 0 deletions lib/delphi/test/server.dpr
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ uses
TestServerEvents in 'TestServerEvents.pas',
Thrift.Test, // in gen-delphi folder
Thrift in '..\src\Thrift.pas',
Thrift.Exception in '..\src\Thrift.Exception.pas',
Thrift.Transport in '..\src\Thrift.Transport.pas',
Thrift.Socket in '..\src\Thrift.Socket.pas',
Thrift.Transport.Pipes in '..\src\Thrift.Transport.Pipes.pas',
Expand Down
1 change: 1 addition & 0 deletions lib/delphi/test/skip/skiptest_version1.dpr
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ uses
Classes, Windows, SysUtils,
Skiptest.One,
Thrift in '..\..\src\Thrift.pas',
Thrift.Exception in '..\..\src\Thrift.Exception.pas',
Thrift.Socket in '..\..\src\Thrift.Socket.pas',
Thrift.Transport in '..\..\src\Thrift.Transport.pas',
Thrift.Protocol in '..\..\src\Thrift.Protocol.pas',
Expand Down
1 change: 1 addition & 0 deletions lib/delphi/test/skip/skiptest_version2.dpr
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ uses
Classes, Windows, SysUtils,
Skiptest.Two,
Thrift in '..\..\src\Thrift.pas',
Thrift.Exception in '..\..\src\Thrift.Exception.pas',
Thrift.Socket in '..\..\src\Thrift.Socket.pas',
Thrift.Transport in '..\..\src\Thrift.Transport.pas',
Thrift.Protocol in '..\..\src\Thrift.Protocol.pas',
Expand Down
1 change: 1 addition & 0 deletions lib/delphi/test/typeregistry/TestTypeRegistry.dpr
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ uses
Classes, Windows, SysUtils, Generics.Collections, TypInfo,
Thrift in '..\..\src\Thrift.pas',
Thrift.Transport in '..\..\src\Thrift.Transport.pas',
Thrift.Exception in '..\..\src\Thrift.Exception.pas',
Thrift.Socket in '..\..\src\Thrift.Socket.pas',
Thrift.Protocol in '..\..\src\Thrift.Protocol.pas',
Thrift.Protocol.JSON in '..\..\src\Thrift.Protocol.JSON.pas',
Expand Down
20 changes: 10 additions & 10 deletions tutorial/delphi/DelphiClient/DelphiClient.dpr
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ uses
Generics.Collections,
Thrift in '..\..\..\lib\delphi\src\Thrift.pas',
Thrift.Collections in '..\..\..\lib\delphi\src\Thrift.Collections.pas',
Thrift.Console in '..\..\..\lib\delphi\src\Thrift.Console.pas',
Thrift.Exception in '..\..\..\lib\delphi\src\Thrift.Exception.pas',
Thrift.Utils in '..\..\..\lib\delphi\src\Thrift.Utils.pas',
Thrift.Stream in '..\..\..\lib\delphi\src\Thrift.Stream.pas',
Thrift.Protocol in '..\..\..\lib\delphi\src\Thrift.Protocol.pas',
Expand Down Expand Up @@ -62,10 +62,10 @@ begin
transport.Open;

client.ping;
Console.WriteLine('ping()');
WriteLn('ping()');

sum := client.add( 1, 1);
Console.WriteLine( Format( '1+1=%d', [sum]));
WriteLn( Format( '1+1=%d', [sum]));

work := TWorkImpl.Create;

Expand All @@ -74,32 +74,32 @@ begin
work.Num2 := 0;
try
quotient := client.calculate(1, work);
Console.WriteLine( 'Whoa we can divide by 0');
Console.WriteLine( Format('1/0=%d',[quotient]));
WriteLn( 'Whoa we can divide by 0');
WriteLn( Format('1/0=%d',[quotient]));
except
on io: TInvalidOperation
do Console.WriteLine( 'Invalid operation: ' + io.Why);
do WriteLn( 'Invalid operation: ' + io.Why);
end;

work.Op := TOperation.SUBTRACT;
work.Num1 := 15;
work.Num2 := 10;
try
diff := client.calculate( 1, work);
Console.WriteLine( Format('15-10=%d', [diff]));
WriteLn( Format('15-10=%d', [diff]));
except
on io: TInvalidOperation
do Console.WriteLine( 'Invalid operation: ' + io.Why);
do WriteLn( 'Invalid operation: ' + io.Why);
end;

log := client.getStruct(1);
Console.WriteLine( Format( 'Check log: %s', [log.Value]));
WriteLn( Format( 'Check log: %s', [log.Value]));

transport.Close();

except
on e : Exception
do Console.WriteLine( e.ClassName+': '+e.Message);
do WriteLn( e.ClassName+': '+e.Message);
end;
end;

Expand Down
Loading

0 comments on commit 606f1ef

Please sign in to comment.