fix: crash on unavailable source api (#4)

* fix: crash on unavailable source api

the screen not updating because of errors

* Change temperature format if null

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit was merged in pull request #4.
This commit is contained in:
2025-08-14 17:55:40 +02:00
committed by GitHub
parent 8284ae9695
commit 32b136d4cc
11 changed files with 338 additions and 96 deletions

View File

@@ -1,5 +1,6 @@
using System.Dynamic;
using System.Reflection;
using HomeApi.Extensions;
using HomeApi.Models.Configuration;
using MediatR;
using Microsoft.Extensions.Options;
@@ -22,8 +23,8 @@ public static class ImageGeneration
public async Task<Stream> Handle(Command request, CancellationToken cancellationToken)
{
var weather = await mediator.Send(new Weather.Command(), cancellationToken);
var departureBoard = await mediator.Send(new DepartureBoard.Command(), cancellationToken);
var weather = await mediator.TrySendAsync(new Weather.Command(), cancellationToken);
var departureBoard = await mediator.TrySendAsync(new DepartureBoard.Command(), cancellationToken);
var model = new Models.Image
{
@@ -31,8 +32,6 @@ public static class ImageGeneration
TimeTable = departureBoard
};
if(weather is null)
throw new Exception("Weather data not found");
var engine = new RazorLightEngineBuilder()
.SetOperatingAssembly(Assembly.GetExecutingAssembly())
@@ -59,7 +58,7 @@ public static class ImageGeneration
{
var browserFetcher = new BrowserFetcher();
await browserFetcher.DownloadAsync();
var browser = await Puppeteer.LaunchAsync(new LaunchOptions
await using var browser = await Puppeteer.LaunchAsync(new LaunchOptions
{
Headless = true,
Args = ["--disable-gpu"]

View File

@@ -35,16 +35,27 @@ public static class Weather
public async Task<WeatherInformation> Handle(Command request, CancellationToken cancellationToken)
{
var coordinates = await _geocodingService.GetCoordinatesAsync(_apiConfiguration.DefaultCity);
if (coordinates is null)
throw new Exception("Coordinates not found");
var coordinates = await _geocodingService.TryCallAsync(service =>
service.GetCoordinatesAsync(_apiConfiguration.DefaultCity),
_logger,
"Failed to get coordinates"
);
var aurora = await _auroraService.TryCallAsync(service =>
service.GetAuroraForecastAsync(coordinates?.Lat ?? "0.00", coordinates?.Lon ?? "0.00"),
_logger,
"Failed to get aurora forecast"
);
var weather = await _weatherService.TryCallAsync(service =>
service.GetWeatherAsync(coordinates?.Lat ?? string.Empty, coordinates?.Lon ?? string.Empty),
_logger,
"Failed to get weather data"
);
var aurora = await _auroraService.GetAuroraForecastAsync(coordinates.Lat, coordinates.Lon);
var weather = await _weatherService.GetWeatherAsync(coordinates.Lat, coordinates.Lon);
var forecasts = weather?.Forecast.Forecastday.Select(day => day.ToForecast()).ToList();
var forecasts = weather.Forecast.Forecastday.Select(day => day.ToForecast()).ToList();
return weather.ToContract(coordinates.Name, aurora, forecasts);
return weather?.ToContract(coordinates?.Name, aurora, forecasts) ?? new WeatherInformation();
}
}
}