You are not logged in.
Pages: 1
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
It works. But isn't there a better solution?
Offline
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.
Online
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
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
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
Online
Thank you for the quick adjustment.
Offline
Pages: 1