#1 2024-07-19 21:44:15

mrbar2000
Member
From: Brazil
Registered: 2016-10-26
Posts: 83

Connection failed

I'm trying to creat a postgres connection using this:

function TRepository.ConnectionClass(pDBConnection: TSqlDBConnectionPropertiesClass): IRepository;
begin
  Result := Self;
  FConfigDB := pDBConnection.Create(FHost, FDatabase, FUser, FPassword);
  VirtualTableExternalRegisterAll(FOrmModel, FConfigDB);
  FOrmModel.Props[TAmostra].ExternalDB.MapField('Solicitacao', 'IdSolicitacao');
  FRestOrm := TRestServerDB.Create(FOrmModel, SQLITE_MEMORY_DATABASE_NAME);
  FRestOrm.DB.Synchronous := smOff;
  FRestOrm.DB.LockingMode := lmExclusive;
  FRestOrm.Server.CreateMissingTables;
  FRestOrm.Server.CreateSqlMultiIndex(TAmostra, ['empresa', 'numero'], true, 'sk_amostra_01');
  FRestOrm.Server.CreateSqlMultiIndex(TSolicitacao, ['empresa', 'numero'], true, 'sk_solicitacao_01');
  FRestOrm.Server.TrackChanges([TEmpresa, TUsuario, TAmostra,
    TRack, TEvento], TMyHistory, 100, 10, 65536);
end;

But it's returning an error: Project SorotrackAPI.exe raised exception class ESqlDBPostgres with message 'TSqlDBPostgresLib' Exec failed '. Can someone help me to find what's wrong?

Offline

#2 2024-07-20 06:44:01

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

Re: Connection failed

My guess is that there is more info to the ESqlDBPostgres error than this.

Which statement is it executing?

Offline

#3 2024-07-22 13:01:19

mrbar2000
Member
From: Brazil
Registered: 2016-10-26
Posts: 83

Re: Connection failed

AB,

I found the error, but why it dont appear on message box?

erro

tnks.

Offline

#4 2024-07-22 13:27:50

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

Re: Connection failed

It sounds like if errCode seems incorrect in the context.

Please try with https://github.com/synopse/mORMot2/commit/a0e5ce27

Offline

#5 2024-07-22 21:39:38

mrbar2000
Member
From: Brazil
Registered: 2016-10-26
Posts: 83

Re: Connection failed

Tnks AB.

There are something midleware on momot to exception´s treatment?

My client Rest receive:

{
    "errorCode": 400,
    "errorText": "Bad Request"
}


I whould like receive errorText with real information or treat it before return to rest client.

Offline

#6 2024-07-23 07:31:01

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

Re: Connection failed

You need to debug a little more.
Enable at least the logs on server side.

The 400 error can comes from plenty of context.

Offline

#7 2024-07-23 12:16:26

mrbar2000
Member
From: Brazil
Registered: 2016-10-26
Posts: 83

Re: Connection failed

Sorry, I didn't ask the proper question.
The error appears in the log:

0000000044FC141D EXC    ESqlDBPostgres {Message:"TSqlDBPostgresLib Exec failed: 23503 [ERROR:  update or delete on table \"empresa\" violates foreign key constraint \"fk_posto_01\" on table \"posto\"\nDETAIL:  Key (id)=(1) is still referenced from table \"posto\".\n]",Statement:null} [TAppRestHttpSrv 8081v1 THttpSrv] at ad04c1

But this error cannot be caught by a try except in my application.

function TServiceBase.Delete(pOrmClass: TOrmClass; pId: Int64): TServiceCustomAnswer;
var
  vErros: RawUtf8;
begin
  if not GlobalRepository.Delete(pOrmClass, pId, vErros) then
    Result := ReturnCannotDeleteRecord(StringToUtf8(vErros), HTTP_BADREQUEST);
end;

....

function TRepository.Delete(pOrmClass: TOrmClass; pId: Int64; var pErros: RawUTF8): Boolean;
begin
  pErros := EmptyStr;
  try
    Result := FRestOrm.MemberExists(pOrmClass, pId)
      and FRestOrm.Delete(pOrmClass, pId);   <<<<< into here generate exception
  except on E: Exception do
    begin
      Result := False;     <<<<<<<<   never quick here
      pErros := E.Message;
    end;
  end;
end;

How to capture this error message so you can send on TServiceCustomAnswer?

I looked through the code and documentation and didn't find in IRestORM or any other class that had an event or property with LastError

Offline

#8 2024-07-23 12:36:45

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

Re: Connection failed

Note that it is usually a wrong approach to send back all the detailed error information to a client.
This is most of the time confusing for the end user, and may lead to security risks.

So it is on purpose that IRestOrm methods are encapsulating the row database exception, and only return an error code - false for Delete().

But you can retrieve the last raw DB exception text on the current thread by calling the GetDbError function from mormot.db.core, if you really want to send back some content.

Offline

#9 2024-07-24 04:00:12

mrbar2000
Member
From: Brazil
Registered: 2016-10-26
Posts: 83

Re: Connection failed

I know, i dont want send detailed error information to a client, but i need show something. client need know the cause to not delete or update or insert the record.

There are some event or a method that can i should override to treat whatever type of exception on rest service?

Offline

#10 2024-07-24 10:52:25

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

Re: Connection failed

The cause should be done in your business logic, and returned explicitly, not from an exception message.
The exception message is likely to change from one DB to another. Don't make your persistence logic specific. Or don't use the ORM.

Offline

Board footer

Powered by FluxBB