#1 2015-08-03 21:50:27

EMartin
Member
From: Buenos Aires - Argentina
Registered: 2013-01-09
Posts: 337

How send custom HTTP error code and message w/interface based service

Hi,

   I have the following service:

function TDBService.ContactDataRecycle(const aUser, aSourceTableName, aDestTableName: RawUTF8;
                                       const aOverwriteOrAppend, aFax, aAnswerMachine, aNoContact: Boolean;
                                       const aDuration: Integer; const aVarTMCondition: RawUTF8): Integer;
begin
  if (aSourceTableName = '') then
    raise EASOA.CreateUTF8('Invalid parameters: table name=%', [aSourceTableName]);
  if Assigned(fTableList.Find(aSourceTableName)) then
    ServiceContext.Request.Error('Table % is in use by an outgoing activity', [aSourceTableName]);
end;

   but the response is (second if is executed):

{"result":[4206772]}

   Is there another way to return custom error code and message ? I want receive the response in the same way that when raising an exception, but with another error code and without raise an exception.

Thanks in advance.

EMartin.


Esteban

Offline

#2 2015-08-04 09:24:17

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

Re: How send custom HTTP error code and message w/interface based service

If the error is a fatal error, use an exception.

If the error is a business logic error, use an enumerate, and a custom code (with an optional string).
See e.g. how we defined CQRS services for mORMotDDD.pas.

Corresponding doc is http://synopse.info/files/html/Synopse% … l#TITL_165

Offline

#3 2015-08-04 10:35:58

EMartin
Member
From: Buenos Aires - Argentina
Registered: 2013-01-09
Posts: 337

Re: How send custom HTTP error code and message w/interface based service

I have existing code without DDD implementation (you were building this).

I'll study how to use DDD and the change of return codes from HTTP to DDD.

But now I have to deliver the work so I'll use exceptions for send message with the error.

Thanks.

EMartin.


Esteban

Offline

#4 2015-08-04 14:25:59

EMartin
Member
From: Buenos Aires - Argentina
Registered: 2013-01-09
Posts: 337

Re: How send custom HTTP error code and message w/interface based service

Hi ab,

Why ServiceContext.Request.Error(...) is not working in interfaces as Ctxt.Error(...) works in methods ?

Thanks.

EMartin.


Esteban

Offline

#5 2015-08-05 05:36:49

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

Re: How send custom HTTP error code and message w/interface based service

Because interface-based services override the Ctxt content at output.
This is how it works.

ServiceContext.Request may be used for input (e.g. retrieve the IP), not for output.
In theory, it is better not to rely on ServiceContext.Request at all, since interface-based services may be executed with ServiceContext.Request=nil, e.g. if they are resolved and executed directly on the server side.

Offline

#6 2015-08-05 10:29:21

EMartin
Member
From: Buenos Aires - Argentina
Registered: 2013-01-09
Posts: 337

Re: How send custom HTTP error code and message w/interface based service

Thanks ab.

I think that when request is not nil it should be able to send an error response, is another way of override the Ctxt content at output.

My workaround was to use TServiceCustomAnswer creating both the correct response as the incorrect because changing to DDD is much more work for the moment.

EMartin.


Esteban

Offline

Board footer

Powered by FluxBB