-
-
Notifications
You must be signed in to change notification settings - Fork 19
Move Response extraction logic out of init of DavException
#81
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR refactors the DavException
class by extracting HTTP response processing logic from the constructor into a separate populateHttpResponse
method. This improves separation of concerns and makes the exception instantiation more flexible.
Key changes:
- Extracted HTTP response processing logic from
init
block intopopulateHttpResponse
method - Made
DavException
constructor no longer accepthttpResponse
parameter - Updated all usage sites to call
populateHttpResponse
after exception creation
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
File | Description |
---|---|
DavException.kt | Removed httpResponse parameter from constructor, moved processing logic to populateHttpResponse method |
HttpException.kt | Updated constructor to call populateHttpResponse instead of passing response to parent |
DavResource.kt | Updated exception throwing to use fluent API with populateHttpResponse |
DavExceptionTest.kt | Updated test to use new fluent API pattern |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's bothersome to first create DavException
and then populate with HttpResponse afterwards.
Is there a good reason for not adding a static fromResponse(response: HttpResponse)
to the companion object of DavException
, which would create DavException
already populated with the needed properties from the response, like @rfc2822 suggested?
Yes, The other option is to use reflection or something like that, but I don't love that approach. |
Right. I meant this: why don't we make fun fromResponse(
message: String,
httpResponse: Response?,
ex: Throwable? = null
): DavException =
DavException(message, ex).populateWithHttpResponse(httpResponse) |
I also wonder about how a good API would look like for the (discussable) requirements that:
Both should be able to contain some context of the response body because servers often provide useful information in the response body. For instance, errors are often just 500 Internal Server Error, but the response body could contain more information. This information is shown for instance to DAVx5 users in the debug info screen. Additionally there's the WebDAV XML error element that should be processed. The classes probably shouldn't be coupled to a specific HTTP framework too tightly. So some questions that come to my mind:
|
@sunkup please take a look and see if you like this more. |
mmh, no. Now there is a side effect in the constructor: It executes logic (reading/parsing HTTP body) that can throw exceptions or cause blocking I/O. It makes the constructor "unsafe" and violates the general principle that constructors should be light and free of side effects. It now has more than one responsibility (to simply construct and fill the object) so it violates the single-responsibility principle. Also it is harder to test/mock now because you need to know which constructor you need to use in order to not invoke the population logic. Using the factory method ( Sure, it's slightly more verbose in usage, but I think that's actually a benefit here. |
I've tried setting up some kind of templating but it's ugly... I honestly don't like it. |
I agree. Replaced by #83, please follow up there. |
Moved all the code inside of
init
inDavException
to its own function to initialize the contents, decoupling a bit better how the data is instantiated.Depends on #82