8000 Refit interface methods that return Task<IApiResponse> assign ApiException to the IApiResponse.Error property on error by DouglasKSmith · Pull Request #1290 · reactiveui/refit · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Refit interface methods that return Task<IApiResponse> assign ApiException to the IApiResponse.Error property on error #1290

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

Merged
merged 3 commits into from
Jan 19, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1149,10 +1149,10 @@ public class HomeController : Controller
```

### Handling exceptions
Refit has different exception handling behavior depending on if your Refit interface methods return `Task<T>` or if they return `Task<ApiResponse<T>>`.
Refit has different exception handling behavior depending on if your Refit interface methods return `Task<T>` or if they return `Task<IApiResponse>`, `Task<IApiResponse<T>>`, or `Task<ApiResponse<T>>`.

#### When returning `Task<ApiResponse<T>>`
Refit traps any `ApiException` raised by the `ExceptionFactory` when processing the response and any errors that occur when attempting to deserialize the response to `Task<ApiResponse<T>>` and populates the exception into the `Error` property on `ApiResponse<T>` without throwing the exception.
#### When returning `Task<IApiResponse>`, `Task<IApiResponse<T>>`, or `Task<ApiResponse<T>>`
Refit traps any `ApiException` raised by the `ExceptionFactory` when processing the response, and any errors that occur when attempting to deserialize the response to `ApiResponse<T>`, and populates the exception into the `Error` property on `ApiResponse<T>` without throwing the exception.

You can then decide what to do like so:

Expand Down
23 changes: 23 additions & 0 deletions Refit.Tests/ResponseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ public interface IMyAliasService

[Get("/GetApiResponseTestObject")]
Task<ApiResponse<TestAliasObject>> GetApiResponseTestObject();

[Get("/GetIApiResponse")]
Task<IApiResponse> GetIApiResponse();
}

[Fact]
Expand Down Expand Up @@ -250,6 +253,26 @@ public async Task BadRequestWithEmptyContent_ShouldReturnApiResponse()
Assert.Equal("Hello world", apiResponse.Error.Content);
}

[Fact]
public async Task BadRequestWithStringContent_ShouldReturnIApiResponse()
{
var expectedResponse = new HttpResponseMessage(HttpStatusCode.BadRequest)
{
Content = new StringContent("Hello world")
};
expectedResponse.Content.Headers.Clear();

mockHandler.Expect(HttpMethod.Get, $"http://api/{nameof(fixture.GetIApiResponse)}")
.Respond(req => expectedResponse);

var apiResponse = await fixture.GetIApiResponse();

Assert.NotNull(apiResponse);
Assert.NotNull(apiResponse.Error);
Assert.NotNull(apiResponse.Error.Content);
Assert.Equal("Hello world", apiResponse.Error.Content);
}

[Fact]
public async Task ValidationApiException_HydratesBaseContent()
{
Expand Down
6 changes: 3 additions & 3 deletions Refit/RestMethodInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,9 @@ public RestMethodInfo(Type targetInterface, MethodInfo methodInfo, RefitSettings
CancellationToken = ctParams.FirstOrDefault();

IsApiResponse = ReturnResultType!.GetTypeInfo().IsGenericType &&
(ReturnResultType!.GetGenericTypeDefinition() == typeof(ApiResponse<>)
|| ReturnResultType.GetGenericTypeDefinition() == typeof(IApiResponse<>)
|| ReturnResultType == typeof(IApiResponse));
(ReturnResultType!.GetGenericTypeDefinition() == typeof(ApiResponse<>)
|| ReturnResultType.GetGenericTypeDefinition() == typeof(IApiResponse<>))
|| ReturnResultType == typeof(IApiResponse);
}

private ISet<int> BuildHeaderCollectionParameterMap(List<ParameterInfo> parameterList)
Expand Down
0