#1 2024-11-27 09:18:01

anouri
Member
Registered: 2024-02-11
Posts: 51

Http response code (200,500,..) in interface base service

Hi,
I call insert method from interface base service, when primary key field is duplicate server return error like below:
How can I get http rersponse code. after calling I.Insert(LInvoice);

TInterfacedObjectFakeClient.FakeCall(IInvoiceService.Insert) failed: '{
"errorCode":500,
"error":
{"EZSQLException":
{
    "ClassName": "EZSQLException",
    "Address": "073e8890",
    "Message": "SQL Error: Duplicate entry '10' for key 'PRIMARY'"
}}
}'

procedure TfrmInvoice.Button1Click(Sender: TObject);
var
  I: IInvoiceService;
  LInvoice: TInvoice;
begin
  if HttpClient.Services['InvoiceService'].get(I) then
  begin
    LInvoice.code := '10';
    LInvoice.descr := 'test';
    I.Insert(LInvoice);
  end;
end;

Last edited by anouri (2024-11-27 09:21:17)

Offline

#2 2024-11-27 09:36:50

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,725
Website

Re: Http response code (200,500,..) in interface base service

With interface-based services, by default you will always get either 200 or 500.
And do not try to get the HTTP status code with interfaces-based services: it would induce a dependency to HTTP, which we want to avoid by using interfaces and their abstraction.
You may implement the service without any remote access involved.

Catch it in the server code, and let the method return false.
You can add an output string value for addition information, or an enumeration for the kind of error.

See https://synopse.info/files/html/Synopse … l#TITL_165

Offline

#3 2024-11-27 15:53:09

anouri
Member
Registered: 2024-02-11
Posts: 51

Re: Http response code (200,500,..) in interface base service

I changed insert method and solved:

function Insert(const AInvoice: TInvoice): TServiceCustomAnswer ;

Thank you.

Offline

#4 2024-11-27 18:35:57

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,725
Website

Re: Http response code (200,500,..) in interface base service

It is a possibility for non-Delphi REST clients.
But for a Delphi REST client, using interfaces, using a boolean or an enumeration is always better.

Offline

Board footer

Powered by FluxBB