Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / pas2js / 2019-07

Proposal: Improved error messages and handling

1 message · 1 person ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

warleyalex

Look at the class TFormatSettings (sysutils), we can improve it

[code]
type
  { TFormatSettings }
  TFormatSettings = class
  private
    FTranslations: TStrings;
    procedure SetTranslations(Value: TStrings);
  protected
    procedure SetupTranslations; virtual;
    procedure Error(const Format: string; const Msg: string); virtual;
  public
    constructor Create;
    destructor Destroy; override;
    property Translations: TStrings read FTranslations write
SetTranslations;
  end;
{ global functions }
function Translate(const ID: string): string; overload;
function Translate(const ID: string; const Values: array of string): string;
overload;
  { global variable }
var
  FormatSettings_: TFormatSettings = nil;
implementation
function Translate(const ID: string): string;
begin
  Result := FormatSettings_.Translations.Values[ID];
end;
function Translate(const ID: string; const Values: array of string): string;
begin
  Result := Format(Translate(ID), Values);
end;
{ TFormatSettings }
constructor TFormatSettings.Create;
begin
  inherited Create;
  FTranslations := TStringList.Create;
  SetupTranslations;
end;
destructor TFormatSettings.Destroy;
begin
  inherited Destroy;
end;
procedure TFormatSettings.SetupTranslations;
begin
  with FTranslations do
  begin
    BeginUpdate;
  try
    Add('TYPE_UNKNOWN=Unknown');
    Add('TYPE_STRING=String');
    Add('TYPE_BOOLEAN=Boolean');
    Add('TYPE_INTEGER=Integer');
    Add('TYPE_FLOAT=Float');
    Add('TYPE_DATE=Date');
    Add('TYPE_TIME=Time');
    Add('TYPE_DATETIME=DateTime');
    Add('TYPE_BLOB=Blob');
    Add('ERR_FORMAT=Error in the format string %s (%s)');
    Add('ERR_VALUE=see at the source "%s" - an error has been occurred at
the line "%s"');
  finally
    EndUpdate;
  end;
  end;
end;
procedure TFormatSettings.SetTranslations(Value: TStrings);
begin
  FTranslations.Assign(Value);
end;
procedure TFormatSettings.Error(const Format: string; const Msg: string);
begin
  raise Exception.Create(Translate('ERR_FORMAT', [Format, Msg]));
end;
initialization
  FormatSettings_ := TFormatSettings.Create;
(...)
procedure TestWarley;
var
 value: String;
begin
 if value = '' then
   raise Exception.Create(Translate('ERR_VALUE', [{$i %FILE%}, {$i %LINE%}])
);
end;
(...)
TestWarley;

[/code]
we can have this nice msg:

project1.lpr:93 Uncaught {fMessage: "see at the source "D:\fpc2js\projects\projCreature…pr" - an error has been occurred at the line "93"", fHelpContext: 0}

Reply by email