using Amazon.CloudSearchDomain.Model; using Amazon.Runtime; using Amazon.Runtime.Internal; using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Amazon.CloudSearchDomain.Internal { /// /// Custom pipeline handler to make sure streams are closed in case of exceptions. /// public class ProcessExceptionHandler : PipelineHandler { #if BCL /// /// Override to do extra exception handling if thrown in the pipeline. /// /// public override void InvokeSync(IExecutionContext executionContext) { try { base.InvokeSync(executionContext); } catch (Exception exception) { HandleException(executionContext, exception); throw; } } #endif #if BCL45 /// /// Override to do extra exception handling if thrown in the pipeline. /// /// /// /// public override async System.Threading.Tasks.Task InvokeAsync(IExecutionContext executionContext) { try { return await base.InvokeAsync(executionContext).ConfigureAwait(false); } catch(Exception exception) { HandleException(executionContext, exception); throw; } } #elif AWS_APM_API /// /// Override to do extra exception handling if thrown in the pipeline. /// /// protected override void InvokeAsyncCallback(IAsyncExecutionContext executionContext) { var exception = executionContext.ResponseContext.AsyncResult.Exception; if (executionContext.ResponseContext.AsyncResult.Exception != null) { HandleException(ExecutionContext.CreateFromAsyncContext(executionContext), exception); } // Call outer handler base.InvokeAsyncCallback(executionContext); } #endif #if BCL /// /// Make sure stream is closed in case of exceptions in the pipeline. /// /// /// protected virtual void HandleException(IExecutionContext executionContext, Exception exception) { var uploadDocumentsRequest = executionContext.RequestContext.OriginalRequest as UploadDocumentsRequest; if (uploadDocumentsRequest != null) { // If Documents property is set as the underlying stream for FilePath, dispose it. if (uploadDocumentsRequest.Documents != null && !string.IsNullOrEmpty(uploadDocumentsRequest.FilePath)) { uploadDocumentsRequest.Documents.Dispose(); uploadDocumentsRequest.Documents = null; } } } #endif } }