/// <summary> /// Try to parse an Elasticsearch <see cref="ServerError"/>, this only works if /// <see cref="ITransportConfigurationValues.DisableDirectStreaming"/> gives us access to <see cref="IApiCallDetails.RequestBodyInBytes"/> /// </summary> public static bool TryGetElasticsearchServerError(this ITransportResponse response, out ServerError serverError) { serverError = null; var bytes = response.ApiCall.ResponseBodyInBytes; if (bytes == null || response.ApiCall.ResponseMimeType != RequestData.MimeType) { return(false); } var settings = response.ApiCall.ConnectionConfiguration; using var stream = settings.MemoryStreamFactory.Create(bytes); return(ServerError.TryCreate(stream, out serverError)); }
private static void ThrowBadAuthPipelineExceptionWhenNeeded(IApiCallDetails details, ITransportResponse response = null) { if (details?.HttpStatusCode == 401) { throw new PipelineException(PipelineFailure.BadAuthentication, details.OriginalException) { Response = response, ApiCall = details } } ; }
private Exception ThrowOnBadBulk(ITransportResponse response, string message) { _incrementFailed(); _partitionedBulkRequest.BackPressure?.Release(); return(Throw(message, response.ApiCall)); }
// ReSharper disable once ParameterOnlyUsedForPreconditionCheck.Local private void HandleTransportException(RequestData data, Exception clientException, ITransportResponse response) { if (response.ApiCall is ApiCallDetails a) { //if original exception was not explicitly set during the pipeline //set it to the TransportException we created for the bad response if (clientException != null && a.OriginalException == null) { a.OriginalException = clientException; } //On .NET Core the IConnection implementation throws exceptions on bad responses //This causes it to behave differently to .NET FULL. We already wrapped the WebException //under TransportException and it exposes way more information as part of it's //exception message e.g the the root cause of the server error body. #if !DOTNETCORE if (a.OriginalException is WebException) { a.OriginalException = clientException; } #endif } Settings.OnRequestCompleted?.Invoke(response.ApiCall); if (data != null && (clientException != null && data.ThrowExceptions)) { throw clientException; } }