コガネブログ

平日更新を目標に Unity や C#、Visual Studio、ReSharper などのゲーム開発アレコレを書いていきます

【Unity】FirebaseException の ErrorCode を列挙型に変換する拡張メソッド

ソースコード

using Firebase;
using Firebase.Auth;

namespace Kogane
{
    public static class FirebaseExceptionExtensionMethods
    {
        public static AuthError GetAuthError( this FirebaseException self )
        {
            return ( AuthError )self.ErrorCode;
        }
    }
}

使用例

using System;
using System.Linq;
using Firebase;
using Firebase.Auth;
using Kogane;
using UnityEngine;

public class Example : MonoBehaviour
{
    private async void Start()
    {
        try
        {
            var auth = FirebaseAuth.DefaultInstance;
            var user = await auth.CreateUserWithEmailAndPasswordAsync( "", "" );
        }
        catch ( AggregateException e ) when ( e.Flatten().InnerExceptions.Any( x => x is FirebaseException ) )
        {
            foreach ( var firebaseException in e.Flatten().InnerExceptions.OfType<FirebaseException>() )
            {
                Debug.LogError( firebaseException.GetAuthError() );
            }
        }
    }
}