Reputation: 5
every time I add the following code function, I get the error "errors thrown from here are not handled" on the line shown here:
let (data, _) = try await URLSession.shared.data(for: request)
This has been an issue as I am unable to follow through on the API request and am struggling to understand the nature of the issue as I have tried various strategies that were shown on Stack Overflow.
Here is my entire function:
import Foundation
func WeatherAPI() async {
let url = URL(string: "https://api.tomorrow.io/v4/weather/forecast")!
var components = URLComponents(url: url, resolvingAgainstBaseURL: true)!
let queryItems: [URLQueryItem] = [
URLQueryItem(name: "location", value: "Location..."),
URLQueryItem(name: "apikey", value: "API Code Not Going to be shown"),
]
components.queryItems = components.queryItems.map { $0 + queryItems } ?? queryItems
var request = URLRequest(url: components.url!)
request.httpMethod = "GET"
request.timeoutInterval = 10
request.allHTTPHeaderFields = [
"accept": "application/json",
"accept-encoding": "deflate, gzip, br"
]
let (data, _) = try await URLSession.shared.data(for: request)
print(String(decoding: data, as: UTF8.self))
}
Upvotes: 0
Views: 36
Reputation: 29614
Change this
func WeatherAPI() async {
To
func weatherAPI() async throws {
And eventually you need
do {
try await weatherAPI()
} catch {
print(error)
// show error to user
}
Upvotes: 1