Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Exception handling in Datasnap REST Service



Permlink Replies: 3 - Last Post: Oct 3, 2017 1:59 PM Last Post By: Ederson Selvati
Jason Sweby

Posts: 46
Registered: 5/20/00
Exception handling in Datasnap REST Service
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 29, 2015 11:59 PM
Hi,

I'm struggling with exception handling inside my Datasnap REST Service (XE3 but also tried with 10 Seattle). I have written half a dozen Windows Service over the years and I always include a TApplicationEvents component so that I can log any Application Exceptions to the Windows Event Log

However, this behaviour is not happening with a Datasnap Service. The TApplicationEvents.OnException event never gets fired, so I assume something else is eating the exception and handling it before it gets here.

The exception is displayed in the web service method's result, which is fine, but I'd also like to catch it before then so that I might be able to handle different exceptions.

The only consistent way I have managed so far is to wrap each method in a try..except block, and handle the exception in each method, before reraising the exception. However, with Web Service of 20 methods and growing, this isn't really going to scale up.

I have also tried implementing the OnError, OnTrace and other events of the some of the Datasnap components (DSServer, HTTPTransport, TCPTransport, etc.) but these never seem to get fired either.

Has anyone come across anything like this, please?
Dave Nottage

Posts: 1,850
Registered: 1/7/00
Re: Exception handling in Datasnap REST Service
Click to report abuse...   Click to reply to this thread Reply
  Posted: Dec 26, 2015 4:37 PM   in response to: Jason Sweby in response to: Jason Sweby
I'm struggling with exception handling inside my Datasnap REST Service (XE3 but also tried with 10 Seattle). I have written half a dozen Windows Service over the years and I always include a TApplicationEvents component so that I can log any Application Exceptions to the Windows Event Log

I had precisely the same issue. My solution was to rewrite the TRESTExecutionThread class (from the REST.Client unit), because it doesn't handle exceptions.

type
  TThreadCompleteHandler = reference to procedure(const AMessage: string);
 
  TRESTThread = class(TThread)
  private
    FCompletionHandler: TThreadCompleteHandler;
    FMessage: string;
    FRequest: TCustomRESTRequest;
    FSuccess: Boolean;
  protected
    procedure HandleCompletion;
    procedure Execute; override;
  public
    constructor Create(ARequest: TCustomRESTRequest; ACompletionHandler: TThreadCompleteHandler);
  end;


type
  TOpenCustomRESTRequest = class(TCustomRESTRequest); // Provides access to protected methods
 
{ TRESTThread }
 
constructor TRESTThread.Create(ARequest: TCustomRESTRequest; ACompletionHandler: TThreadCompleteHandler);
begin
  inherited Create(False);
  FreeOnTerminate := True;
  FCompletionHandler := ACompletionHandler;
  FRequest := ARequest;
end;
 
procedure TRESTThread.Execute;
begin
  FMessage := '';
  FSuccess := False;
  try
    FRequest.Execute;
    FSuccess := True;
    Synchronize(HandleCompletion);
  except
    on E: Exception do
    begin
      FMessage := E.Message;
      Synchronize(HandleCompletion);
    end;
  end;
end;
 
procedure TRESTThread.HandleCompletion;
begin
  if Assigned(FCompletionHandler) then
    FCompletionHandler(FMessage);
  TOpenCustomRESTRequest(FRequest).DoAfterExecute;
end;


Note that I'm not actually using the FSuccess member (yet).

  TRESTThread.Create(ARequest, procedure(const AMessage: string)
    begin
        // Do after completion stuff here. Success can be determined from ARequest.Response.Status.Success
    end
  );


--
Dave Nottage [TeamB]
Delphi Worlds blog: http://delphi.radsoft.com.au/blog
Max Power

Posts: 3
Registered: 4/26/13
Re: Exception handling in Datasnap REST Service
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 10, 2016 8:13 AM   in response to: Jason Sweby in response to: Jason Sweby
Hey guys,

I'm making REST datasnap application and ended up in similar(or same) situation. When an exception raises within datasnap server, client always gets '500 internal server error message'.
I wonder if it's possible to change this message and show something like this: '500 internal server error: <exception text>'

Thanks.
Ederson Selvati

Posts: 34
Registered: 12/2/10
Re: Exception handling in Datasnap REST Service
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 3, 2017 1:59 PM   in response to: Max Power in response to: Max Power
Max Power wrote:
Hey guys,

I'm making REST datasnap application and ended up in similar(or same) situation. When an exception raises within datasnap server, client always gets '500 internal server error message'.
I wonder if it's possible to change this message and show something like this: '500 internal server error: <exception text>'

Thanks.

Hi there

I have this same problem, how do I show the correct error message instead of 500 internal error when an error is fired on server side of datasnap rest server?

Thanks

Ederson
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02