public override Task <EnrollmentGroup> UpdateEnrollmentGroupAsync(EnrollmentGroup enrollmentGroup, bool forceUpdate, CancellationToken cancellationToken) { ThrowIfClosed(); ValidateEnrollmentGroup(enrollmentGroup); if (string.IsNullOrWhiteSpace(enrollmentGroup.ETag) && !forceUpdate) { throw new ArgumentException(ApiResources.ETagNotSetWhileUpdatingEnrollmentGroup); } var errorMappingOverrides = new Dictionary <HttpStatusCode, Func <HttpResponseMessage, Task <Exception> > > { { HttpStatusCode.Conflict, responseMessage => Task.FromResult((Exception) new EnrollmentGroupAlreadyExistsException(enrollmentGroup.EnrollmentGroupId)) }, { HttpStatusCode.PreconditionFailed, async(responseMessage) => new PreconditionFailedException(await ExceptionHandlingHelper.GetExceptionMessageAsync(responseMessage)) } }; PutOperationType operationType = forceUpdate ? PutOperationType.ForceUpdateEntity : PutOperationType.UpdateEntity; return(this.httpClientHelper.PutAsync(GetEnrollmentGroupUri(enrollmentGroup.EnrollmentGroupId), enrollmentGroup, operationType, errorMappingOverrides, cancellationToken)); }
private static async Task <Exception> MapToExceptionAsync( HttpResponseMessage response, IDictionary <HttpStatusCode, Func <HttpResponseMessage, Task <Exception> > > errorMapping) { if (!errorMapping.TryGetValue(response.StatusCode, out Func <HttpResponseMessage, Task <Exception> > func)) { return(new IotHubException( await ExceptionHandlingHelper.GetExceptionMessageAsync(response).ConfigureAwait(false), isTransient: true)); } Func <HttpResponseMessage, Task <Exception> > mapToExceptionFunc = errorMapping[response.StatusCode]; Task <Exception> exception = mapToExceptionFunc(response); return(await exception.ConfigureAwait(false)); }
static async Task <Exception> MapToExceptionAsync( HttpResponseMessage response, IDictionary <HttpStatusCode, Func <HttpResponseMessage, Task <Exception> > > errorMapping) { Func <HttpResponseMessage, Task <Exception> > func; if (!errorMapping.TryGetValue(response.StatusCode, out func)) { return(new IotHubException( await ExceptionHandlingHelper.GetExceptionMessageAsync(response), isTransient: true)); } var mapToExceptionFunc = errorMapping[response.StatusCode]; var exception = mapToExceptionFunc(response); return(await exception); }