unit Azure.CognitiveServices.Core.Exceptions;

interface

uses
  System.SysUtils;

type

  EAcsException = class(Exception)
  public
    constructor Create();
  end;

  EAcsHttpException = class(EAcsException)
  private
    FErrorDescription: string;
    FStatusCode: Integer;
    FStatusMessage: string;
  public
    constructor Create(AStatusCode: Integer; const AStatusMessage: string;
      const AErrorDescription: string);
    function ToString(): string; override;
    property ErrorDescription: string read FErrorDescription;
    property StatusCode: Integer read FStatusCode;
    property StatusMessage: string read FStatusMessage;
  end;

implementation

constructor EAcsException.Create;
begin
  inherited Create(Self.ToString);
end;

constructor EAcsHttpException.Create(AStatusCode: Integer;
  const AStatusMessage, AErrorDescription: string);
begin
  FErrorDescription := AErrorDescription;
  FStatusCode := AStatusCode;
  FStatusMessage := AStatusMessage;
  inherited Create();
end;

function EAcsHttpException.ToString: string;
var
  LBuffer: TStringBuilder;
begin
  LBuffer := TStringBuilder.Create;
  try
    LBuffer.AppendFormat('[%d]', [FStatusCode]);
    if Length(FStatusMessage) > 0 then
      LBuffer.Append(' ').Append(FStatusMessage);
    if Length(FErrorDescription) > 0 then
      LBuffer.AppendLine().Append(FErrorDescription);
    Result := LBuffer.ToString();
  finally
    LBuffer.Free;
  end;
end;

end.
