コガネブログ

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

【Unity】AggregateException から FirebaseException を取得する拡張メソッド

ソースコード

using System;
using System.Linq;
using Firebase;

namespace Kogane
{
    public static class AggregateExceptionExtensionMethods
    {
        public static bool IsFirebaseException( this AggregateException self )
        {
            return self
                    .Flatten()
                    .InnerExceptions.Any( x => x is FirebaseException )
                ;
        }

        public static FirebaseException GetFirebaseException( this AggregateException self )
        {
            return self
                    .Flatten()
                    .InnerExceptions
                    .OfType<FirebaseException>()
                    .First()
                ;
        }
    }
}

使用例

using System;
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.IsFirebaseException() )
        {
            var firebaseException = e.GetFirebaseException();
            Debug.LogError( firebaseException.GetAuthError() );
        }
    }
}