Handle errors
This commit is contained in:
parent
8a18e71a6f
commit
04d00e0fe9
|
|
@ -16,11 +16,14 @@ namespace AS1024.GeoFeed.Controllers
|
|||
private readonly IGeoFeedProvider builder;
|
||||
private readonly IMemoryCache memoryCache;
|
||||
private readonly IWebHostEnvironment environment;
|
||||
private readonly ILogger<GeofeedController> logger;
|
||||
private const string GeoFeedCacheKey = "GeoFeedData";
|
||||
|
||||
public GeofeedController(IGeoFeedProvider builder,
|
||||
IMemoryCache memoryCache,
|
||||
IWebHostEnvironment environment) {
|
||||
IWebHostEnvironment environment,
|
||||
ILogger<GeofeedController> logger) {
|
||||
this.logger = logger;
|
||||
this.builder = builder;
|
||||
this.memoryCache = memoryCache;
|
||||
this.environment = environment;
|
||||
|
|
@ -30,16 +33,17 @@ namespace AS1024.GeoFeed.Controllers
|
|||
[Route("")]
|
||||
public async Task<IActionResult> Get()
|
||||
{
|
||||
if (!memoryCache.TryGetValue(GeoFeedCacheKey, out List<IPGeoFeed>? feed)
|
||||
&& environment.IsProduction())
|
||||
try
|
||||
{
|
||||
if (!memoryCache.TryGetValue(GeoFeedCacheKey, out List<IPGeoFeed>? feed))
|
||||
{
|
||||
feed = await builder.GetGeoFeedData();
|
||||
if (environment.IsProduction())
|
||||
{
|
||||
MemoryCacheEntryOptions cacheEntryOptions = new MemoryCacheEntryOptions()
|
||||
.SetSlidingExpiration(TimeSpan.FromMinutes(15));
|
||||
memoryCache.Set(GeoFeedCacheKey, feed, cacheEntryOptions);
|
||||
} else
|
||||
{
|
||||
feed = await builder.GetGeoFeedData();
|
||||
}
|
||||
}
|
||||
|
||||
string csvContent = feed.ToGeoFeedCsv(); // Assuming ToGeoFeedCsv() returns a string in CSV format.
|
||||
|
|
@ -50,6 +54,11 @@ namespace AS1024.GeoFeed.Controllers
|
|||
{
|
||||
FileDownloadName = "geofeed.csv"
|
||||
};
|
||||
} catch (Exception ex)
|
||||
{
|
||||
logger.LogError($"Geofeed generation failed. Exception: {ex}");
|
||||
return StatusCode(500);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue