[Pas2js] Proposal: Improved error messages and handling

warleyalex warleyalex at yahoo.com.br
Sun Jul 7 01:37:23 CEST 2019


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}




--
Sent from: http://pas2js.38893.n8.nabble.com/


More information about the Pas2js mailing list