Skip to content

Commit 023bb11

Browse files
Hongyan JiangGitHub Enterprise
authored andcommitted
track statusCode along with error message for failed http request (#51)
1 parent 2647310 commit 023bb11

File tree

7 files changed

+29
-18
lines changed

7 files changed

+29
-18
lines changed

Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## 1.8.2
44
- crash beacon errorType value update
5+
- When http request failed, track statusCode along with error message
56

67
## 1.8.1
78
- added functionality to accept internal metadata from cross-platform agents

Sources/InstanaAgent/Error/HTTPError.swift

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ enum HTTPError: LocalizedError, RawRepresentable, CustomStringConvertible, Equat
4343
case userAuthenticationRequired
4444
case userCancelledAuthentication
4545

46-
case statusCode(Int)
46+
case statusCode(Int, NSError?)
4747
case unknownHTTPError(NSError)
4848
case unknown(NSError)
4949

@@ -84,7 +84,7 @@ enum HTTPError: LocalizedError, RawRepresentable, CustomStringConvertible, Equat
8484
case .unsupportedURL: return "Unsupported URL"
8585
case .userAuthenticationRequired: return "User Authentication Required"
8686
case .userCancelledAuthentication: return "User Cancelled Authentication"
87-
case let .statusCode(code): return "HTTP \(code)"
87+
case let .statusCode(code, _): return "HTTP \(code)"
8888
case .unknownHTTPError: return "URL Error"
8989
case .unknown: return "Error"
9090
}
@@ -135,7 +135,13 @@ enum HTTPError: LocalizedError, RawRepresentable, CustomStringConvertible, Equat
135135
case .unsupportedURL: return "A properly formed URL couldn’t be handled by the framework."
136136
case .userAuthenticationRequired: return "Authentication was required to access a resource."
137137
case .userCancelledAuthentication: return "An asynchronous request for authentication has been canceled by the user."
138-
case let .statusCode(code): return "HTTP Error with status code \(code)"
138+
139+
case let .statusCode(code, nsError):
140+
guard let error = nsError else {
141+
return "HTTP Error with status code \(code)"
142+
}
143+
return error.localizedDescription
144+
139145
case let .unknownHTTPError(error): return "\(error.localizedDescription)"
140146
case let .unknown(error): return "\(error.localizedDescription)"
141147
}
@@ -149,7 +155,7 @@ enum HTTPError: LocalizedError, RawRepresentable, CustomStringConvertible, Equat
149155
// swiftlint:disable:next cyclomatic_complexity
150156
init?(error: NSError?, statusCode: Int? = nil) {
151157
if let httpCode = statusCode, 400 ... 599 ~= httpCode {
152-
self = .statusCode(httpCode)
158+
self = .statusCode(httpCode, error)
153159
return
154160
}
155161
guard let error = error else {

Sources/InstanaAgent/Monitors/HTTP/HTTPMarker.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ protocol HTTPMarkerDelegate: AnyObject {
3131

3232
@objc public class HTTPMarker: NSObject {
3333
enum State {
34-
case started, failed(error: Error), finished(responseCode: Int), canceled
34+
case started, failed(responseCode: Int, error: Error), finished(responseCode: Int), canceled
3535
}
3636

3737
enum Trigger {
@@ -85,7 +85,7 @@ protocol HTTPMarkerDelegate: AnyObject {
8585
/// Note: Make sure you don't call any methods on this HTTPMarker after you called finish
8686
@objc public func finish(response: URLResponse?, error: Error?) {
8787
let httpURLResponse = (response as? HTTPURLResponse)
88-
let statusCode = httpURLResponse?.statusCode ?? 400
88+
let statusCode = httpURLResponse?.statusCode ?? -1
8989
let size = response != nil ? HTTPMarker.Size(response!) : nil
9090

9191
var bothHeaders: HTTPHeader = [:]
@@ -124,7 +124,7 @@ protocol HTTPMarkerDelegate: AnyObject {
124124
@objc public func finish(_ result: HTTPCaptureResult) {
125125
guard case .started = state else { return }
126126
if let error = result.error {
127-
state = .failed(error: error)
127+
state = .failed(responseCode: result.statusCode, error: error)
128128
} else {
129129
state = .finished(responseCode: result.statusCode)
130130
}
@@ -168,7 +168,8 @@ extension HTTPMarker {
168168
error = NSError(domain: NSURLErrorDomain, code: NSURLErrorCancelled, userInfo: nil)
169169
case let .finished(code):
170170
responseCode = code
171-
case let .failed(theError):
171+
case let .failed(code, theError):
172+
responseCode = code
172173
error = theError
173174
}
174175
let header = filter.filterHeaderFields(header)

Tests/InstanaAgentTests/Beacons/Beacon Types/HTTPBeaconTests.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ class HTTPBeaconTests: InstanaTestCase {
116116
AssertEqualAndNotNil(sut.hs, String(code))
117117
AssertTrue(sut.ec == "1")
118118
AssertTrue(sut.et == "HTTPError")
119-
AssertTrue(sut.em == "HTTP \(code): HTTP Error with status code \(code)")
119+
let expectedPrefix = "HTTP \(code): "
120+
AssertTrue(sut.em!.starts(with: expectedPrefix))
120121
}
121122
}
122123

Tests/InstanaAgentTests/Error/HTTPErrorTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ class HTTPErrorTests: InstanaTestCase {
363363
let sut = HTTPError(error: nil, statusCode: 404)
364364

365365
// Then
366-
AssertEqualAndNotNil(sut, HTTPError.statusCode(404))
366+
AssertEqualAndNotNil(sut, HTTPError.statusCode(404, nil))
367367
AssertEqualAndNotNil(sut?.description, "HTTP Error with status code 404")
368368
AssertEqualAndNotNil(sut?.rawValue, "HTTP 404")
369369
}
@@ -373,8 +373,8 @@ class HTTPErrorTests: InstanaTestCase {
373373
let sut = HTTPError(error: error(NSURLErrorTimedOut), statusCode: 404)
374374

375375
// Then
376-
AssertEqualAndNotNil(sut, HTTPError.statusCode(404))
377-
AssertEqualAndNotNil(sut?.description, "HTTP Error with status code 404")
376+
AssertEqualAndNotNil(sut, HTTPError.statusCode(404, nil))
377+
AssertEqualAndNotNil(sut?.description, sut?.localizedDescription)
378378
AssertEqualAndNotNil(sut?.rawValue, "HTTP 404")
379379
}
380380

Tests/InstanaAgentTests/Monitors/HTTP/HTTPMarkerTests.swift

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ class HTTPMarkerTests: InstanaTestCase {
176176
XCTAssertEqual(marker.trigger, .manual)
177177
XCTAssertEqual(delegate.didFinishCount, 1)
178178
XCTAssertEqual(marker.backendTracingID, "BackendID")
179-
if case let .failed(error: result) = marker.state {
179+
if case let .failed(_, error: result) = marker.state {
180180
XCTAssertEqual(result as? InstanaError, expectedError)
181181
} else {
182182
XCTFail("Wrong marker state: \(marker.state)")
@@ -247,7 +247,7 @@ class HTTPMarkerTests: InstanaTestCase {
247247
XCTAssertEqual(delegate.didFinishCount, 1)
248248
XCTAssertEqual(marker.responseSize, responseSize)
249249
XCTAssertTrue(marker.duration > 0)
250-
if case let .failed(e) = marker.state {
250+
if case let .failed(_, e) = marker.state {
251251
XCTAssertEqual(e as? CocoaError, error)
252252
} else {
253253
XCTFail("Wrong marker state: \(marker.state)")
@@ -345,9 +345,10 @@ class HTTPMarkerTests: InstanaTestCase {
345345
let marker = HTTPMarker(url: url, method: "t", trigger: .automatic, delegate: Delegate())
346346
let error = NSError(domain: NSCocoaErrorDomain, code: -1, userInfo: nil)
347347

348+
let statusCode = 409
348349
// When
349350
marker.set(responseSize: responseSize)
350-
marker.finish(response: createMockResponse(409), error: error)
351+
marker.finish(response: createMockResponse(statusCode), error: error)
351352
guard let beacon = marker.createBeacon(filter: .init()) as? HTTPBeacon else {
352353
XCTFail("Beacon type missmatch"); return
353354
}
@@ -358,12 +359,12 @@ class HTTPMarkerTests: InstanaTestCase {
358359
XCTAssertEqual(beacon.duration, marker.duration)
359360
XCTAssertEqual(beacon.method, "t")
360361
XCTAssertEqual(beacon.url, url)
361-
XCTAssertEqual(beacon.responseCode, -1)
362+
XCTAssertEqual(beacon.responseCode, statusCode)
362363
AssertEqualAndNotNil(beacon.responseSize, responseSize)
363364
AssertEqualAndNotNil(beacon.responseSize?.headerBytes, responseSize.headerBytes)
364365
AssertEqualAndNotNil(beacon.responseSize?.bodyBytes, responseSize.bodyBytes)
365366
AssertEqualAndNotNil(beacon.responseSize?.bodyBytesAfterDecoding, responseSize.bodyBytesAfterDecoding)
366-
XCTAssertEqual(beacon.error, HTTPError.unknown(error))
367+
XCTAssertEqual(beacon.error?.description, error.localizedDescription)
367368
}
368369

369370
func test_createBeacon_canceledMarker() {

Tests/InstanaAgentTests/Monitors/HTTP/InstanaURLProtocolTests.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,8 @@ class InstanaURLProtocolTests: InstanaTestCase {
356356
wait(for: [waitFor], timeout: 3.0)
357357
AssertEqualAndNotNil(urlProtocol.marker?.backendTracingID, backendTracingID)
358358
AssertTrue(delegate.calledFinalized)
359-
if case let .failed(error) = urlProtocol.marker?.state {
359+
let responseCode = 400
360+
if case let .failed(responseCode, error) = urlProtocol.marker?.state {
360361
resultError = error as NSError
361362
} else {
362363
XCTFail("Wrong state for marker")

0 commit comments

Comments
 (0)