Instagram Error handling

Error handling

The Instagram API may respond with an error for a number of different reasons - eg. if you have specified an invalid access token, exceeded the rate limiting or request a resource that doesn't exist. When the API responds with an error, Skybrud.Social will throw an exception of the type InstagramHttpException or one of the classes inheriting from this class.

To make sure your code doesn't break in the event of any errors returned by the API, you should always put a try/catch clause around it.

Exception types

OAuth

The Instagram API may respond with a number of different errors as described by OAuth 2.0 - eg. if the fails to authenticate you based on the specified access token, the API will respond with an OAuth error. For these OAuth errors, Skybrud.Social will trigger an exception of the type InstagramOAuthException (or one of the classes inheriting from it).

Type Description
InstagramOAuthAccessTokenException The specified access token isn't valid.
InstagramOAuthForbiddenException Access to the requested resource is forbidden - eg. signed request are enabled for your app, put haven't enabled this in Skybrud.Social.
InstagramOAuthParameterException An OAuth parameter is missing - eg. if you haven't specified an access token.
InstagramOAuthPermissionsException The authenticated user (or your app really) hasn't been granted the required scope to access the resource.
InstagramOAuthRateLimitException The rate limiting of the authenticated user has been exceeded.

Not found

In a similar way to OAuth exceptions, Skybrud.Social will throw an exception if you request a resource in the Instagram API that doesn't exist. The exception will be of the type InstagramNotFoundException.

Other errors

For any other errors received from the API, an instance of InstagramHttpException will be thrown instead. The error message provided by the Instagram API is available through the Message property of the exception class.

The Razor example below illustrates how you can catch the different exceptions:

@using Skybrud.Social.Instagram.Exceptions
@using Skybrud.Social.Instagram.Objects.Locations
@using Skybrud.Social.Instagram.Responses.Locations
@inherits WebViewPage<Skybrud.Social.Instagram.InstagramService>

@{

    try {

        // Request information about a non-existing location
        InstagramGetLocationResponse response = Model.Locations.GetLocation(0);

        // Get the location from the response
        InstagramLocation location = response.Body.Data;

        // Print some information about the location
        <p>ID: @location.Id</p>
        <p>Name: @location.Name</p>
        <p>Latitude: @location.Latitude</p>
        <p>Longitude: @location.Longitude</p>

    } catch (InstagramOAuthParameterException) {

        <div class="alert alert-danger">Have you specified an access token?</div>

    } catch (InstagramOAuthAccessTokenException) {

        <div class="alert alert-danger">Invalid access token.</div>

    } catch (InstagramOAuthForbiddenException) {

        <div class="alert alert-danger">Forbidden.</div>

    } catch (InstagramOAuthPermissionsException) {

        <div class="alert alert-danger">Insufficient permissions.</div>

    } catch (InstagramOAuthRateLimitException) {

        <div class="alert alert-danger">Rate limit exceeded.</div>

    } catch (InstagramOAuthException ex) {

        <div class="alert alert-danger">Bad OAuth: @ex.Message</div>

    } catch (InstagramNotFoundException) {

        <div class="alert alert-danger">The requested location does not exist.</div>

    } catch (InstagramHttpException ex) {

        <div class="alert alert-danger">The Instagram API responded with an error: @ex.Message</div>

    } catch (Exception) {

        <div class="alert alert-danger">Oh noes. Something bad happened.</div>

    }

}

But as described earlier, all of the Instagram exception classes inherits from the InstagramHttpException class, so you can just check for this alone instead:

@using Skybrud.Social.Instagram.Exceptions
@using Skybrud.Social.Instagram.Objects.Locations
@using Skybrud.Social.Instagram.Responses.Locations
@inherits WebViewPage<Skybrud.Social.Instagram.InstagramService>

@{

    try {

        // Request information about a non-existing location
        InstagramGetLocationResponse response = Model.Locations.GetLocation(0);

        // Get the location from the response
        InstagramLocation location = response.Body.Data;

        // Print some information about the location
        <p>ID: @location.Id</p>
        <p>Name: @location.Name</p>
        <p>Latitude: @location.Latitude</p>
        <p>Longitude: @location.Longitude</p>

    } catch (InstagramHttpException ex) {

        <div class="alert alert-danger">The Instagram API responded with an error: @ex.Message</div>

    } catch (Exception) {

        <div class="alert alert-danger">Oh noes. Something bad happened.</div>

    }

}