unit XData.Module.Exceptions;

interface

uses
  SysUtils, Rtti,
  XData.Module.Base,
  XData.Model.Classes,
  XData.Sys.Exceptions;

type
  EXDataHttpUnsupportedPath = class(EXDataHttpException)
  public
    constructor Create(const AContext: TXDataBaseRequestHandler);
  end;

  EXDataHttpInvalidHeaderValue = class(EXDataHttpException)
  public
    constructor Create(const HeaderName, HeaderValue: string);
  end;

  EXDataHttpEntityNotFound = class(EXDataHttpException)
  public
    constructor Create(const AContext: TXDataBaseRequestHandler);
  end;

  EXDataHttpUrlInfoError = class(EXDataHttpException)
  public
    constructor Create(const AContext: TXDataBaseRequestHandler);
  end;

  EXDataHttpUnsupportedQuery = class(EXDataHttpException)
  public
    constructor Create(const AQuery: string);
  end;

  EXDataHttpUnsupportedMediaType = class(EXDataHttpException)
  public
    constructor Create(const AType: string);
  end;

  EXDataHttpMissingParameter = class(EXDataHttpException)
  public
    constructor Create(const ParamName: string);
  end;

  EXDataHttpMissingServiceImplementation = class(EXDataHttpException)
  public
    constructor Create(const ControllerName: string);
  end;

  EXDataHttpCannotRetrieveServiceInterface = class(EXDataHttpException)
  public
    constructor Create(const ServiceInterfaceName, ServiceClassName: string);
  end;

  EXDataHttpUnsupportedTunneledMethod = class(EXDataHttpException)
  public
    constructor Create(const TunelledMethod: string);
  end;

  EXDataHttpAmbiguousActionError = class(EXDataHttpException)
  public
    constructor Create(const ActionName: string);
  end;

  EXDataHttpParamFromUrlError = class(EXDataHttpException)
  public
    constructor Create(Param: TXDataParamDef; const Value: TValue);
  end;

implementation

{ EXDataHttpUnsupportedPath }

constructor EXDataHttpUnsupportedPath.Create(const AContext: TXDataBaseRequestHandler);
begin
  inherited Create(400, 'Navigation path provided in requested Url is not supported');
end;

{ EXDataHttpUrlInfoError }

constructor EXDataHttpUrlInfoError.Create(const AContext: TXDataBaseRequestHandler);
var
  ErrorCode: integer;
begin
  ErrorCode := AContext.ResourceInfo.ErrorCode;
  if ErrorCode = 0 then
    ErrorCode := 404;
  inherited Create(ErrorCode, AContext.ResourceInfo.ErrorMessage);
end;

{ EXDataHttpEntityNotFound }

constructor EXDataHttpEntityNotFound.Create(
  const AContext: TXDataBaseRequestHandler);
begin
  inherited Create(404, 'Requested entity does not exist.');
end;

{ EXDataHttpUnsupportedQuery }

constructor EXDataHttpUnsupportedQuery.Create(const AQuery: string);
begin
  inherited CreateFmt(400, 'Unsupported query string: "%s"', [AQuery]);
end;

{ EXDataHttpUnsupportedMediaType }

constructor EXDataHttpUnsupportedMediaType.Create(const AType: string);
begin
  inherited CreateFmt(415, 'Unsupported media type: %s', [AType]);
end;

{ TXDataHttpMissingParameter }

constructor EXDataHttpMissingParameter.Create(const ParamName: string);
begin
  inherited CreateFmt(400, 'Missing value for parameter "%s"', [ParamName]);
end;

{ EXDataHttpMissingServiceImplementation }

constructor EXDataHttpMissingServiceImplementation.Create(const ControllerName: string);
begin
  inherited CreateFmt(500, 'Missing service implementation for controller "%s"', [ControllerName]);
end;

{ EXDataHttpCannotRetrieveServiceInterface }

constructor EXDataHttpCannotRetrieveServiceInterface.Create(const ServiceInterfaceName,
  ServiceClassName: string);
begin
  inherited CreateFmt(500, 'Cannot retrieve service interface "%s" from service class "%s"',
    [ServiceInterfaceName, ServiceClassName]);
end;

{ EXDataHttpUnsupportedTunneledMethod }

constructor EXDataHttpUnsupportedTunneledMethod.Create(
  const TunelledMethod: string);
begin
  inherited CreateFmt(400, 'Invalid HTTP method "%s" tunneled with x-http-method-override header', [TunelledMethod]);
end;

{ EXDataHttpAmbiguousActionError }

constructor EXDataHttpAmbiguousActionError.Create(const ActionName: string);
begin
  inherited CreateFmt(500, 'Requested action "%s" has ambiguous implementation', [ActionName]);
end;

{ EXDataHttpInvalidHeaderValue }

constructor EXDataHttpInvalidHeaderValue.Create(const HeaderName,
  HeaderValue: string);
begin
  inherited CreateFmt(400, 'Invalid value "%s" for request header "%s"', [HeaderValue, HeaderName]);
end;

{ EXDataHttpParamFromUrlError }

constructor EXDataHttpParamFromUrlError.Create(Param: TXDataParamDef; const Value: TValue);
begin
  inherited CreateFmt(500,
    'Cannot deserialize URL value "%s" for parameter "%s.%s"',
    [Value.ToString, Param.Parent.Name, Param.Name]);
end;

end.
