Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement FirestoreErrorCode #39

Merged
merged 5 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sources/FirebaseFirestore/DocumentReference+Swift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ extension DocumentReference {
self, { snapshot, errorCode, errorMessage, pvListener in
let callback = Unmanaged<AnyObject>.fromOpaque(pvListener!).takeUnretainedValue() as! ListenerCallback

let error = NSError.firestore(errorCode, errorMessage: errorMessage)
let error = FirestoreErrorCode(errorCode, errorMessage: errorMessage)
// We only return a snapshot if the error code isn't 0 (aka the 'ok' error code)
let returned = error == nil ? snapshot?.pointee : nil

Expand Down
47 changes: 47 additions & 0 deletions Sources/FirebaseFirestore/FirestoreErrorCode.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// SPDX-License-Identifier: BSD-3-Clause

public struct FirestoreErrorCode: Error {
public typealias Code = firebase.firestore.Error

public let code: Code
public let localizedDescription: String

public init(_ error: firebase.firestore.Error, errorMessage: String?) {
code = error
localizedDescription = errorMessage ?? "\(code.rawValue)"
}

public init?(_ error: firebase.firestore.Error?, errorMessage: UnsafePointer<CChar>? = nil) {
guard let actualError = error, actualError.rawValue != 0 else { return nil }
var errorMessageString: String?
if let errorMessage {
errorMessageString = .init(cString: errorMessage)
}
self.init(actualError, errorMessage: errorMessageString)
}
}

extension FirestoreErrorCode: Equatable {}

// Unfortunately `Error` is not defined as a `enum class` so we need to add
// these wrappers.
extension FirestoreErrorCode.Code {
public static var ok: Self { firebase.firestore.kErrorOk }
public static var none: Self { firebase.firestore.kErrorNone }
public static var cancelled: Self { firebase.firestore.kErrorCancelled }
public static var unknown: Self { firebase.firestore.kErrorUnknown }
public static var invalidArgument: Self { firebase.firestore.kErrorInvalidArgument }
public static var deadlineExceeded: Self { firebase.firestore.kErrorDeadlineExceeded }
public static var notFound: Self { firebase.firestore.kErrorNotFound }
public static var alreadyExists: Self { firebase.firestore.kErrorAlreadyExists }
public static var permissionDenied: Self { firebase.firestore.kErrorPermissionDenied }
public static var resourceExhausted: Self { firebase.firestore.kErrorResourceExhausted }
public static var failedPrecondition: Self { firebase.firestore.kErrorFailedPrecondition }
public static var aborted: Self { firebase.firestore.kErrorAborted }
public static var outOfRange: Self { firebase.firestore.kErrorOutOfRange }
public static var unimplemented: Self { firebase.firestore.kErrorUnimplemented }
public static var `internal`: Self { firebase.firestore.kErrorInternal }
public static var unavailable: Self { firebase.firestore.kErrorUnavailable }
public static var dataLoss: Self { firebase.firestore.kErrorDataLoss }
public static var unauthenticated: Self { firebase.firestore.kErrorUnauthenticated }
}
25 changes: 0 additions & 25 deletions Sources/FirebaseFirestore/NSError+FirestoreError.swift

This file was deleted.

2 changes: 1 addition & 1 deletion Sources/FirebaseFirestore/Query+Swift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ extension QueryProtocol {
_asQuery, { snapshot, errorCode, errorMessage, pvListener in
let callback = Unmanaged<AnyObject>.fromOpaque(pvListener!).takeUnretainedValue() as! ListenerCallback

let error = NSError.firestore(errorCode, errorMessage: errorMessage)
let error = FirestoreErrorCode(errorCode, errorMessage: errorMessage)
// We only return a snapshot if the error code isn't 0 (aka the 'ok' error code)
let returned = error == nil ? snapshot?.pointee : nil

Expand Down
2 changes: 1 addition & 1 deletion Sources/FirebaseFirestore/Transaction+Swift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ extension Transaction {
let snapshot = Get(document, &error, &errorMessage)

if error != firebase.firestore.kErrorNone {
throw NSError.firestore(error, errorMessage: String(errorMessage))
throw FirestoreErrorCode(error, errorMessage: String(errorMessage))
}

return snapshot
Expand Down
Loading