/// <summary> /// Parses the task exception. /// </summary> /// <param name="e">The <see cref="AggregateException"/> to parse.</param> /// <returns>The parsed <see cref="GoogleAdsBaseException"/> if parsing is successful; /// The underlying <see cref="RpcException" /> if the exception cannot be parsed as a /// GoogleAdsException.</returns> /// <remarks><code>AggregateException</code> is very close to a catch-all exception for /// Tasks. While all the situations that we know of involves this exception being thrown /// by an underlying <code>RpcException</code>, theoretically this method may return a /// null object, if the <code>AggregateException</code> is not caused by an /// <code>RpcException</code>. That would typically indicate an underlying issue with /// the code.</remarks> internal static RpcException ParseTaskException(AggregateException e) { RpcException rpcException = ExtractRpcException(e); GoogleAdsBaseException googleAdsException = ParseRpcException(rpcException); return((googleAdsException == null) ? rpcException : googleAdsException); }
/// <summary> /// Creates an <see cref="RpcException"/> for testing purposes. /// </summary> internal static RpcException CreateRpcException(string errorMessage, string errorTrigger, string requestId) { GoogleAdsFailure failure = new GoogleAdsFailure(); failure.Errors.Add(new GoogleAdsError() { ErrorCode = new ErrorCode() { DistinctError = DistinctErrorEnum.Types.DistinctError.DuplicateElement }, Message = errorMessage, Trigger = new Value() { StringValue = errorTrigger } }); Metadata metadata = new Metadata(); using (MemoryStream memoryStream = new MemoryStream()) { failure.WriteTo(memoryStream); metadata.Add(GoogleAdsBaseException.GetFailureKeyFromTypeName( typeof(GoogleAdsException)), memoryStream.ToArray()); metadata.Add(MetadataKeyNames.RequestId, requestId); } return(new RpcException(Status.DefaultSuccess, metadata)); }