docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Asynchronous errors

    Handle asynchronous exceptions in the Instant Games SDK.

    API endpoints that return WebTask or WebTask<T> and use the suffix Async in their names might throw asynchronous exceptions when awaited. The Instant Games SDK for Unity uses type Meta.InstantGames.APIError to represent these exceptions.

    For details about async endpoints, refer to Call asynchronous methods.

    Try/catch in asynchronous functions

    If you're using async functions, enclose the await expression in a try block to catch and handle asynchronous errors:

    var task = Meta.InstantGames.FBInstant.CreateShortcutAsync();
    try
    {
        await task;
    }
    catch (APIError e)
    {
        // respond to the failure
    }
    

    Asynchronous error handling using coroutines

    When using coroutines for asynchronous operations, asynchronous errors don't throw automatically. Check the completed task manually via the property IsFaulted to discover any errors:

    IEnumerator GetPlayersCoroutine()
    {
        var task = FBInstant.Player.GetConnectedPlayersAsync();
        yield return task.WaitForCompletion();
        if (task.IsFaulted)
        {
            // respond to the error
        }
        else
        {
            var players = task.Result;
            // do something with the result
        }
    }
    

    Handling different types of APIError

    APIError has a property Code, of type Meta.InstantGames.ErrorCode, that provides more detail on the reason for the failure. You can use this property to respond differently to different types of failures:

    catch (APIError e)
    {
        switch (e.Code)
        {
            case ErrorCode.USER_INPUT:
                // respond to user input error
                break;
            case ErrorCode.PENDING_REQUEST:
                // respond to pending request error
                break;
            // ...
        }
    }
    

    Additional resources

    • Error handling
    • Synchronous errors
    • Synchronous errors thrown by async methods
    In This Article
    Back to top
    Copyright © 2025 Unity Technologies — Trademarks and terms of use
    • Legal
    • Privacy Policy
    • Cookie Policy
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)