Baseball
Baseball

Reputation: 5

Constant error handling issues when trying to implement Tomorrow.io functionality API into my app

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

Answers (1)

lorem ipsum
lorem ipsum

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

Related Questions