Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance the error that occurs during the http request. #10387

Merged
merged 4 commits into from
Nov 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ protected virtual RemoteServiceErrorInfo CreateErrorInfoWithoutCode(Exception ex

exception = TryToGetActualException(exception);

if (exception is AbpRemoteCallException remoteCallException)
if (exception is AbpRemoteCallException remoteCallException && remoteCallException.Error != null)
{
return remoteCallException.Error;
}
Expand All @@ -76,7 +76,7 @@ protected virtual RemoteServiceErrorInfo CreateErrorInfoWithoutCode(Exception ex

var errorInfo = new RemoteServiceErrorInfo();

if (exception is IUserFriendlyException)
if (exception is IUserFriendlyException || exception is AbpRemoteCallException)
{
errorInfo.Message = exception.Message;
errorInfo.Details = (exception as IHasErrorDetails)?.Details;
Expand Down Expand Up @@ -175,7 +175,7 @@ protected virtual RemoteServiceErrorInfo CreateEntityNotFoundError(EntityNotFoun

return new RemoteServiceErrorInfo(exception.Message);
}

protected virtual Exception TryToGetActualException(Exception exception)
{
if (exception is AggregateException && exception.InnerException != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,20 @@ public AbpRemoteCallException()

}

public AbpRemoteCallException(string message, Exception innerException = null)
: base(message, innerException)
{

}

public AbpRemoteCallException(SerializationInfo serializationInfo, StreamingContext context)
: base(serializationInfo, context)
{

}

public AbpRemoteCallException(RemoteServiceErrorInfo error)
: base(error.Message)
public AbpRemoteCallException(RemoteServiceErrorInfo error, Exception innerException = null)
: base(error.Message, innerException)
{
Error = error;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,19 @@ protected virtual async Task<HttpContent> RequestAsync(ClientProxyRequestContext
);
}

var response = await client.SendAsync(
requestMessage,
HttpCompletionOption.ResponseHeadersRead /*this will buffer only the headers, the content will be used as a stream*/,
GetCancellationToken(requestContext.Arguments)
);
HttpResponseMessage response;
try
{
response = await client.SendAsync(
requestMessage,
HttpCompletionOption.ResponseHeadersRead /*this will buffer only the headers, the content will be used as a stream*/,
GetCancellationToken(requestContext.Arguments)
);
}
catch (Exception ex)
{
throw new AbpRemoteCallException($"An error occurred during the ABP remote HTTP request. ({ex.Message}) See the inner exception for details.", ex);
}

if (!response.IsSuccessStatusCode)
{
Expand Down Expand Up @@ -197,26 +205,46 @@ protected virtual async Task ThrowExceptionForResponseAsync(HttpResponseMessage
{
if (response.Headers.Contains(AbpHttpConsts.AbpErrorFormat))
{
var errorResponse = JsonSerializer.Deserialize<RemoteServiceErrorResponse>(
await response.Content.ReadAsStringAsync()
);
RemoteServiceErrorResponse errorResponse;
try
{
errorResponse = JsonSerializer.Deserialize<RemoteServiceErrorResponse>(
await response.Content.ReadAsStringAsync()
);
}
catch (Exception ex)
{
throw new AbpRemoteCallException(
new RemoteServiceErrorInfo
{
Message = response.ReasonPhrase,
Code = response.StatusCode.ToString()
},
ex
)
{
HttpStatusCode = (int)response.StatusCode
};
}

throw new AbpRemoteCallException(errorResponse.Error)
{
HttpStatusCode = (int) response.StatusCode
};
}

throw new AbpRemoteCallException(
new RemoteServiceErrorInfo
{
Message = response.ReasonPhrase,
Code = response.StatusCode.ToString()
}
)
else
{
HttpStatusCode = (int) response.StatusCode
};
throw new AbpRemoteCallException(
new RemoteServiceErrorInfo
{
Message = response.ReasonPhrase,
Code = response.StatusCode.ToString()
}
)
{
HttpStatusCode = (int) response.StatusCode
};
}
}

protected virtual void AddHeaders(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;
using Shouldly;
using Volo.Abp.AspNetCore.TestBase;
using Volo.Abp.Http.Client;
using Volo.Abp.Http.Client.Proxying;
using Xunit;

namespace Volo.Abp.Http.DynamicProxying
{
public class RegularTestControllerClientProxy_AbpRemoteCallException_Tests : AbpHttpClientTestBase
{
private readonly IRegularTestController _controller;

public RegularTestControllerClientProxy_AbpRemoteCallException_Tests()
{
_controller = ServiceProvider.GetRequiredService<IRegularTestController>();
}

protected override void ConfigureServices(HostBuilderContext context, IServiceCollection services)
{
services.Replace(ServiceDescriptor.Singleton<IProxyHttpClientFactory, TestProxyHttpClientFactory>());
}

[Fact]
public async Task AbpRemoteCallException_On_SendAsync_Test()
{
var exception = await Assert.ThrowsAsync<AbpRemoteCallException>(async () => await _controller.AbortRequestAsync(default));
exception.Message.ShouldContain("An error occurred during the ABP remote HTTP request.");
}

class TestProxyHttpClientFactory : IProxyHttpClientFactory
{
private readonly ITestServerAccessor _testServerAccessor;

private int _count;

public TestProxyHttpClientFactory(ITestServerAccessor testServerAccessor)
{
_testServerAccessor = testServerAccessor;
}

public HttpClient Create(string name) => Create();

public HttpClient Create()
{
if (_count++ > 0)
{
//Will get an error on the SendAsync method.
return new HttpClient();
}

// for DynamicHttpProxyInterceptor.GetActionApiDescriptionModel
return _testServerAccessor.Server.CreateClient();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,8 @@ public async Task AbortRequestAsync()
var result = await _controller.AbortRequestAsync(default);
result.ShouldBe("AbortRequestAsync");

var exception = await Assert.ThrowsAsync<HttpRequestException>(async () => await _controller.AbortRequestAsync(cts.Token));
exception.InnerException.InnerException.Message.ShouldBe("The client aborted the request.");
var exception = await Assert.ThrowsAsync<AbpRemoteCallException>(async () => await _controller.AbortRequestAsync(cts.Token));
exception.InnerException.InnerException.InnerException.Message.ShouldBe("The client aborted the request.");
}
}
}