LoginSignup
3
3

【Swift】Firestoreのエラーをローカライズする

Posted at

はじめに

Firestoreは便利ですが、SDKで定義されているエラーのerrorDescriptionは英語でそのまま使うとユーザーにとってわかりにくいものになってしまいます。
そこでFirestoreのエラーを自前でローカライズしてみようという記事です。

実装

StringCatalogを使うためにString(localized:)で実装しています

import FirebaseFirestore
import FirebaseFirestoreSwift
import Foundation

public enum PostError: Error, LocalizedError {
    case firestore(FirestoreErrorCode.Code)

    public var errorDescription: String? {
        switch self {
        
        case .firestore(.OK):
            return String(localized: "操作は正常に完了しました")
        
        case .firestore(.cancelled):
            return String(localized: "操作がキャンセルされました")
        
        case .firestore(.unknown):
            return String(localized: "不明なエラーが発生しました")
        
        case .firestore(.invalidArgument):
            return String(localized: "無効な引数が指定されました")
        
        case .firestore(.deadlineExceeded):
            return String(localized: "操作がタイムアウトしました")
        
        case .firestore(.notFound):
            return String(localized: "ドキュメントが見つかりません")
        
        case .firestore(.alreadyExists):
            return String(localized: "作成しようとしたドキュメントは既に存在しています")
        
        case .firestore(.permissionDenied):
            return String(localized: "アクセス許可が拒否されました")
        
        case .firestore(.resourceExhausted):
            return String(localized: "リソースが枯渇しています")
        
        case .firestore(.failedPrecondition):
            return String(localized: "操作が拒否されました")
        
        case .firestore(.aborted):
            return String(localized: "操作が中止されました")
        
        case .firestore(.outOfRange):
            return String(localized: "許可されていない操作です")
        
        case .firestore(.unimplemented):
            return String(localized: "未実装またはサポートされていません")
        
        case .firestore(.internal):
            return String(localized: "内部エラーが発生しました")
        
        case .firestore(.unavailable):
            return String(localized: "サービスが利用できません")
        
        case .firestore(.dataLoss):
            return String(localized: "データの損失または破損しています")
        
        case .firestore(.unauthenticated):
            return String(localized: "認証情報が無効です")
        
        case .firestore:
            return String(localized: "不明なエラーが発生しました")
        }
    }
}

使い方

private func fetchSample() async throws -> [Sample] {
    do {
        /// ... 処理

        let querySnapshot = try await query.getDocuments()
        
        /// ... 処理

        return querySnapshot.documents
+   } catch let error as NSError where error.domain == FirestoreErrorDomain {
+       let firestoreErrorCode = FirestoreErrorCode.Code(rawValue: error.code) ?? .internal
+       throw PostError.firestore(firestoreErrorCode)
    } catch {
        throw error
    }
}

おわり

Firestoreのエラーを日本語対応する事ができました

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3