2

Calling a Cloud Function from an iOS app looks like this:

var functions = Functions.functions() functions.httpsCallable(name).call(data){ (result, error) in // Code here } 

How can I send an error from my Cloud Function, so it will get picked up in the above code? For example:

Cloud Function:

exports.joinGame = functions.https.onCall((data, context) => { return Error("The game is full") }) 

SwiftUI:

var functions = Functions.functions() functions.httpsCallable("joinGame").call(data){ (result, error) in print(error) // "The game is full" } 
6
  • The Cloud Function is just returning an HTTP response. If you catch an error on the function's side, you should return an appropiate HTTP response code: tools.ietf.org/html/rfc7231#section-6 . Then, you handle that HTTP response code from your local code as any regular response from an HTTP request. Commented Nov 24, 2020 at 8:58
  • 1
    It is not clear what do you want to do in SwiftUI. Commented Nov 24, 2020 at 9:02
  • Calling the cloud function will return: (HTTPSCallableResult?, Error?) in SwiftUI - I want to return an error message from my cloud function that will appear under the 2nd parameter of the response (Error). @Asperi Commented Nov 24, 2020 at 9:10
  • 1
    @Paulw11 That makes sense. I will return { error: "error message" } or { success: "success message" } depending on the scenario. Thanks. Commented Nov 24, 2020 at 9:12
  • 1
    Yes, I think that is a better approach. You could also return { "success": true/false, "message":null/"Game full" } or whatever Commented Nov 24, 2020 at 9:16

1 Answer 1

2

I'm still not sure where do you want to return error... but here is some variants of possible handling:

@State private var error: Error? var body: some View { VStack { Text("Wait for Error") .onAppear { functions.httpsCallable("joinGame").call(data){ (result, error) in print(error) // "The game is full" DispatchQueue.main.async { self.error = error } } } if nil != error { Text("Got error!") // << do anything with error here } } .onChange(of: error) { newError in // << or here } } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.