Export (0) Print
Expand All

Accessing the Fault Details in C#

The following example shows how to handle Bing Ads campaign management service fault exceptions. For more information, see Handling Service Errors and Exceptions.

This example has been developed and run within the environment described in Getting Started Using C# and Visual Basic with Bing Ads Services.

System_CLiX_note Note

This example uses the UserName and Password elements for authentication. For Managing User Authentication with OAuth, replace the UserName and Password elements with the AuthenticationToken, which is your OAuth access token.


try
{
    // Run Bing Ads campaign management service request
}
// Campaign Management service operations can throw AdApiFaultDetail.
catch (FaultException<AdApiFaultDetail> fault)
{
    // Log this fault.

    Console.WriteLine("The operation failed with the following faults:\n");

    // The AdApiFaultDetail may contain an AdApiError array named Errors
    foreach (AdApiError error in fault.Detail.Errors)
    {
        Console.WriteLine("AdApiError");
        Console.WriteLine("Code: {0}\nError Code: {1}\nMessage: {2}\n", error.Code, error.ErrorCode, error.Message);

        switch (error.Code)
        {
            case 0:     // InternalError
                break;
            default:
                Console.WriteLine("Please see MSDN documentation for more details about the error code output above.");
                break;
        }
    }

    throw new Exception("", fault);
}
// Some Campaign Management service operations such as AddGoals can throw AnalyticsApiFaultDetail.
catch (FaultException<AnalyticsApiFaultDetail> fault)
{
    // Log this fault.

    Console.WriteLine("The operation failed with the following faults:\n");

    // The AnalyticsApiFaultDetail may contain a GoalError array named GoalErrors
    foreach (GoalError goalError in fault.Detail.GoalErrors)
    {
        Console.WriteLine("GoalError");

        // The GoalError may contain a BatchError array named BatchErrors
        foreach (BatchError error in goalError.BatchErrors)
        {
            Console.WriteLine("BatchError");
            Console.WriteLine("Code: {0}\nError Code: {1}\nMessage: {2}\n", error.Code, error.ErrorCode,
                                error.Message);
            switch (error.Code)
            {
                case 0: // InternalError
                    break;
                default:
                    Console.WriteLine(
                        "Please see MSDN documentation for more details about the error code output above.");
                    break;
            }
        }

        // The GoalError may contain a BatchError array named StepErrors
        foreach (BatchError error in goalError.StepErrors)
        {
            Console.WriteLine("StepError");
            Console.WriteLine("Code: {0}\nError Code: {1}\nMessage: {2}\n", error.Code, error.ErrorCode,
                                error.Message);

            switch (error.Code)
            {
                case 0: // InternalError
                    break;
                default:
                    Console.WriteLine(
                        "Please see MSDN documentation for more details about the error code output above.");
                    break;
            }
        }
    }

    throw new Exception("", fault);
}
// Campaign Management service operations can throw ApiFaultDetail.
catch (FaultException<ApiFaultDetail> fault)
{
    // Log this fault.

    Console.WriteLine("The operation failed with the following faults:\n");

    // The ApiFaultDetail may contain a BatchError array named BatchErrors
    foreach (BatchError error in fault.Detail.BatchErrors)
    {
        Console.WriteLine("BatchError at Index: {0}", error.Index);
        Console.WriteLine("Code: {0}\nError Code: {1}\nMessage: {2}\n", error.Code, error.ErrorCode, error.Message);

        switch (error.Code)
        {
            case 0:     // InternalError
                break;
            default:
                Console.WriteLine("Please see MSDN documentation for more details about the error code output above.");
                break;
        }
    }

    // The ApiFaultDetail may contain an OperationError array named OperationErrors
    foreach (OperationError error in fault.Detail.OperationErrors)
    {
        Console.WriteLine("OperationError");
        Console.WriteLine("Code: {0}\nError Code: {1}\nMessage: {2}\n", error.Code, error.ErrorCode, error.Message);

        switch (error.Code)
        {
            case 0:     // InternalError
                break;
            default:
                Console.WriteLine("Please see MSDN documentation for more details about the error code output above.");
                break;
        }
    }

    throw new Exception("", fault);
}
// Some Campaign Management service operations such as AddKeywords can throw EditorialApiFaultDetail.
catch (FaultException<EditorialApiFaultDetail> fault)
{
    // Log this fault.

    Console.WriteLine("The operation failed with the following faults:\n");

    // The EditorialApiFaultDetail may contain a BatchError array named BatchErrors
    foreach (BatchError error in fault.Detail.BatchErrors)
    {
        Console.WriteLine("BatchError at Index: {0}", error.Index);
        Console.WriteLine("Code: {0}\nError Code: {1}\nMessage: {2}\n", error.Code, error.ErrorCode, error.Message);

        switch (error.Code)
        {
            case 0:     // InternalError
                break;
            default:
                Console.WriteLine("Please see MSDN documentation for more details about the error code output above.");
                break;
        }
    }

    // The EditorialApiFaultDetail may contain an EditorialError array named EditorialErrors
    foreach (EditorialError error in fault.Detail.EditorialErrors)
    {
        Console.WriteLine("EditorialError at Index: {0}", error.Index);
        Console.WriteLine("Code: {0}\nError Code: {1}\nMessage: {2}\n", error.Code, error.ErrorCode, error.Message);
        Console.WriteLine("Appealable: {0}\nDisapproved Text: {1}\nCountry: {2}\n", error.Appealable, error.DisapprovedText, error.PublisherCountry);

        switch (error.Code)
        {
            case 0:     // InternalError
                break;
            default:
                Console.WriteLine("Please see MSDN documentation for more details about the error code output above.");
                break;
        }
    }

    // The EditorialApiFaultDetail may contain an OperationError array named OperationErrors
    foreach (OperationError error in fault.Detail.OperationErrors)
    {
        Console.WriteLine("OperationError");
        Console.WriteLine("Code: {0}\nError Code: {1}\nMessage: {2}\n", error.Code, error.ErrorCode, error.Message);

        switch (error.Code)
        {
            case 0:     // InternalError
                break;
            default:
                Console.WriteLine("Please see MSDN documentation for more details about the error code output above.");
                break;
        }
    }

    throw new Exception("", fault);
}

Community Additions

Show:
© 2014 Microsoft