#1 2025-11-22 17:56:43

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

accesse Call parameters in ONError?

why it is not possible get url parameters in onError?
Ctxt.parameters is empty here

Offline

#2 2025-11-23 07:39:41

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 15,305
Website

Re: accesse Call parameters in ONError?

Do you mean TRestServer.OnUriError?

If the exception is raised during the parsing, or there is no parameter, it is nil.
You should be able to use Ctxt.Call^.Uri instead.

Offline

#3 2025-11-23 11:34:47

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

Re: accesse Call parameters in ONError?

Yes exactly

Offline

#4 2025-11-23 11:38:57

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

Re: accesse Call parameters in ONError?

there is no Uri. but there are Url = roort\CustomerService\Delete

Ctxt.Call^.InBody is unterminated json. {"cust_code"

error reurned from mysql server

Cannot delete or update a parent row: a foreign key constraint fails (`passak_utf`.`scm_sorder_cust_item`, CONSTRAINT `scm_sorder_cust_item_fk1` FOREIGN KEY (`sale_order`, `sorder_line_no`) REFERENCES `scm_sorder_det` (`sale_order`, `line_no`) ON UPDATE CASCADE)

Last edited by anouri (2025-11-23 11:46:20)

Offline

#5 2025-11-23 11:59:23

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 15,305
Website

Re: accesse Call parameters in ONError?

All this is as intended.

But anyway, the error is to be caught with a try..finally in your own method code.
No in OnErrorUri callback.

Note: you can get the DB error from GetDbError() function of mormot.db.core.pas.

Offline

#6 2025-11-23 12:08:53

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

Re: accesse Call parameters in ONError?

True, but I wanted to raise errors in different places in the application if possible and handle them in only one place. This way, I would avoid writing hundreds of try excepts and centralize all error handling in one place.

Offline

#7 2025-11-23 12:12:15

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 15,305
Website

Re: accesse Call parameters in ONError?

I am afraid it may lead to a confusion between logic error and database error.

As a better alternative, you may encapsulate the SQL execution into some methods of your own, with a centralized error handling.

Offline

#8 2025-11-23 12:26:19

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

Re: accesse Call parameters in ONError?

I created a custom exception class containing error codes and messages for handling business errors. However, one limitation I'm facing is that I cannot access URI parameters in this scenario.

Offline

#9 2025-11-25 06:48:55

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

Re: accesse Call parameters in ONError?

I followed your suggestion:

function DeleteRecord(const ASql: RawUtf8; const Params: array of const; AConnection: TSqlDBConnection; out UpdateCount: Integer): TServiceCustomAnswer;
begin
  try
    UpdateCount := AConnection.Properties.ExecuteNoResult(ASql, Params);
    if UpdateCount = 0 then
    begin
      Result.Status := HTTP_SERVERERROR;
      Result.Content := TErrorDTO.CreateErrorJson('1' , 'no record found');     
      Exit;
    end;
    Result.Status := HTTP_SUCCESS;
    Result.Content := TUtils.AddCommonHeaderToJSon('[]', true);
  except
    on E: EZSQLException do
    begin
      if EZSQLException(E).ErrorCode = 1451 then // foreign key constraint error
      begin
        Result.Status := HTTP_SERVERERROR;
        Result.Content := TErrorDTO.CreateErrorJson('1' , 'record can not be deleted.');
      end;
    end;
  end;
end;

Last edited by anouri (2025-11-25 10:37:31)

Offline

Board footer

Powered by FluxBB