UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Cupcake Corner 2nd challenge - please some hints

Forums > 100 Days of SwiftUI

hi,

i have problem with second challenge in Cupcake Corner.

Can you give me some hints where to start or what should i use? Do i have to add another catch block? Create some enum with cases? It has to me specific error to no intenret conection or if function fails?

"If our call to placeOrder() fails – for example if there is no internet connection – show an informative alert for the user. To test this, try commenting out the request.httpMethod = "POST" line in your code, which should force the request to fail. "

3      

Hi! Most probably you already have this line of code in the project.

let (data, _) = try await URLSession.shared.upload(for: request, from: encoded)

if you option + click on upload in the pop up help you will notice that this function can throw error

func upload(
    for request: URLRequest,
    from bodyData: Data,
    delegate: (URLSessionTaskDelegate)? = nil
) async throws -> (Data, URLResponse)

which means that you need to wrap it in do catch block. So if there are errors thrown you can use the error and show it to the user. Errors already "pre-defined" so you don't really need to create your own.

So make the long story short in catch block you can use that error and assign it to a variable and then use another variable to trigger the alert.

catch {
            errorMessage = "Sorry, checkout failed.\n\nMessage: \(error.localizedDescription)"
            showingError = true
        }

so when there is an errro you can catch it and toggle showingError message and it will be shown to the user.

3      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.