Error handling when using Task<ApiResponse<T>> #2187
-
|
Good afternoon, I've upgraded to the version 12.1.0 of Refit and im having trouble in the error finding. In previous versions I used to capture the error and log the response.Error.Content, which gave me more information. Currently on the new version im not sure how to access that field anymore and with the documentation im not able to figure it out either. Can someone point me to the good direction on how i could get that content again? Thanks in advanced. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
The Content you were reading via response.Error.Content is still there - it just needs one narrowing step after the v12 exception refactor. In v12, IApiResponse.Error was widened from ApiException to ApiExceptionBase, the shared base of:
Content lives on ApiException, so response.Error.Content no longer compiles directly. Recover it like this: // Preferred: safe narrowing to the response-side exception
if (response.HasResponseError(out var apiException))
{
_logger.LogError(apiException, apiException.Content);
}
// Or a direct cast / pattern match
var content = (response.Error as ApiException)?.Content;If you also want to distinguish transport failures from server responses: if (response.HasRequestError(out var requestError))
_logger.LogError(requestError, "Request never reached the server.");
else if (response.HasResponseError(out var responseError))
_logger.LogError(responseError, responseError.Content);This is covered in the README breaking-changes section (the IApiResponse.Error type change and HasResponseError). We agree it wasn't discoverable enough from the "Error.Content is gone" symptom, so there's a docs issue to improve the upgrade notes: #2189. |
Beta Was this translation helpful? Give feedback.
The Content you were reading via response.Error.Content is still there - it just needs one narrowing step after the v12 exception refactor.
In v12, IApiResponse.Error was widened from ApiException to ApiExceptionBase, the shared base of:
Content lives on ApiException, so response.Error.Content no longer compiles directly. Recover it like this: