How to set e.message before Eurekalog call

328 Views Asked by At

I'm using the code below to try to create a custom error message for logging by EL. The code works for logging myself (ie when {ifNdef EUREKALOG}) - in which case the '(Extra Info)' shows in the ShowMessage, but not when calling EL logging. In the latter case, the orginal e.message is logged. Is there a way to achieve this?

on e: exception do
begin
  e := Exception(AcquireExceptionObject);
  e.Message := '(Extra info) ' + e.Message;
{$if defined(EUREKALOG)}
  // EExceptionManager.ExceptionManager.ShowLastExceptionData;
  // OR
  EBASE.HandleException(e);
{$else}
  ShowMessage(e.message + ' I got this, thanks!');
{$endif}
  ReleaseExceptionObject;
end;
2

There are 2 best solutions below

3
Freddie Bell On
procedure TForm1.btnTryExceptELClick(Sender: TObject);
begin
  try
    ProcWithError;
  except
  on e: exception do
  begin
  e := Exception(AcquireExceptionObject);
  try
    e.Message := E.Message + '(Extra info) ';
    {$if defined(EUREKALOG)}
      raise Exception.Create(e.message); // with "extra info"
    // if you really want to do this yourself (for no reason)
    //    EExceptionManager.ExceptionManager.ShowLastExceptionData;
    // OR
    //  EBASE.HandleException(e);
    {$else}
      ShowMessage(e.message + ' I''ve got this, thanks!');
      LogError(e.message);
    {$endif}
  finally
    ReleaseExceptionObject;
  end;
  end;
  end;
  ShowMessage('After except');
end;
0
Alex On

The previous answers are correct: EurekaLog captures exception's information when exception is raised. It does not know that you have changed exception object later. You need to explicitly tell EurekaLog new information. For example:

uses
  EException,        // for TEurekaExceptionInfo
  EExceptionManager; // for ExceptionManager

procedure TForm1.Button1Click(Sender: TObject);
{$IFDEF EUREKALOG}
var
  EI: TEurekaExceptionInfo;
{$ENDIF}
begin
  try
    raise Exception.Create('Error Message');
  except
    on E: Exception do
    begin
      E.Message := E.Message + sLineBreak + 'Hello from except block';

      {$IFDEF EUREKALOG}
      EI := ExceptionManager.Info(E);
      // Сould be NIL if EurekaLog is disabled or instructed to ignore this exception
      if Assigned(EI) then
        // Overrides both dialog and logs
        EI.ExceptionMessage := E.Message;
        // OR:
        // Overrides only dialogs
        // EI.AdditionalInfo := E.Message;
      {$ENDIF}

      raise; // or Application.ShowException(E);
    end;
  end;
end;