Skip to content

Commit 56d4c18

Browse files
authored
Refactored Functions’s Result Handling (#14086)
1 parent 15caf49 commit 56d4c18

File tree

1 file changed

+31
-37
lines changed

1 file changed

+31
-37
lines changed

FirebaseFunctions/Sources/Functions.swift

Lines changed: 31 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -401,11 +401,9 @@ enum FunctionsConstants {
401401

402402
do {
403403
let rawData = try await fetcher.beginFetch()
404-
return try callableResultFromResponse(data: rawData, error: nil)
404+
return try callableResult(fromResponseData: rawData)
405405
} catch {
406-
// This method always throws when `error` is not `nil`, but ideally,
407-
// it should be refactored so it looks less confusing.
408-
return try callableResultFromResponse(data: nil, error: error)
406+
throw processedError(fromResponseError: error)
409407
}
410408
}
411409

@@ -455,10 +453,16 @@ enum FunctionsConstants {
455453

456454
fetcher.beginFetch { [self] data, error in
457455
let result: Result<HTTPSCallableResult, any Error>
458-
do {
459-
result = try .success(callableResultFromResponse(data: data, error: error))
460-
} catch {
461-
result = .failure(error)
456+
if let error {
457+
result = .failure(processedError(fromResponseError: error))
458+
} else if let data {
459+
do {
460+
result = try .success(callableResult(fromResponseData: data))
461+
} catch {
462+
result = .failure(error)
463+
}
464+
} else {
465+
result = .failure(FunctionsError(.internal))
462466
}
463467

464468
DispatchQueue.main.async {
@@ -519,46 +523,36 @@ enum FunctionsConstants {
519523
return fetcher
520524
}
521525

522-
private func callableResultFromResponse(data: Data?,
523-
error: (any Error)?) throws -> HTTPSCallableResult {
524-
let processedData = try processedResponseData(from: data, error: error)
526+
private func processedError(fromResponseError error: any Error) -> any Error {
527+
let error = error as NSError
528+
let localError: (any Error)? = if error.domain == kGTMSessionFetcherStatusDomain {
529+
FunctionsError(
530+
httpStatusCode: error.code,
531+
body: error.userInfo["data"] as? Data,
532+
serializer: serializer
533+
)
534+
} else if error.domain == NSURLErrorDomain, error.code == NSURLErrorTimedOut {
535+
FunctionsError(.deadlineExceeded)
536+
} else { nil }
537+
538+
return localError ?? error
539+
}
540+
541+
private func callableResult(fromResponseData data: Data) throws -> HTTPSCallableResult {
542+
let processedData = try processedData(fromResponseData: data)
525543
let json = try responseDataJSON(from: processedData)
526544
// TODO: Refactor `decode(_:)` so it either returns a non-optional object or throws
527545
let payload = try serializer.decode(json)
528546
// TODO: Remove `as Any` once `decode(_:)` is refactored
529547
return HTTPSCallableResult(data: payload as Any)
530548
}
531549

532-
private func processedResponseData(from data: Data?, error: (any Error)?) throws -> Data {
533-
// Case 1: `error` is not `nil` -> always throws
534-
if let error = error as NSError? {
535-
let localError: (any Error)?
536-
if error.domain == kGTMSessionFetcherStatusDomain {
537-
localError = FunctionsError(
538-
httpStatusCode: error.code,
539-
body: data ?? error.userInfo["data"] as? Data,
540-
serializer: serializer
541-
)
542-
} else if error.domain == NSURLErrorDomain, error.code == NSURLErrorTimedOut {
543-
localError = FunctionsError(.deadlineExceeded)
544-
} else {
545-
localError = nil
546-
}
547-
548-
throw localError ?? error
549-
}
550-
551-
// Case 2: `data` is `nil` -> always throws
552-
guard let data else {
553-
throw FunctionsError(.internal)
554-
}
555-
556-
// Case 3: `data` is not `nil` but might specify a custom error -> throws conditionally
550+
private func processedData(fromResponseData data: Data) throws -> Data {
551+
// `data` might specify a custom error. If so, throw the error.
557552
if let bodyError = FunctionsError(httpStatusCode: 200, body: data, serializer: serializer) {
558553
throw bodyError
559554
}
560555

561-
// Case 4: `error` is `nil`; `data` is not `nil`; `data` doesn’t specify an error -> OK
562556
return data
563557
}
564558

0 commit comments

Comments
 (0)