Jump to content
Michal S.

Get Exception message on client

Recommended Posts

Hello, I start with MARS and I need to show on client exception message from server.

 

What I mean:

Server side:

function THelloWorldResource.SayHelloWorld: string;
begin
    Result := 'Hello World message!';

    raise EMARSApplicationException.Create('This is server exception message.', 500);
end;

 

On client I tried this:

DataModuleRest.HelloWorldResource.GET(nil, nil,
	procedure (AException: Exception)
	begin
		raise Exception.Create('Exception message: '+AException.Message);
	end);

 

But only error message what I get on client is "HTTP/1.1 500 Internal Server Error"

 

Please can you help me, how to get the server exception message on the client?

Share this post


Link to post

Hi @Michal S.,

for security reasons, I decided to avoid error messaging to automatically flow to the client. Error messages may contain sensitive data and by default MARS reply with an 'Internal Server Error' message if an Exception is raised in the server code.

 

However:

1) for debug purposes, you can switch the Build Configuration from Release to Debug and have the error message appended after the Internal Server Error string.

2) in real code, if you want the message to flow to the client use a EMARSHttpException object:

 

function THelloWorldResource.MyMethodWithException: TJSONObject;
begin
  Result := TJSONObject.Create;
  try
    Result.WriteStringValue('msg', 'This method will raise an exception');
  //  raise Exception.Create('This is my custom error message'); // this will be seen as 'Internal server error'
    raise EMARSHttpException.Create('This is my custom error message'); // this message will flow to the client
  except
    Result.Free;
    raise;
  end;
end;

 

HTH.

Sincerely,

Andrea

 

Share this post


Link to post

Hello @Andrea Magni,

thank you for your reply, but it doesn't work for me 😞

Even if I try the Hello world demo and modify the HelloWorld server function this way:

function THelloWorldResource.HelloWorld(): string;
begin
  Result := 'Hello, World!';
  raise EMARSHttpException.Create('This is my ugly error message...');
end;

On the client side I still have only Internal server error message.

 

Don't you have some demo for this?

Thank you very much

 

Sincerely,

Michal

Share this post


Link to post

Sorry, it's my fault: MARS supports two http layer implementor on the client side: Indy and TNetHttpClient.

Handling errors has some differences across the two, so it depends if you are using a TMARSClient (Indy) or TMARSNetClient (TNetHttpClient).

 

Either ways, you can define an OnError handler for your TMARSApplication:

 

uses FMX.Dialogs
, MARS.Core.Utils, MARS.Client.Utils
, IdHttp;

var
  LException: EMARSClientHttpException;
  LIdException: EIdHTTPProtocolException;
begin
  if AException is EMARSClientHttpException then
  begin
    LException := EMARSClientHttpException(AException);
    ShowMEssage('Error' + LException.Message + ' Details: ' + StreamToString(LException.Content));
    AHandled := True;
  end;
  if AException is EIdHttpProtocolException then
  begin
    LIdException := EIdHttpProtocolException(AException);
    ShowMEssage('Error' + LIdException.Message);
    AHandled := True;
  end;

end;

 

IIRC I was not able to find a way to collect error details (that are sent back from the server to the client using the body of the response) when using Indy.

I am open to discuss how to improve the overall mechanism, please express your thoughts (everybody!).

I'll try to update the old demos (such as HelloWorld) to include error handling at some point.

 

Sincerely,

Andrea

 

Share this post


Link to post

Hello @Andrea Magni,

still the same 😞 - I'm using TMARSClient and when I declare MarsApplication.OnError handler, I receive again only general exception with 500 Internal Server Error message.

 

On the client the EIdHttpProtocolException is raised, but from your example we need the EMARSClientHttpException, where error message is in the Exception.Content.

 

Maybe update the Hello world demo will be the best way (I'm trying on the Hello world demo too).

 

Thanks for your help

 

Sincerely,

Michal

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×