#1 Re: mORMot 2 » OpenAPI Generator Error Handling » 2024-10-08 13:09:01

Thank you for the quick adjustment.

#2 Re: mORMot 2 » OpenAPI Generator Error Handling » 2024-10-08 10:25:41

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.

#3 Re: mORMot 2 » OpenAPI Generator Error Handling » 2024-10-08 09:32:46

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;

#4 Re: mORMot 2 » OpenAPI Generator Error Handling » 2024-10-07 09:30:29

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

#5 mORMot 2 » OpenAPI Generator Error Handling » 2024-10-04 12:43:46

MarcelG
Replies: 8

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;

Board footer

Powered by FluxBB