mirror of
https://github.com/Ranchero-Software/NetNewsWire
synced 2026-11-29 08:42:51 +00:00
Include up to 500 characters of http error response body in error log.
This commit is contained in:
1 parent
ac5f8dac6a
commit
49dbebf677
8 files changed
+40
-24
No files matched your search
@@ -22,7 +22,7 @@ public enum AccountError: LocalizedError {
|
||||
|
||||
public var isCredentialsError: Bool {
|
||||
if case .wrappedError(let error, _, _, _) = self {
|
||||
if case WebserviceError.httpError(let status) = error {
|
||||
if case WebserviceError.httpError(let status, _) = error {
|
||||
return isCredentialsError(status: status)
|
||||
}
|
||||
}
|
||||
@@ -58,7 +58,7 @@ public enum AccountError: LocalizedError {
|
||||
return NSLocalizedString("Unknown error", comment: "Unknown error")
|
||||
case .wrappedError(let error, _, let accountName, let accountType):
|
||||
switch error {
|
||||
case WebserviceError.httpError(let status):
|
||||
case WebserviceError.httpError(let status, _):
|
||||
if isCredentialsError(status: status) {
|
||||
let localizedText = NSLocalizedString("Your “%@” credentials are invalid or expired.", comment: "Invalid or expired")
|
||||
return NSString.localizedStringWithFormat(localizedText as NSString, accountName) as String
|
||||
@@ -79,7 +79,7 @@ public enum AccountError: LocalizedError {
|
||||
return nil
|
||||
case .wrappedError(let error, _, _, _):
|
||||
switch error {
|
||||
case WebserviceError.httpError(let status):
|
||||
case WebserviceError.httpError(let status, _):
|
||||
if isCredentialsError(status: status) {
|
||||
return NSLocalizedString("Please update your credentials for this account, or ensure that your account with this service is still valid.", comment: "Expired credentials")
|
||||
} else {
|
||||
@@ -115,6 +115,9 @@ extension AccountError {
|
||||
let underlyingNSError = underlyingError as NSError
|
||||
message += " -- underlying: \(underlyingError.localizedDescription) (\(underlyingNSError.domain) \(underlyingNSError.code))"
|
||||
}
|
||||
if case WebserviceError.httpError(_, let responseBody) = error, let responseBody {
|
||||
message += " -- server response: \(responseBody)"
|
||||
}
|
||||
return message
|
||||
}
|
||||
}
|
||||
|
||||
@@ -60,7 +60,7 @@ enum CreateSubscriptionResult {
|
||||
try await session.send(request: request)
|
||||
return credentials
|
||||
} catch {
|
||||
if case WebserviceError.httpError(let status) = error, status == 401 {
|
||||
if case WebserviceError.httpError(let status, _) = error, status == 401 {
|
||||
return nil
|
||||
}
|
||||
throw error
|
||||
@@ -169,11 +169,11 @@ enum CreateSubscriptionResult {
|
||||
case HTTPResponseCode.redirectTemporary: // 302
|
||||
return .alreadySubscribed
|
||||
default:
|
||||
throw WebserviceError.httpError(status: response.forcedStatusCode)
|
||||
throw WebserviceError.httpError(status: response.forcedStatusCode, responseBody: nil)
|
||||
}
|
||||
} catch {
|
||||
switch error {
|
||||
case WebserviceError.httpError(let status):
|
||||
case WebserviceError.httpError(let status, _):
|
||||
switch status {
|
||||
case HTTPResponseCode.unauthorized: // 401
|
||||
// I don’t know why we get 401s here. This looks like a Feedbin bug, but it only happens
|
||||
|
||||
@@ -400,21 +400,21 @@ private extension FeedlyAPICaller {
|
||||
private func withReauthorizationRetry<T>(request: URLRequest, _ perform: (URLRequest) async throws -> T) async throws -> T {
|
||||
do {
|
||||
return try await perform(request)
|
||||
} catch WebserviceError.httpError(let status) where status == 401 {
|
||||
} catch WebserviceError.httpError(let status, _) where status == 401 {
|
||||
guard let delegate else {
|
||||
assertionFailure("Check the delegate is set to \(FeedlyAccountDelegate.self).")
|
||||
throw WebserviceError.httpError(status: 401)
|
||||
throw WebserviceError.httpError(status: 401, responseBody: nil)
|
||||
}
|
||||
|
||||
let didReauthorize = await delegate.reauthorizeFeedlyAPICaller()
|
||||
guard didReauthorize else {
|
||||
throw WebserviceError.httpError(status: 401)
|
||||
throw WebserviceError.httpError(status: 401, responseBody: nil)
|
||||
}
|
||||
|
||||
// The server may hand back the same token — only require that one exists.
|
||||
guard let accessToken = credentials?.secret else {
|
||||
assertionFailure("Could not update the request with an OAuth token. Did \(String(describing: delegate)) set them on \(self)?")
|
||||
throw WebserviceError.httpError(status: 401)
|
||||
throw WebserviceError.httpError(status: 401, responseBody: nil)
|
||||
}
|
||||
|
||||
var reauthorizedRequest = request
|
||||
|
||||
@@ -700,7 +700,7 @@ import Secrets
|
||||
try await account.logActivity(kind: .removeFeed, detail: feed.url) {
|
||||
try await caller.removeFeed(feed.feedID, fromCollectionWith: collectionID)
|
||||
}
|
||||
} catch WebserviceError.httpError(let status) where status == HTTPResponseCode.badRequest || status == HTTPResponseCode.notFound {
|
||||
} catch WebserviceError.httpError(let status, _) where status == HTTPResponseCode.badRequest || status == HTTPResponseCode.notFound {
|
||||
// Feedly doesn’t recognize this subscription — most likely a stale or malformed feedID.
|
||||
// Let the local removal stand, since otherwise the feed could never be deleted.
|
||||
// If Feedly still has the feed, the next sync will bring it back.
|
||||
@@ -1284,7 +1284,7 @@ extension FeedlyAccountDelegate: FeedlyAPICallerDelegate {
|
||||
if error is FeedlyAccountDelegateError || error is DecodingError {
|
||||
return true
|
||||
}
|
||||
if case WebserviceError.httpError(let status) = error, (400...499).contains(status) {
|
||||
if case WebserviceError.httpError(let status, _) = error, (400...499).contains(status) {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
|
||||
@@ -363,7 +363,7 @@ import os
|
||||
feed.lastCheckDate = Date()
|
||||
feed.lastResponseCode = statusCode
|
||||
|
||||
let webserviceError = WebserviceError.httpError(status: statusCode)
|
||||
let webserviceError = WebserviceError.httpError(status: statusCode, responseBody: nil)
|
||||
let statusDescription = webserviceError.localizedDescription
|
||||
let errorMessage = "HTTP \(statusCode) \(statusDescription): \(url.absoluteString)"
|
||||
let error = NSError(domain: "NetNewsWire", code: statusCode, userInfo: [NSLocalizedDescriptionKey: errorMessage])
|
||||
|
||||
@@ -138,7 +138,7 @@ struct ReaderAPIUsageLimits {
|
||||
return self.credentials
|
||||
|
||||
} catch {
|
||||
if let webserviceError = error as? WebserviceError, case .httpError(let code) = webserviceError, code == 404 {
|
||||
if let webserviceError = error as? WebserviceError, case .httpError(let code, _) = webserviceError, code == 404 {
|
||||
throw AccountError.urlNotFound
|
||||
} else {
|
||||
throw error
|
||||
@@ -183,7 +183,7 @@ struct ReaderAPIUsageLimits {
|
||||
let token = try await requestAuthorizationToken(endpoint: endpoint)
|
||||
do {
|
||||
return try await operation(token)
|
||||
} catch WebserviceError.httpError(let status) where status == 401 || status == 403 {
|
||||
} catch WebserviceError.httpError(let status, _) where status == 401 || status == 403 {
|
||||
accessToken = nil
|
||||
let freshToken = try await requestAuthorizationToken(endpoint: endpoint)
|
||||
return try await operation(freshToken)
|
||||
|
||||
@@ -55,7 +55,7 @@ import RSWeb
|
||||
if case WebserviceError.tooManyRequests = error {
|
||||
return true
|
||||
}
|
||||
if treatsForbiddenAsRateLimited, case WebserviceError.httpError(let status) = error, status == HTTPResponseCode.forbidden {
|
||||
if treatsForbiddenAsRateLimited, case WebserviceError.httpError(let status, _) = error, status == HTTPResponseCode.forbidden {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
|
||||
@@ -13,12 +13,12 @@ public enum WebserviceError: LocalizedError, Sendable {
|
||||
case noData
|
||||
case noURL
|
||||
case suspended
|
||||
case httpError(status: Int)
|
||||
case httpError(status: Int, responseBody: String?)
|
||||
case tooManyRequests(retryAfter: TimeInterval?)
|
||||
|
||||
public var errorDescription: String? {
|
||||
switch self {
|
||||
case .httpError(let status):
|
||||
case .httpError(let status, _):
|
||||
return "HTTP \(status): \(HTTPURLResponse.localizedString(forStatusCode: status))"
|
||||
case .noData:
|
||||
return NSLocalizedString("No data was returned by the server.", comment: "No data")
|
||||
@@ -80,26 +80,26 @@ nonisolated extension URLSession {
|
||||
@discardableResult
|
||||
public func send(request: URLRequest) async throws -> (HTTPURLResponse, Data) {
|
||||
let (data, response) = try await data(for: request)
|
||||
return (try Self.validatedHTTPResponse(response), data)
|
||||
return (try Self.validatedHTTPResponse(response, data: data), data)
|
||||
}
|
||||
|
||||
public func send(request: URLRequest, method: String) async throws {
|
||||
var sendRequest = request
|
||||
sendRequest.httpMethod = method
|
||||
let (_, response) = try await data(for: sendRequest)
|
||||
try Self.validatedHTTPResponse(response)
|
||||
let (data, response) = try await data(for: sendRequest)
|
||||
try Self.validatedHTTPResponse(response, data: data)
|
||||
}
|
||||
|
||||
public func send(request: URLRequest, method: String, payload: Data) async throws -> (HTTPURLResponse, Data) {
|
||||
var sendRequest = request
|
||||
sendRequest.httpMethod = method
|
||||
let (data, response) = try await upload(for: sendRequest, from: payload)
|
||||
return (try Self.validatedHTTPResponse(response), data)
|
||||
return (try Self.validatedHTTPResponse(response, data: data), data)
|
||||
}
|
||||
|
||||
/// Require an HTTP response with a 200...399 status code, or throw the matching `WebserviceError`.
|
||||
@discardableResult
|
||||
private static func validatedHTTPResponse(_ response: URLResponse) throws -> HTTPURLResponse {
|
||||
private static func validatedHTTPResponse(_ response: URLResponse, data: Data?) throws -> HTTPURLResponse {
|
||||
guard let httpResponse = response as? HTTPURLResponse else {
|
||||
throw WebserviceError.noData
|
||||
}
|
||||
@@ -113,7 +113,20 @@ nonisolated extension URLSession {
|
||||
}
|
||||
throw WebserviceError.tooManyRequests(retryAfter: retryAfter)
|
||||
default:
|
||||
throw WebserviceError.httpError(status: httpResponse.forcedStatusCode)
|
||||
throw WebserviceError.httpError(status: httpResponse.forcedStatusCode, responseBody: responseBodyForError(data))
|
||||
}
|
||||
}
|
||||
|
||||
/// A trimmed, whitespace-collapsed prefix of an error response's body.
|
||||
/// The body often says what the server didn't like.
|
||||
private static func responseBodyForError(_ data: Data?) -> String? {
|
||||
guard let data, !data.isEmpty, let body = String(data: data, encoding: .utf8) else {
|
||||
return nil
|
||||
}
|
||||
let collapsed = body.collapsingWhitespace
|
||||
guard !collapsed.isEmpty else {
|
||||
return nil
|
||||
}
|
||||
return String(collapsed.prefix(500))
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user