Skip to content
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

HTTPClient: improved log for failed requests #2669

Merged
merged 1 commit into from
Jun 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions Sources/Logging/Strings/NetworkStrings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ enum NetworkStrings {

case api_request_started(HTTPRequest)
case api_request_completed(_ request: HTTPRequest, httpCode: HTTPStatusCode)
case api_request_failed(_ request: HTTPRequest, error: NetworkError)
case api_request_failed(_ request: HTTPRequest, httpCode: HTTPStatusCode?, error: NetworkError)
case reusing_existing_request_for_operation(CacheableNetworkOperation.Type, String)
case creating_json_error(error: String)
case json_data_received(dataString: String)
Expand Down Expand Up @@ -53,8 +53,9 @@ extension NetworkStrings: LogMessage {
case let .api_request_completed(request, httpCode):
return "API request completed: \(request.description) (\(httpCode.rawValue))"

case let .api_request_failed(request, error):
return "API request failed: \(request.description): \(error.description)"
case let .api_request_failed(request, statusCode, error):
return "API request failed: \(request.description) (\(statusCode?.rawValue.description ?? "<>")): " +
"\(error.description)"

case let .reusing_existing_request_for_operation(operationType, cacheKey):
return "Network operation '\(operationType)' found with the same cache key " +
Expand Down
4 changes: 3 additions & 1 deletion Sources/Networking/HTTPClient/HTTPClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,9 @@ private extension HTTPClient {
httpCode: urlResponse?.httpStatusCode ?? response.statusCode
))
case let .failure(error):
Logger.debug(Strings.network.api_request_failed(request.httpRequest, error: error))
Logger.debug(Strings.network.api_request_failed(request.httpRequest,
httpCode: urlResponse?.httpStatusCode,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the success case we fallback to the cached status code. Is there a reason we need that? Just wondering if it's the same here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because urlResponse is optional here, for cases when the response fails catastrophically and there’s nothing. In the case of an error we don’t have a parsed response anyway, only this optional.

error: error))
}

request.completionHandler?(response)
Expand Down