Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

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.


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;
        }
    }
}
// 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;
        }
    }
}
// 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;
        }
    }
}

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.