Improved error handling for provider model loading

This commit is contained in:
Thorsten Sommer 2026-04-14 09:16:47 +02:00
parent d494fe4bc7
commit 77f2a55598
No known key found for this signature in database
GPG Key ID: B0B7E2FC074BF1F5
2 changed files with 53 additions and 7 deletions

View File

@ -1,4 +1,5 @@
using System.Net.Http.Headers;
using System.Net;
using System.Net.Http.Headers;
using System.Runtime.CompilerServices;
using AIStudio.Chat;
@ -123,9 +124,24 @@ public sealed class ProviderGWDG() : BaseProvider(LLMProviders.GWDG, "https://ch
using var response = await this.httpClient.SendAsync(request, token);
if(!response.IsSuccessStatusCode)
{
if(response.StatusCode is HttpStatusCode.Unauthorized)
LOGGER.LogWarning("Unauthorized access when loading models for provider {ProviderId}. Please check the API key. Status Code: {StatusCode}. Reason: {ReasonPhrase}", this.Id, response.StatusCode, response.ReasonPhrase);
else
LOGGER.LogWarning("Failed to load models for provider {ProviderId}. Status Code: {StatusCode}. Reason: {ReasonPhrase}", this.Id, response.StatusCode, response.ReasonPhrase);
return [];
}
var modelResponse = await response.Content.ReadFromJsonAsync<ModelsResponse>(token);
return modelResponse.Data;
try
{
var modelResponse = await response.Content.ReadFromJsonAsync<ModelsResponse>(token);
return modelResponse.Data;
}
catch (Exception e)
{
LOGGER.LogError(e, "Exception occurred while loading models for provider {ProviderId}. Exception Message: {ExceptionMessage}", this.Id, e.Message);
return [];
}
}
}
}

View File

@ -1,5 +1,6 @@
using System.Net.Http.Headers;
using System.Runtime.CompilerServices;
using System.Text.Json;
using AIStudio.Chat;
using AIStudio.Provider.OpenAI;
@ -121,10 +122,39 @@ public sealed class ProviderHelmholtz() : BaseProvider(LLMProviders.HELMHOLTZ, "
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", secretKey);
using var response = await this.httpClient.SendAsync(request, token);
// Unfortunately, the Helmholtz API does not return a non-success status code when the API key is invalid. Instead, it returns a 200 OK with a body that contains an error message.
// Therefore, we have to check the body of the response to determine if the request was successful or not.
if(!response.IsSuccessStatusCode)
return [];
var modelResponse = await response.Content.ReadFromJsonAsync<ModelsResponse>(token);
return modelResponse.Data;
try
{
var modelResponse = await response.Content.ReadFromJsonAsync<ModelsResponse>(token);
return modelResponse.Data;
}
catch (JsonException e)
{
//
// We expect a JsonException to be thrown when the API key is invalid, because the body of the response will not
// be a valid JSON. Therefore, we catch this exception and show an appropriate error message to the user.
//
var body = await response.Content.ReadAsStringAsync(token);
if(body.Contains("invalid API key", StringComparison.InvariantCultureIgnoreCase) ||
body.Contains("missing API key", StringComparison.InvariantCultureIgnoreCase))
{
LOGGER.LogWarning("Invalid API key provided for provider {ProviderId}. The response body was: '{ResponseBody}'", this.Id, body);
return [];
}
LOGGER.LogError(e, "Unexpected error while parsing models from Helmholtz API response. Status Code: {StatusCode}. Reason: {ReasonPhrase}. Response Body: '{ResponseBody}'", response.StatusCode, response.ReasonPhrase, body);
return [];
}
catch (Exception e)
{
LOGGER.LogError(e, "Unexpected error while loading models from Helmholtz API. Status Code: {StatusCode}. Reason: {ReasonPhrase}", response.StatusCode, response.ReasonPhrase);
return [];
}
}
}
}