Recent

Author Topic: catch error 400  (Read 1302 times)

Nicole

  • Hero Member
  • *****
  • Posts: 1303
catch error 400
« on: July 10, 2025, 04:55:44 pm »
Here is a nice site, which gives me free news.
It is not nice any more, if I key in anything, which is not found.
Then there pops up 2 times "error 400".
This is rather annoying to click away and I would prefer it return a silent "nothing found" instead.
I am not familiar where I hop on an catch those replies and where exactly to hook in.
Can you give me a hint please?
The function reads (the last line is formatting it):

Code: Pascal  [Select][+][-]
  1. // sucht zu einem Ticker die Nachrichten
  2. function TNews.Suche_NewsZu(Ticker: string; abDatum, bisDatum: TDate): string;
  3. var URL, Response: string;
  4.     HttpClient: TFPHTTPClient;
  5.     JsonResponse: TJSONData;
  6.     ts_ab, ts_bis: Int64;
  7.     s_from, s_to: string;
  8. begin
  9.   Result := '';
  10.  
  11.   ts_ab := DateTimeToUnix(abDatum);
  12.   ts_bis := DateTimeToUnix(bisDatum + 1);  // period2 ist exklusiv!
  13.  
  14.   s_from := IntToStr(ts_ab);
  15.   s_to := IntToStr(ts_bis);  // brauche ich offenbar nicht, kam von Yahoo-Vorlage
  16.  
  17.   URL := 'https://newsapi.org/v2/everything?q=' + Ticker +
  18.   '&from=' + s_from + '&sortBy=publishedAt&apiKey='+ APIKEY_News;
  19.  
  20.   HttpClient := TFPHTTPClient.Create(nil);
  21.   HttpClient.AddHeader('User-Agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36');
  22.   try
  23.     Response := HttpClient.Get(URL);
  24.     JsonResponse := GetJSON(Response);
  25.     Ersetze.JsonWirdDatei(JsonResponse, 'myNews_json');  // ich speichere es einmal ab
  26.     result:=JsonResponse.FormatJSON([]);
  27.  
  28.     result:=VerarbeiteArtikel(result); //VerarbeiteAntwort(result);
  29.   finally
  30.     HttpClient.Free;
  31.   end;
  32. end;        

paweld

  • Hero Member
  • *****
  • Posts: 1568
Re: catch error 400
« Reply #1 on: July 10, 2025, 05:12:15 pm »
Use try..except block.
Code: Pascal  [Select][+][-]
  1. // sucht zu einem Ticker die Nachrichten
  2. function TNews.Suche_NewsZu(Ticker: string; abDatum, bisDatum: TDate): string;
  3. var URL, Response: string;
  4.     HttpClient: TFPHTTPClient;
  5.     JsonResponse: TJSONData;
  6.     ts_ab, ts_bis: Int64;
  7.     s_from, s_to: string;
  8. begin
  9.   Result := '';
  10.  
  11.   ts_ab := DateTimeToUnix(abDatum);
  12.   ts_bis := DateTimeToUnix(bisDatum + 1);  // period2 ist exklusiv!
  13.  
  14.   s_from := IntToStr(ts_ab);
  15.   s_to := IntToStr(ts_bis);  // brauche ich offenbar nicht, kam von Yahoo-Vorlage
  16.  
  17.   URL := 'https://newsapi.org/v2/everything?q=' + Ticker +
  18.   '&from=' + s_from + '&sortBy=publishedAt&apiKey='+ APIKEY_News;
  19.  
  20.   HttpClient := TFPHTTPClient.Create(nil);
  21.   HttpClient.AddHeader('User-Agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36');
  22.   try
  23.     try
  24.       Response := HttpClient.Get(URL);
  25.     except
  26.       on E: EHttpClient do
  27.       begin
  28.         case HttpClient.ResponseStatusCode of
  29.           401: Caption := 'Unauthorised';
  30.           404: Caption := 'Not found';
  31.           else
  32.             Caption := Format('error: %d', [HttpClient.ResponseStatusCode]);
  33.         end;
  34.       end;
  35.     end;
  36.     JsonResponse := GetJSON(Response);
  37.     Ersetze.JsonWirdDatei(JsonResponse, 'myNews_json');  // ich speichere es einmal ab
  38.     result:=JsonResponse.FormatJSON([]);
  39.  
  40.     result:=VerarbeiteArtikel(result); //VerarbeiteAntwort(result);
  41.   finally
  42.     HttpClient.Free;
  43.   end;
  44. end;  
  45.  
Best regards / Pozdrawiam
paweld

Nicole

  • Hero Member
  • *****
  • Posts: 1303
Re: catch error 400
« Reply #2 on: July 10, 2025, 06:44:40 pm »
Thank you, looks good, but does not work yet.
I read
"u_news.pas(218,16) Error: Identifier not found "Caption""

at
Code: Pascal  [Select][+][-]
  1.  case HttpClient.ResponseStatusCode of
  2.           401: Caption := 'Unauthorised';  

If I change caption to result, the error message comes back

Code: Text  [Select][+][-]
  1. [Window Title]
  2. Fehler
  3.  
  4. [Content]
  5. Projekt project_Tiger hat Exception-Klasse »External: ACCESS VIOLATION« ausgelöst mit der Meldung:
  6. Access violation reading from address $0000000000000000.
  7.  
  8.  Bei Adresse 10011AEC3
  9.  
  10. [Ok]

followed by an access-thing because it is not json any more.


paweld

  • Hero Member
  • *****
  • Posts: 1568
Re: catch error 400
« Reply #3 on: July 10, 2025, 08:42:15 pm »
You only showed one function, so I thought TNews was a Form and I wanted to display message on its title.
If it is a class then instead of to Caption you can assign this message as a function result or to some property storing the last error etc.
Best regards / Pozdrawiam
paweld

 

TinyPortal © 2005-2018