Skip to content
This repository has been archived by the owner on Apr 20, 2024. It is now read-only.

Commit

Permalink
Merge pull request #55 from nodes-vapor/feature/client_fix
Browse files Browse the repository at this point in the history
Feature/client fix
  • Loading branch information
siemensikkema committed Mar 6, 2019
2 parents 5ea741d + e016914 commit 8506831
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 19 deletions.
23 changes: 11 additions & 12 deletions Sources/Bugsnag/BugsnagReporter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,28 @@ public struct BugsnagReporter: Service {
private let config: BugsnagConfig
private let headers: HTTPHeaders

private let hostName = "notify.bugsnag.com"
private let hostName = "https://notify.bugsnag.com"
private let jsonEncoder = JSONEncoder()
private let notifier = BugsnagNotifier(
name: "nodes-vapor/bugsnag",
url: "https://github.com/nodes-vapor/bugsnag.git",
version: "3"
)
private let payloadVersion = "4"
private let sendReport: (String, HTTPHeaders, Data, Container) -> Future<HTTPResponse>
private let sendReport: (String, HTTPHeaders, Data, Container) throws -> Future<Response>

public init(
config: BugsnagConfig,
sendReport: ((String, HTTPHeaders, Data, Container) -> Future<HTTPResponse>)? = nil
sendReport: ((String, HTTPHeaders, Data, Container) throws -> Future<Response>)? = nil
) {
self.config = config

self.sendReport = sendReport ?? { (hostName, headers, body, container) in
HTTPClient
.connect(hostname: hostName, on: container)
.flatMap(to: HTTPResponse.self) { client in
client.send(.init(method: .POST, headers: headers, body: body))
}
try container
.client()
.post(hostName, headers: headers, beforeSend: { req in
req.http.body = .init(data: body)
})
}

app = BugsnagApp(
Expand Down Expand Up @@ -77,7 +77,6 @@ extension BugsnagReporter: ErrorReporter {
return try jsonEncoder.encode(payload)
}

@discardableResult
public func report(
_ error: Error,
severity: Severity = .error,
Expand Down Expand Up @@ -108,12 +107,12 @@ extension BugsnagReporter: ErrorReporter {
)
)

return self
return try self
.sendReport(self.hostName, self.headers, body, container)
.do { response in
if self.config.debug {
print("Bugsnag response:")
print(response.status.code, response.status.reasonPhrase)
let status = response.http.status
print("Bugsnag response:\n", status.code, status.reasonPhrase)
}
}
.transform(to: ())
Expand Down
4 changes: 2 additions & 2 deletions Sources/Bugsnag/ErrorReporter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public protocol ErrorReporter {
}

extension ErrorReporter {
func report(
public func report(
_ error: Error,
severity: Severity = .error,
userId: CustomStringConvertible? = nil,
Expand All @@ -39,7 +39,7 @@ extension ErrorReporter {
)
}

func report<U: BugsnagReportableUser>(
public func report<U: BugsnagReportableUser>(
_ error: Error,
severity: Severity = .error,
userType: U.Type,
Expand Down
11 changes: 6 additions & 5 deletions Tests/BugsnagTests/BugsnagTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -115,20 +115,21 @@ final class BugsnagTests: XCTestCase {
config: .init(apiKey: "apiKey", releaseStage: "test"),
sendReport: { host, headers, data, container in
capturedSendReportParameters = (host, headers, data, container)
return container.future(HTTPResponse(status: .ok))
return container.future(Response(http: HTTPResponse(status: .ok), using: container))
})
let application = try Application.test()
let request = Request(using: application)
request.breadcrumb(name: "a", type: .log)

reporter.report(NotFound(), severity: .info, userId: 1, metadata: ["a": "b"], on: request)
_ = reporter
.report(NotFound(), severity: .info, userId: 1, metadata: ["a": "b"], on: request)

guard let params = capturedSendReportParameters else {
XCTFail()
return
}

XCTAssertEqual(params.host, "notify.bugsnag.com")
XCTAssertEqual(params.host, "https://notify.bugsnag.com")
XCTAssertEqual(params.headers["Content-Type"].first, "application/json")
XCTAssertEqual(params.headers["Bugsnag-Api-Key"].first, "apiKey")
XCTAssertEqual(params.headers["Bugsnag-Payload-Version"].first, "4")
Expand All @@ -145,9 +146,9 @@ final class BugsnagTests: XCTestCase {
func testReportingCanBeDisabled() throws {
let reporter = BugsnagReporter(
config: .init(apiKey: "apiKey", releaseStage: "test", shouldReport: false),
sendReport: { host, headers, data, request in
sendReport: { host, headers, data, container in
XCTFail("No error should be reported")
return request.future(HTTPResponse(status: .ok))
return container.future(Response(http: HTTPResponse(status: .ok), using: container))
})

let application = try Application.test()
Expand Down

0 comments on commit 8506831

Please sign in to comment.