-
-
Notifications
You must be signed in to change notification settings - Fork 159
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
Add URL to RequestError #198
base: main
Are you sure you want to change the base?
Conversation
Source/Siesta/RequestError.swift
Outdated
@@ -38,8 +38,15 @@ public struct RequestError: Error | |||
*/ | |||
public var userMessage: String | |||
|
|||
/// The HTTP response if this error came from an HTTP response. | |||
public var httpResponse: HTTPURLResponse? |
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.
Perhaps this should be private(set)
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.
What are your thoughts on me changing it to a let
?
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.
Yeah, if it's only set in init
then I'd say that's appropriate.
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.
go for let
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.
👍 will do. Thanks!
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.
@victorcotap all set!
Hi @jrmsklar, and thanks for suggesting this! I have several concerns about attaching the full
I did originally consider designing the API this way. These concerns are why I decided against it. Sorry to pour cold water on this idea! It seems to me that I’m curious: how would you use the URL? Your need for it suggests there may be some underlying unmet need in Siesta’s API. |
Thanks a ton for the detailed response here @pcantrell. All of your concerns make complete sense. I'd like the to have access to the URL in order to log it when handling API response errors. We have remote logging set up (via PaperTrail), and have not been able to send up the URL when logging response errors, and it would be very helpful for us in order to see where these errors are originating from. I'm happy to revert these changes and instead add a |
That does make perfect sense. Adding a It’s not obvious at first, but |
@pcantrell I've reverted the earlier commits, and added a |
@pcantrell when you get a free second, do you mind reviewing the changes here? |
Hey @pcantrell, Any update here? Thanks! |
@jrmsklar and @jeffaburt: Hello, and sorry for the very slow turnaround on this! I see you've been maintaining your own fork; I hope my delay hasn't caused your project distress. This innocent-looking property opened a little a can of worms for me. It occurred to me that it’s not sufficient to set It then occurred to me that the property should be set not only when request hooks and resource observers receive the response, but throughout the response pipeline. Why should At this point, the reasoning seemed to be getting out of hand, and the implementation got mildly messy: https://github.com/bustoutsolutions/siesta/compare/error-url-property That's not terrible, but it smells funny to me. I’m passing the resource URL around all over the pipeline code, only to have transformers ignore it 99% of the time. And if we follow this reasoning, shouldn’t successes also have a URL property? That means it’s a property of This is where I decided to put the brakes on. The way Siesta is designed, it would make more sense for transformers, observers, and hooks alike to all get the URL either by capturing it from context or by receiving it as an input than for it to be passed in the response. It would be helpful to know a little more about the problem you’re trying to solve. So, questions for you: You said this was for logging errors? Are you logging them via an |
Thanks for the thoughtful response, @pcantrell! I appreciate you taking the time to look into this issue and solve in a clean and elegant way. I understand your concerns and am interested in moving this forward and coming up with a solution. To answer your questions:
Yep - this is for logging the URL related to a
We have a custom API response content transformer that conforms to The code looks as follows: struct APIResponseContentTransformer: ResponseTransformer {
func process(_ response: Response) -> Response {
switch response {
case .success(let entity):
return processEntity(entity)
case .failure(let error):
return processError(error)
}
}
fileprivate func processError(_ error: RequestError) -> Response {
let urlString = error.url?.absoluteString ?? "unknown url"
// Log it
return .failure(error)
}
Let me know if you need more information! |
Hmm, that is a compelling and clarifying example. Thanks for taking the time to share it! Although you’re logging only errors, I can imagine scenarios where one would also want the URL for successes (e.g. logging all requests made). In this context, it would make less sense for I’m not thrilled with making the On reflection, this is probably yet another compelling reason for implementing what @Reedyuk suggested in #154. The basic idea is that you could configure observers that receive events only as long as a resource is still in memory, while not causing that resource to be retained indefinitely. That hypothetical non-retaining observer API would certainly send observers the resource in question, which I think would solve your problem. |
Apologies for the delay here, and thank you for the thoughtful response! I also see value in having the URL for not only errors, but successes too - it would be very nice to log all requests made! If @Reedyuk's suggested will allow for the URL to be accessible in errors and successes, then I am all for that 👍. |
Is this going to merged? |
This pull request adds a
URL
object to theRequestError
struct that represents theURL
for the request that the error represents.Motivation
We needed a way to access the
URL
that theRequestError
related to so that when loggingRequestError
s, we could include theURL
for the request that was causing the error.