fix: 精简接口日志并补充已支付航拍订单退款入口
统一 HTTP 和 WebSocket 控制台 JSON 输出,保留完整入参、响应及本地错误。为多点位航拍订单增加已支付退款入口和回归测试,并同步联调文档、接口记录及截图。
This commit is contained in:
@@ -98,20 +98,20 @@ final class APIClient {
|
||||
(data, response) = try await session.data(for: request)
|
||||
} catch is CancellationError {
|
||||
if let trace { droneDockLogSink(trace.message("request.cancelled")) }
|
||||
else { logCancelled(for: request, reason: "CancellationError", category: apiRequest.redactedLogCategory) }
|
||||
else { logCancelled(for: request, reason: "CancellationError", category: apiRequest.redactedLogCategory, includeBody: apiRequest.logsPayload) }
|
||||
throw CancellationError()
|
||||
} catch let error as URLError {
|
||||
if let trace { droneDockLogSink(trace.message(error.code == .cancelled ? "request.cancelled" : "request.failed",
|
||||
fields: DroneDockDiagnostics.errorFields(error))) }
|
||||
if error.code == .cancelled {
|
||||
if trace == nil { logCancelled(for: request, reason: "URLError.cancelled", category: apiRequest.redactedLogCategory) }
|
||||
if trace == nil { logCancelled(for: request, reason: "URLError.cancelled", category: apiRequest.redactedLogCategory, includeBody: apiRequest.logsPayload) }
|
||||
throw CancellationError()
|
||||
}
|
||||
if trace == nil { logFailure(for: request, error: error, category: apiRequest.redactedLogCategory) }
|
||||
if trace == nil { logFailure(for: request, error: error, category: apiRequest.redactedLogCategory, includeBody: apiRequest.logsPayload) }
|
||||
throw APIError.networkFailed(networkErrorMessage(for: error))
|
||||
} catch {
|
||||
if let trace { droneDockLogSink(trace.message("request.failed", fields: DroneDockDiagnostics.errorFields(error))) }
|
||||
if trace == nil { logFailure(for: request, error: error, category: apiRequest.redactedLogCategory) }
|
||||
if trace == nil { logFailure(for: request, error: error, category: apiRequest.redactedLogCategory, includeBody: apiRequest.logsPayload) }
|
||||
throw APIError.networkFailed(error.localizedDescription)
|
||||
}
|
||||
|
||||
@@ -135,7 +135,6 @@ final class APIClient {
|
||||
return value
|
||||
} catch let error as APIError {
|
||||
if let trace { droneDockLogSink(trace.message("response.rejected", fields: DroneDockDiagnostics.errorFields(error))) }
|
||||
else { logFailure(for: request, error: error, category: apiRequest.redactedLogCategory) }
|
||||
notifySessionErrorIfNeeded(for: error, request: request)
|
||||
throw error
|
||||
}
|
||||
@@ -195,7 +194,6 @@ final class APIClient {
|
||||
try validateHTTPResponse(response, data: data)
|
||||
return try decodeEnvelope(Response.self, from: data)
|
||||
} catch let error as APIError {
|
||||
logFailure(for: request, error: error)
|
||||
notifySessionErrorIfNeeded(for: error, request: request)
|
||||
throw error
|
||||
}
|
||||
@@ -270,7 +268,6 @@ final class APIClient {
|
||||
try validateHTTPResponse(response, data: responseData)
|
||||
return try decodeEnvelope(Response.self, from: responseData)
|
||||
} catch let error as APIError {
|
||||
logFailure(for: request, error: error)
|
||||
notifySessionErrorIfNeeded(for: error, request: request)
|
||||
throw error
|
||||
}
|
||||
@@ -422,111 +419,43 @@ final class APIClient {
|
||||
}
|
||||
}
|
||||
|
||||
/// Debug/Release 均打印请求地址、参数、请求头和正文。
|
||||
/// 发起请求时只打印路径和业务入参,不包含 Header、方法或诊断信封。
|
||||
private func logRequest(_ request: URLRequest, includeBody: Bool = true, category: String? = nil) {
|
||||
if let category {
|
||||
debugLogSink(APIRedactedLog.request(request, category: category, includeBody: includeBody))
|
||||
return
|
||||
} else {
|
||||
debugLogSink(APIConsoleFormat.message(path: request.url?.path ?? "",
|
||||
params: APIConsoleFormat.parameters(request, includeBody: includeBody)))
|
||||
}
|
||||
let method = request.httpMethod ?? "REQUEST"
|
||||
let url = request.url?.absoluteString ?? "<invalid url>"
|
||||
var lines = ["\(APIConsoleFormat.prefix("Request", path: request.url?.path ?? "")) \(method) \(url)"]
|
||||
if let headers = request.allHTTPHeaderFields, !headers.isEmpty {
|
||||
lines.append("headers: " + DroneDockDiagnostics.json(headers))
|
||||
}
|
||||
|
||||
if let queryItems = URLComponents(url: request.url ?? URL(fileURLWithPath: "/"), resolvingAgainstBaseURL: false)?.queryItems,
|
||||
!queryItems.isEmpty {
|
||||
let params = queryItems
|
||||
.map { item in
|
||||
let value = item.value ?? ""
|
||||
return "\(item.name)=\(value)"
|
||||
}
|
||||
.joined(separator: "&")
|
||||
lines.append("params: \(params)")
|
||||
}
|
||||
|
||||
let contentType = request.value(forHTTPHeaderField: "Content-Type")
|
||||
if includeBody, let body = Self.debugRequestBody(from: request.httpBody, contentType: contentType) {
|
||||
lines.append("body:\n\(body)")
|
||||
}
|
||||
|
||||
debugLogSink(lines.joined(separator: "\n"))
|
||||
}
|
||||
|
||||
/// Debug/Release 均打印响应状态、响应头及完整正文,包括非成功响应。
|
||||
/// 原始回包整体放入 response,业务失败同样保留完整 JSON,不另打印解析事件。
|
||||
private func logResponse(for request: URLRequest, response: URLResponse, data: Data,
|
||||
includeBody: Bool = true, category: String? = nil) {
|
||||
if let category {
|
||||
debugLogSink(APIRedactedLog.response(for: request, response: response, data: data,
|
||||
category: category, includeBody: includeBody))
|
||||
return
|
||||
}
|
||||
let method = request.httpMethod ?? "REQUEST"
|
||||
let url = request.url?.absoluteString ?? "<invalid url>"
|
||||
let statusCode = (response as? HTTPURLResponse).map { String($0.statusCode) } ?? "unknown"
|
||||
let body = includeBody ? Self.debugResponseBody(from: data) : "<sensitive payload omitted>"
|
||||
let headers = (response as? HTTPURLResponse)?.allHeaderFields.reduce(into: [String: String]()) {
|
||||
$0[String(describing: $1.key)] = String(describing: $1.value)
|
||||
} ?? [:]
|
||||
debugLogSink("\(APIConsoleFormat.prefix("Response", path: request.url?.path ?? "")) \(method) \(url) status=\(statusCode)\nheaders: \(DroneDockDiagnostics.json(headers))\n\(body)")
|
||||
}
|
||||
|
||||
/// Debug/Release 均记录取消,取消不记作成功响应或网络失败。
|
||||
private func logCancelled(for request: URLRequest, reason: String, category: String? = nil) {
|
||||
if let category {
|
||||
debugLogSink(APIRedactedLog.event("Cancelled", for: request, category: category) + " reason=\(reason)")
|
||||
return
|
||||
}
|
||||
let method = request.httpMethod ?? "REQUEST"
|
||||
let url = request.url?.absoluteString ?? "<invalid url>"
|
||||
debugLogSink("\(APIConsoleFormat.prefix("Cancelled", path: request.url?.path ?? "")) \(method) \(url) reason=\(reason)")
|
||||
}
|
||||
|
||||
/// 所有业务模块均记录传输、业务拒绝或解码失败;保留显式模块日志策略。
|
||||
private func logFailure(for request: URLRequest, error: Error, category: String? = nil) {
|
||||
let error = error as NSError
|
||||
if let category {
|
||||
debugLogSink(APIRedactedLog.event("Failure", for: request, category: category)
|
||||
+ " error=\(error.domain)(\(error.code))")
|
||||
} else {
|
||||
debugLogSink("\(APIConsoleFormat.prefix("Failure", path: request.url?.path ?? "")) \(request.httpMethod ?? "REQUEST") \(request.url?.absoluteString ?? "<invalid url>")"
|
||||
+ " error=\(error.domain)(\(error.code)) message=\(error.localizedDescription)")
|
||||
debugLogSink(APIConsoleFormat.message(path: request.url?.path ?? "",
|
||||
params: APIConsoleFormat.parameters(request, includeBody: includeBody),
|
||||
response: includeBody ? APIConsoleFormat.payload(data) : "<sensitive payload omitted>"))
|
||||
}
|
||||
}
|
||||
|
||||
/// 将请求体格式化为便于调试阅读的字符串。
|
||||
private static func debugRequestBody(from data: Data?, contentType: String?) -> String? {
|
||||
guard let data, !data.isEmpty else { return nil }
|
||||
|
||||
if let contentType, contentType.lowercased().contains("multipart/form-data") {
|
||||
return "<multipart/form-data: \(data.count) bytes>"
|
||||
}
|
||||
|
||||
if
|
||||
let object = try? JSONSerialization.jsonObject(with: data),
|
||||
let prettyData = try? JSONSerialization.data(withJSONObject: object, options: [.prettyPrinted, .sortedKeys]),
|
||||
let prettyJSON = String(data: prettyData, encoding: .utf8) {
|
||||
return prettyJSON
|
||||
}
|
||||
|
||||
if let text = String(data: data, encoding: .utf8), !text.isEmpty {
|
||||
return text
|
||||
}
|
||||
|
||||
return "<non-utf8 body: \(data.count) bytes>"
|
||||
/// 取消是本地结果,不生成后端业务成功或失败回包。
|
||||
private func logCancelled(for request: URLRequest, reason: String, category: String? = nil, includeBody: Bool = true) {
|
||||
logLocalFailure(for: request, kind: "cancelled", message: "请求已取消", category: category, includeBody: includeBody)
|
||||
}
|
||||
|
||||
/// 将响应体格式化为便于调试阅读的字符串。
|
||||
private static func debugResponseBody(from data: Data) -> String {
|
||||
guard !data.isEmpty else { return "<empty response>" }
|
||||
if
|
||||
let object = try? JSONSerialization.jsonObject(with: data),
|
||||
let prettyData = try? JSONSerialization.data(withJSONObject: object, options: [.prettyPrinted, .sortedKeys]),
|
||||
let prettyJSON = String(data: prettyData, encoding: .utf8) {
|
||||
return prettyJSON
|
||||
}
|
||||
return String(data: data, encoding: .utf8) ?? "<non-utf8 response: \(data.count) bytes>"
|
||||
/// 仅网络层未收到响应时打印本地错误;已经打印原始回包的业务/解析异常不重复输出。
|
||||
private func logFailure(for request: URLRequest, error: Error, category: String? = nil, includeBody: Bool = true) {
|
||||
logLocalFailure(for: request, kind: "network_failed", message: error.localizedDescription, category: category, includeBody: includeBody)
|
||||
}
|
||||
|
||||
private func logLocalFailure(for request: URLRequest, kind: String, message: String, category: String?, includeBody: Bool) {
|
||||
debugLogSink(APIConsoleFormat.failure(path: request.url?.path ?? "",
|
||||
params: category == nil ? APIConsoleFormat.parameters(request, includeBody: includeBody) : APIRedactedLog.parameters(request, includeBody: includeBody),
|
||||
kind: kind, message: message))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user