Skip to content

Commit

Permalink
Encapsulating error messges into custom exception class. Collecting m…
Browse files Browse the repository at this point in the history
…y garbage introduced into the global namespace :-)
  • Loading branch information
the-Arioch authored and the-Arioch committed Oct 5, 2016
1 parent 801e468 commit c2c3b6f
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions XmlLite.pas
Original file line number Diff line number Diff line change
Expand Up @@ -217,12 +217,6 @@ interface
);
{$WARN BOUNDS_ERROR DEFAULT}

rErrorDecription = record Code: XmlError; Message: String; end;
TXMLLiteKnownErrors = array of rErrorDecription;

var
XMLLiteErrorMessages: TXMLLiteKnownErrors; // may be localized by thrid-party units

type
IXMLReader = interface
['{7279FC81-709D-4095-B63D-69FE4B0D9030}']
Expand Down Expand Up @@ -384,14 +378,21 @@ function OpenXmlFileStreamWriter(const FileName: string): IStream;
procedure CheckHR(const HR: HRESULT); inline; deprecated 'Use EXmlLite.Check';

type
rErrorDecription = record Code: XmlError; Message: String; end;
TXMLLiteKnownErrors = array of rErrorDecription;

EXmlLite = class(Exception)
private
FErrCode: Cardinal;
public
property ErrorCode: Cardinal read FErrCode; // 0 - unknown
constructor CreateForErrCode(const FunctionResult: HRESULT);
public
Class Var XMLLiteErrorMessages: TXMLLiteKnownErrors; // may be localized by thrid-party units
Class Constructor InitMessages;

Class Function Check(const FunctionResult: HRESULT): HResult; inline;
Class Function IsOK(const FunctionResult: HRESULT): boolean; inline;
constructor CreateForErrCode(const FunctionResult: HRESULT);
end;

implementation
Expand Down Expand Up @@ -605,13 +606,12 @@ procedure CheckHR(const HR: HRESULT);
EXmlLite.Check( HR );
end;


function r(const c: cardinal; m: string): rErrorDecription; inline;
begin
Result.Code := XmlError(c);
Result.Message := m;
end;

class constructor EXmlLite.InitMessages;
function r(const c: cardinal; m: string): rErrorDecription; inline;
begin
Result.Code := XmlError(c);
Result.Message := m;
end;
begin
XMLLiteErrorMessages := TXMLLiteKnownErrors.Create(
r($C00CEE01, 'unexpected end of input'),
Expand Down Expand Up @@ -700,4 +700,6 @@ function r(const c: cardinal; m: string): rErrorDecription; inline;
r($C00CE01F, 'XML: Invalid Unicode characters'),
r($C00CE06E, 'XML: Invalid charset encoding')
);
end;

end.

0 comments on commit c2c3b6f

Please sign in to comment.