diff --git a/Sources/PubNub/Networking/Routers/HistoryRouter.swift b/Sources/PubNub/Networking/Routers/HistoryRouter.swift index c6dca541..f4594803 100644 --- a/Sources/PubNub/Networking/Routers/HistoryRouter.swift +++ b/Sources/PubNub/Networking/Routers/HistoryRouter.swift @@ -202,10 +202,24 @@ struct MessageHistoryResponseDecoder: ResponseDecoder { messageType: message.messageType, error: error ) - PubNub.log.error("History message failed to decrypt due to \(error)") + PubNub.log.warn("History message failed to decrypt due to \(error)") } + } else { + messages[index] = MessageHistoryMessagePayload( + message: message.message, + timetoken: message.timetoken, + meta: message.meta, + uuid: message.uuid, + messageType: message.messageType, + error: PubNubError( + .decryptionFailure, + additional: ["Cannot decrypt message due to invalid Base-64 input"] + ) + ) } } + + return messages } diff --git a/Sources/PubNub/Networking/Routers/SubscribeRouter.swift b/Sources/PubNub/Networking/Routers/SubscribeRouter.swift index 24605d1b..e1212fd8 100644 --- a/Sources/PubNub/Networking/Routers/SubscribeRouter.swift +++ b/Sources/PubNub/Networking/Routers/SubscribeRouter.swift @@ -120,23 +120,25 @@ struct SubscribeDecoder: ResponseDecoder { } func decrypt(_ cryptoModule: CryptoModule, message: SubscribeMessagePayload) -> SubscribeMessagePayload { - // Convert base64 string into Data + var message = message + // Convert Base64 string into Data if let messageData = message.payload.dataOptional { // If a message fails we just return the original and move on switch cryptoModule.decryptedString(from: messageData) { case .success(let decodedString): // Create mutable copy of payload - var message = message message.payload = AnyJSON(reverse: decodedString) return message case .failure(let error): - PubNub.log.error("Subscribe message failed to decrypt due to \(error)") - var message = message + PubNub.log.warn("Subscribe message failed to decrypt due to \(error)") message.error = error return message } } - + message.error = PubNubError( + .decryptionFailure, + additional: ["Cannot decrypt message due to invalid Base-64 input"] + ) return message } diff --git a/Tests/PubNubTests/Networking/Routers/SubscribeRouterTests.swift b/Tests/PubNubTests/Networking/Routers/SubscribeRouterTests.swift index b6315aec..e8389e17 100644 --- a/Tests/PubNubTests/Networking/Routers/SubscribeRouterTests.swift +++ b/Tests/PubNubTests/Networking/Routers/SubscribeRouterTests.swift @@ -917,7 +917,7 @@ extension SubscribeRouterTests { listener.didReceiveMessage = { [weak self, pubNubWithMockedSession] message in XCTAssertEqual(message.channel, self?.testChannel) XCTAssertEqual(message.payload.stringOptional, "Test Message") - XCTAssertNil(message.error) + XCTAssertTrue(message.error?.reason == .decryptionFailure) pubNubWithMockedSession.unsubscribeAll() messageExpect.fulfill()