test error

This commit is contained in:
kittapath 2025-10-18 20:09:52 +07:00
parent c91d9d72c5
commit f6b1e7779d
2 changed files with 28 additions and 14 deletions

View file

@ -37,21 +37,32 @@ namespace BMA.EHR.Recruit.Service.Core
{ {
using (var client = new HttpClient()) using (var client = new HttpClient())
{ {
// Set timeout to 30 seconds instead of default 100 seconds
client.Timeout = TimeSpan.FromSeconds(30);
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken.Replace("Bearer ", "")); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken.Replace("Bearer ", ""));
client.DefaultRequestHeaders.Add("api_key", apiKey); client.DefaultRequestHeaders.Add("api_key", apiKey);
var _res = await client.GetAsync(apiPath);
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(30));
var _res = await client.GetAsync(apiPath, cts.Token);
if (_res.IsSuccessStatusCode) if (_res.IsSuccessStatusCode)
{ {
var _result = await _res.Content.ReadAsStringAsync(); var _result = await _res.Content.ReadAsStringAsync();
return _result; return _result;
} }
return string.Empty; return string.Empty;
} }
} }
catch catch (TaskCanceledException)
{ {
throw; // Log timeout but don't throw - return empty result instead
Console.WriteLine($"API call timed out: {apiPath}");
return string.Empty;
}
catch (Exception ex)
{
// Log other exceptions but don't throw - return empty result instead
Console.WriteLine($"API call failed: {apiPath}, Error: {ex.Message}");
return string.Empty;
} }
} }
@ -60,10 +71,10 @@ namespace BMA.EHR.Recruit.Service.Core
try try
{ {
var apiPath = $"{_configuration["API"]}/org/dotnet/keycloak/{keycloakId}"; var apiPath = $"{_configuration["API"]}/org/dotnet/keycloak/{keycloakId}";
var apiKey = _configuration["API_KEY"]; var apiKey = _configuration["API_KEY"] ?? "";
var apiResult = await GetExternalAPIAsync(apiPath, accessToken ?? "", apiKey); var apiResult = await GetExternalAPIAsync(apiPath, accessToken ?? "", apiKey);
if (apiResult != null) if (!string.IsNullOrEmpty(apiResult))
{ {
var raw = JsonConvert.DeserializeObject<GetProfileByKeycloakIdResultLocal>(apiResult); var raw = JsonConvert.DeserializeObject<GetProfileByKeycloakIdResultLocal>(apiResult);
if (raw != null) if (raw != null)
@ -72,9 +83,11 @@ namespace BMA.EHR.Recruit.Service.Core
return null; return null;
} }
catch catch (Exception ex)
{ {
throw; // Log exception but don't throw - return null instead
Console.WriteLine($"GetProfileByKeycloakIdAsync failed for {keycloakId}: {ex.Message}");
return null;
} }
} }

View file

@ -67,6 +67,12 @@ builder.Services.AddTransient<RecruitService>();
builder.Services.AddTransient<MinIOService>(); builder.Services.AddTransient<MinIOService>();
builder.Services.AddTransient<PermissionRepository>(); builder.Services.AddTransient<PermissionRepository>();
// Configure HttpClient with timeout
builder.Services.AddHttpClient("default", client =>
{
client.Timeout = TimeSpan.FromSeconds(30);
});
// use serilog // use serilog
//ConfigureLogs(); //ConfigureLogs();
//builder.Host.UseSerilog(); //builder.Host.UseSerilog();
@ -107,10 +113,6 @@ builder.Services.AddSwaggerGen();
builder.Services.ConfigureOptions<ConfigureSwaggerOptions>(); builder.Services.ConfigureOptions<ConfigureSwaggerOptions>();
builder.Services.AddHealthChecks(); builder.Services.AddHealthChecks();
// Register Service
builder.Services.AddTransient<RecruitService>();
builder.Services.AddTransient<MinIOService>();
var app = builder.Build(); var app = builder.Build();
var apiVersionDescriptionProvider = app.Services.GetRequiredService<IApiVersionDescriptionProvider>(); var apiVersionDescriptionProvider = app.Services.GetRequiredService<IApiVersionDescriptionProvider>();
@ -133,12 +135,11 @@ app.UseHttpsRedirection();
app.UseCors(); app.UseCors();
app.UseAuthentication(); app.UseAuthentication();
app.UseAuthorization(); app.UseAuthorization();
app.UseMiddleware<RequestLoggingMiddleware>();
app.UseDefaultFiles(); app.UseDefaultFiles();
app.UseStaticFiles(); app.UseStaticFiles();
app.MapControllers(); app.MapControllers();
app.UseMiddleware<RequestLoggingMiddleware>();
// apply migrations // apply migrations
await using var scope = app.Services.CreateAsyncScope(); await using var scope = app.Services.CreateAsyncScope();
await using var db = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>(); await using var db = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();