#1 2024-10-04 12:43:46

MarcelG
Member
Registered: 2024-10-04
Posts: 5

OpenAPI Generator Error Handling

Hello, how can I handle errors with the OpenAPI Client Generator Code?

This is my current approach where I changed fClient to public:

procedure TForm1.HandleJsonClientError(const Sender: IJsonClient; const Response: TJsonResponse; const ErrorMsg: shortstring);
begin
  fehlerAufgetreten := true;
  ShowMessage('Error: ' + ErrorMsg);
end;

function GetPet(PetId: integer): string;
var
  Pet: TPet;
  jerror: TOnJsonClientError;
begin
 Pet := Client.GetPetById(PetId);
 jerror := Form1.HandleJsonClientError;
 Form1.fehlerAufgetreten := false;
 Client.fClient.Request('GET', '/api/pet/%', [PetId], [], [],Pet, TypeInfo(TPet) ,jerror);
  if not Form1.fehlerAufgetreten then
    Result := 'ID:' + InttoStr(Pet.Id) + ', Name:' + Pet.Name + ', Status:' + GetEnumName(TypeInfo(TEnumPetStore1), Ord(Pet.Status)) //+ ' Fotourl:' + Pet.PhotoUrls[0];
end;

Offline

#2 2024-10-04 17:52:57

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

Re: OpenAPI Generator Error Handling

What is wrong with the errors raising exceptions?

Offline

#3 2024-10-07 09:30:29

MarcelG
Member
Registered: 2024-10-04
Posts: 5

Re: OpenAPI Generator Error Handling

It works. But isn't there a better solution?

Offline

#4 2024-10-07 15:01:29

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

Re: OpenAPI Generator Error Handling

I don't like exceptions on server side, but I have nothing against them on client side.
For instance, it sounds perfectly natural to write:

try
  Pet := Client.GetPetById(PetId);
  Result := 'ID:' + InttoStr(Pet.Id) + ', Name:' + Pet.Name + ', Status:' + GetEnumName(TypeInfo(TEnumPetStore1), Ord(Pet.Status)) //+ ' Fotourl:' + Pet.PhotoUrls[0];
except
  Result := 'failed';
end;

with no global and thread-unsafe fehlerAufgetreten flag.

I just wonder how a callback could be a better solution.
You need to stop the normal flow of execution on error. An exception is perfect for this.
Your "ShowMessage" doesn't stop anything, just pauses the execution.

Only explicit error output parameters could be possible as far as I can see.
That is, not only return the regular output, but a set of potential error structures... then the caller should manually check if any of those structures are set. Or return some kind of "exception" class, but without raising anything. Or return the "status code" integer, or a "success" boolean.

My big concerns are that
1) "silent" errors may not be caught by the client, whereas exceptions are always notified.
2) using a temporary storage of the response in the main client class (i.e. copying the last TJsonResponse content for manual error handling) would make the client class not thread-safe any more.
3) honestly, I don't see any better scheme.

Any feedback is welcome.

Offline

#5 2024-10-08 09:32:46

MarcelG
Member
Registered: 2024-10-04
Posts: 5

Re: OpenAPI Generator Error Handling

In my case it seems, that the exception does not work. Maybe because TPet is a record.

What do you think about this implementation?

function TCustomPetStoreClient.GetPetByID(PetID: Integer; var ResultPet: TPet): Boolean;
begin
  fClient.Request('GET', '/api/pet/%', [PetId], [], [], ResultPet, TypeInfo(TPet));
  Result := fClient.Http.Status = 200;
end;

Offline

#6 2024-10-08 09:45:38

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

Re: OpenAPI Generator Error Handling

Please define "the exception does not work".

Offline

#7 2024-10-08 10:25:41

MarcelG
Member
Registered: 2024-10-04
Posts: 5

Re: OpenAPI Generator Error Handling

The generated method Client.GetPetById(PetId) does not throw errors even if the ID does not exist, so the except block can never be reached.

Offline

#8 2024-10-08 11:01:15

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

Re: OpenAPI Generator Error Handling

What is the HTTP status code returned by the server?

Did you set jcoHttpErrorRaise in the client options?

Edit:
I think now I understand your problem.
I guess your IJsonClient does not have the jcoHttpErrorRaise option.
I have forced it by default now:
https://github.com/synopse/mORMot2/commit/af7f9914
and in the OpenAPI generator itself:
https://github.com/synopse/mORMot2/commit/bdae0ffb

Offline

#9 2024-10-08 13:09:01

MarcelG
Member
Registered: 2024-10-04
Posts: 5

Re: OpenAPI Generator Error Handling

Thank you for the quick adjustment.

Offline

Board footer

Powered by FluxBB