You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Problem statement
When catching an ApiException, you may want to look at the response body as it could be a well known Error format.
Of course this peeking might fail: no body or not the right format.
ApiException offers T? GetContentAsAsync<T>() to deserialize the error contents.
At first glance it looks good, but in practice it falls short.
The problem is that this method can throw, or return null.
When it happens you want to fallback to a more generic catch handler, which isn't easily doable (no goto!).
The obvious solution would be to put GetContentAsAsync() in an exception filter (aka catch .. when ..).
That does not work because you can't await inside an exception filter, CLR requires synchronous filters.
Using only plain Refit API, you end up with code like this...
try{try{// call a Refit APIvarresponse=await client.DoStuff();}catch(ApiExceptionex){varerror=await ex.GetContentAsAsync<Error>();if(error isnull)throw;// Do Error-specific stuffreturn;}}catch(Exceptionex){// Do generic exception stuff}
Suggested solution
As the response is already buffered in a string, GetContentAs may well be sync.
(This implies one new sync method on IHttpContentSerializer).
It would also be nice if the result is non-nullable and it wouldn't throw on errors, maybe bool TryGetContentAs<T>(out T result).
(Those two requirements are not hard-requirements: 1. an exception filter that throws is equivalent to false; 2. one could do when (ex.GetContentAs<T>() is T error) to filter out nulls.)
With this, plain Refit code becomes a lot more intuitive and readable:
try{}catch(ApiExceptionex)when(ex.TryGetContentAs<Error>(outvar error)){// Do something with `error`}catch(Exceptionex){// Non-specific error handling}
The text was updated successfully, but these errors were encountered:
Problem statement
When catching an
ApiException
, you may want to look at the response body as it could be a well knownError
format.Of course this peeking might fail: no body or not the right format.
ApiException
offersT? GetContentAsAsync<T>()
to deserialize the error contents.At first glance it looks good, but in practice it falls short.
The problem is that this method can throw, or return null.
When it happens you want to fallback to a more generic
catch
handler, which isn't easily doable (nogoto
!).The obvious solution would be to put
GetContentAsAsync()
in an exception filter (akacatch .. when ..
).That does not work because you can't await inside an exception filter, CLR requires synchronous filters.
Using only plain Refit API, you end up with code like this...
Suggested solution
As the response is already buffered in a string,
GetContentAs
may well be sync.(This implies one new sync method on
IHttpContentSerializer
).It would also be nice if the result is non-nullable and it wouldn't throw on errors, maybe
bool TryGetContentAs<T>(out T result)
.(Those two requirements are not hard-requirements: 1. an exception filter that throws is equivalent to false; 2. one could do
when (ex.GetContentAs<T>() is T error)
to filter out nulls.)With this, plain Refit code becomes a lot more intuitive and readable:
The text was updated successfully, but these errors were encountered: