Skip to content
Discussion options

You must be logged in to vote

There is no setting that swaps IApiResponse.Error to a custom type globally. Error is an ApiExceptionBase (ApiException for response errors), so the strongly typed body is not surfaced there directly. But you have two clean global options:

  1. Deserialize the error body on demand from the exception.
    ApiException exposes GetContentAsAsync(), so you do not need GetContentAsync everywhere:
if (response.Error is ApiException apiEx && apiEx.HasContent)
{
    var myError = await apiEx.GetContentAsAsync<MyErrorBody>();
}
  1. Plug in a custom ExceptionFactory on RefitSettings to produce your own exception type carrying the parsed body. This is exactly how Refit's built-in ValidationApiException works …

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by glennawatson
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants