From 542e5d2f6eceeb40c64e988b8779af7e8ac0816b Mon Sep 17 00:00:00 2001 From: John Woo Date: Mon, 16 Dec 2024 17:40:14 -0800 Subject: [PATCH 1/4] Print failed requests to console in debug mode --- .../Source/Internal/API Bindings/APIRequest.swift | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/StripePayments/StripePayments/Source/Internal/API Bindings/APIRequest.swift b/StripePayments/StripePayments/Source/Internal/API Bindings/APIRequest.swift index 8c612fdf8f0..7cbb788d6f8 100644 --- a/StripePayments/StripePayments/Source/Internal/API Bindings/APIRequest.swift +++ b/StripePayments/StripePayments/Source/Internal/API Bindings/APIRequest.swift @@ -189,9 +189,14 @@ let JSONKeyObject = "object" if let responseObject = ResponseType.decodedObject(fromAPIResponse: jsonDictionary) { safeCompletion(responseObject, nil) } else { - let error: Error = - NSError.stp_error(fromStripeResponse: jsonDictionary, httpResponse: httpResponse) - ?? NSError.stp_genericFailedToParseResponseError() + let nsError: NSError? = NSError.stp_error(fromStripeResponse: jsonDictionary, httpResponse: httpResponse) + let error: Error = nsError ?? NSError.stp_genericFailedToParseResponseError() + + #if DEBUG + if let nsError, let requestId = nsError.userInfo[STPError.stripeRequestIDKey] { + print("[Stripe SDK]: Failed request \(requestId)") + } + #endif safeCompletion(nil, error) } } From 999053117b48f07ad08750b3488a7489c048e20b Mon Sep 17 00:00:00 2001 From: John Woo Date: Tue, 17 Dec 2024 15:26:30 -0800 Subject: [PATCH 2/4] Add all requests --- .../Internal/API Bindings/APIRequest.swift | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/StripePayments/StripePayments/Source/Internal/API Bindings/APIRequest.swift b/StripePayments/StripePayments/Source/Internal/API Bindings/APIRequest.swift index 7cbb788d6f8..cb7cbb12edc 100644 --- a/StripePayments/StripePayments/Source/Internal/API Bindings/APIRequest.swift +++ b/StripePayments/StripePayments/Source/Internal/API Bindings/APIRequest.swift @@ -186,17 +186,20 @@ let JSONKeyObject = "object" } // END OF STPEmptyStripeResponse HACK + #if DEBUG + if let httpResponse, + let requestId = httpResponse.value(forHTTPHeaderField: "request-id"), + let url = httpResponse.value(forKey: "URL") as? URL { + print("[Stripe SDK]: Request \(requestId), \(url.relativePath)") + } + #endif + if let responseObject = ResponseType.decodedObject(fromAPIResponse: jsonDictionary) { safeCompletion(responseObject, nil) } else { - let nsError: NSError? = NSError.stp_error(fromStripeResponse: jsonDictionary, httpResponse: httpResponse) - let error: Error = nsError ?? NSError.stp_genericFailedToParseResponseError() - - #if DEBUG - if let nsError, let requestId = nsError.userInfo[STPError.stripeRequestIDKey] { - print("[Stripe SDK]: Failed request \(requestId)") - } - #endif + let error: Error = + NSError.stp_error(fromStripeResponse: jsonDictionary, httpResponse: httpResponse) + ?? NSError.stp_genericFailedToParseResponseError() safeCompletion(nil, error) } } From 3bfd199a02a808036635930a59fc643a6013fc61 Mon Sep 17 00:00:00 2001 From: John Woo Date: Wed, 18 Dec 2024 14:27:33 -0800 Subject: [PATCH 3/4] updating format of log --- .../Source/Internal/API Bindings/APIRequest.swift | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/StripePayments/StripePayments/Source/Internal/API Bindings/APIRequest.swift b/StripePayments/StripePayments/Source/Internal/API Bindings/APIRequest.swift index cb7cbb12edc..5c3bb821865 100644 --- a/StripePayments/StripePayments/Source/Internal/API Bindings/APIRequest.swift +++ b/StripePayments/StripePayments/Source/Internal/API Bindings/APIRequest.swift @@ -39,7 +39,7 @@ let JSONKeyObject = "object" apiClient.urlSession.stp_performDataTask( with: request as URLRequest, completionHandler: { body, response, error in - self.parseResponse(response, body: body, error: error, completion: completion) + self.parseResponse(response, method: "POST", body: body, error: error, completion: completion) } ) } @@ -81,7 +81,7 @@ let JSONKeyObject = "object" apiClient.urlSession.stp_performDataTask( with: request as URLRequest, completionHandler: { body, response, error in - self.parseResponse(response, body: body, error: error, completion: completion) + self.parseResponse(response, method: "GET", body: body, error: error, completion: completion) } ) } @@ -123,13 +123,14 @@ let JSONKeyObject = "object" apiClient.urlSession.stp_performDataTask( with: request as URLRequest, completionHandler: { body, response, error in - self.parseResponse(response, body: body, error: error, completion: completion) + self.parseResponse(response, method: "DELETE" ,body: body, error: error, completion: completion) } ) } class func parseResponse( _ response: URLResponse?, + method: String, body: Data?, error: Error?, completion: @escaping (ResponseType?, HTTPURLResponse?, Error?) -> Void @@ -190,7 +191,7 @@ let JSONKeyObject = "object" if let httpResponse, let requestId = httpResponse.value(forHTTPHeaderField: "request-id"), let url = httpResponse.value(forKey: "URL") as? URL { - print("[Stripe SDK]: Request \(requestId), \(url.relativePath)") + print("[Stripe SDK]: \(method) \"\(url.relativePath)\" \(httpResponse.statusCode) \(requestId)") } #endif From 6be57af966aac0e01583c5c0fc1a1c82cbea369f Mon Sep 17 00:00:00 2001 From: John Woo Date: Wed, 18 Dec 2024 14:47:31 -0800 Subject: [PATCH 4/4] fix tests --- Stripe/StripeiOSTests/APIRequestTest.swift | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Stripe/StripeiOSTests/APIRequestTest.swift b/Stripe/StripeiOSTests/APIRequestTest.swift index 2a097d2053f..43c7afe2734 100644 --- a/Stripe/StripeiOSTests/APIRequestTest.swift +++ b/Stripe/StripeiOSTests/APIRequestTest.swift @@ -140,6 +140,7 @@ class APIRequestTest: STPNetworkStubbingTestCase { APIRequest.parseResponse( httpURLResponse, + method: "GET", body: body, error: errorParameter ) { (object: STPCard?, response, error) in @@ -173,6 +174,7 @@ class APIRequestTest: STPNetworkStubbingTestCase { APIRequest.parseResponse( httpURLResponse, + method: "GET", body: body, error: errorParameter ) { (object: STPCard?, response, error) in @@ -200,6 +202,7 @@ class APIRequestTest: STPNetworkStubbingTestCase { APIRequest.parseResponse( httpURLResponse, + method: "GET", body: body, error: errorParameter ) { (object: STPCard?, response, error) in @@ -230,6 +233,7 @@ class APIRequestTest: STPNetworkStubbingTestCase { APIRequest.parseResponse( httpURLResponse, + method: "GET", body: body, error: errorParameter ) { (object: STPCard?, response, error) in