Extract ApiException creation from SendRequestAsync method #256
Replies: 1 comment 1 reply
-
Sure, sounds sensible. Thanks for checking in first. I'm generally trying to move away from asking people to subclass Requester these days, as subclassing is quite a brittle form of customisation and it means it's very hard to make changes to Requester without breaking people. However for a single person doing a niche thing I think it's fine, as long as you're OK with potentially having to deal with breaks (i.e. changes in method signature etc, rather than having to take an entirely different approach) in the future. Why not extract the whole of: if (!response.IsSuccessStatusCode && !requestInfo.AllowAnyStatusCode)
{
var deserializer = new ApiExceptionContentDeserializer(this, response, requestInfo);
throw await ApiException.CreateAsync(message, response, deserializer).ConfigureAwait(false);
} Then the method can become "Figure out whether this response is good, and throw if it isn't", which seems like a more sensible thing to try and extract. |
Beta Was this translation helpful? Give feedback.
-
@canton7 : Before creating a PR I would like to get your opinion on that topic.
Currently the creation of the
ApiException
is part of theSendRequestAsync
method (in case it is enabled).I do have the requirement to adapt exactly this part as we are using our own ApiException class due to reasons.
For this scenario, I currently see two options:
Requester
, override theSendRequestAsync
method2a. Call
base.SendRequestAsync
in try-catch block (basically the same as option 1)2b. Copy paste the existing code in that method and only replace the exception handling part (avoids the unnecessary try-catch, but also not nice, because when there are changes in the original code, we would have to check on every update of the package if we need to align the copied code.
Hence, the preferrable solution from my point of view would be to create a PR that refactors the method by exctracting lines 473 and 474:
into a seperate virtual method e.g.:
In this case we could again inherit from
Requester
but only override this new method and keep everything else as is, avoiding again the non-required try-catch approach.What do you think?
Thanks in advance and best regards!
Tobi
Beta Was this translation helpful? Give feedback.
All reactions