Skip to content

Commit 178a174

Browse files
committed
Fix error handling for invalid clock times and task cancellations
- Add validation in LocalClockTime.init(dto:) to throw WeatherDTOMappingError.invalidClockTime instead of crashing on invalid hour/minute values - Add CancellationError handling in loadWeather to prevent false error banners when location changes cancel in-flight requests
1 parent 4ae2d3b commit 178a174

2 files changed

Lines changed: 6 additions & 0 deletions

File tree

example_projects/Weather/Weather/ContentView.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@ struct ContentView: View {
150150

151151
do {
152152
report = try await weatherService.weather(for: locationID)
153+
} catch is CancellationError {
154+
return
153155
} catch {
154156
weatherErrorMessage = "Weather unavailable"
155157
}

example_projects/Weather/Weather/Services/WeatherClientDTOs.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ enum WeatherDTOMappingError: Error, Equatable {
142142
case missingWeekday
143143
case invalidWeekday(Int)
144144
case invalidWindDirection(Double)
145+
case invalidClockTime(hour: Int, minute: Int)
145146
}
146147

147148
extension WeatherReport {
@@ -241,6 +242,9 @@ extension DailyForecast {
241242

242243
private extension LocalClockTime {
243244
init(dto: LocalClockTimeDTO) throws {
245+
guard (0...23).contains(dto.hour), (0...59).contains(dto.minute) else {
246+
throw WeatherDTOMappingError.invalidClockTime(hour: dto.hour, minute: dto.minute)
247+
}
244248
self.init(hour: dto.hour, minute: dto.minute)
245249
}
246250
}

0 commit comments

Comments
 (0)