Skip to content

Commit

Permalink
better logging in SO-checker
Browse files Browse the repository at this point in the history
  • Loading branch information
MahdiBM committed Oct 13, 2023
1 parent eb4e6d2 commit 02d51ec
Showing 1 changed file with 14 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import AsyncHTTPClient
import NIOCore
import Logging
import Foundation

struct DefaultSOService: SOService {
let httpClient: HTTPClient
let logger = Logger(label: "DefaultSOService")
let decoder = JSONDecoder()

func listQuestions(after: Date) async throws -> [SOQuestions.Item] {
Expand All @@ -22,6 +24,18 @@ struct DefaultSOService: SOService {
let request = HTTPClientRequest(url: url)
let response = try await httpClient.execute(request, deadline: .now() + .seconds(15))
let buffer = try await response.body.collect(upTo: 1 << 25) /// 32 MB

guard 200..<300 ~= response.status.code else {
let collected = try? await response.body.collect(upTo: 1 << 16)
let body = collected.map { String(buffer: $0) } ?? "nil"
logger.error("SO-service failed", metadata: [
"status": "\(response.status)",
"headers": "\(response.headers)",
"body": "\(body)",
])
throw ServiceError.badStatus(response.status)
}

let questions = try decoder.decode(
SOQuestions.self,
from: buffer
Expand Down

0 comments on commit 02d51ec

Please sign in to comment.