From e84fc1aa04d5c8db5a1b11fcf99509e49ec3733d Mon Sep 17 00:00:00 2001 From: Niels Andriesse Date: Thu, 15 Jul 2021 09:47:03 +1000 Subject: [PATCH] Include destination info in error messages --- .../ConversationVC+Interaction.swift | 14 ++++++++++++- .../Jobs/AttachmentDownloadJob.swift | 2 +- SessionMessagingKit/Jobs/MessageSendJob.swift | 2 +- .../Open Groups/OpenGroupAPIV2.swift | 2 +- SessionSnodeKit/OnionRequestAPI.swift | 21 ++++++++++++------- 5 files changed, 30 insertions(+), 11 deletions(-) diff --git a/Session/Conversations/ConversationVC+Interaction.swift b/Session/Conversations/ConversationVC+Interaction.swift index 2e8279e16..795cc80ae 100644 --- a/Session/Conversations/ConversationVC+Interaction.swift +++ b/Session/Conversations/ConversationVC+Interaction.swift @@ -458,7 +458,8 @@ extension ConversationVC : InputViewDelegate, MessageCellDelegate, ContextMenuAc func showFailedMessageSheet(for tsMessage: TSOutgoingMessage) { let thread = self.thread - let sheet = UIAlertController(title: tsMessage.mostRecentFailureText, message: nil, preferredStyle: .actionSheet) + let error = tsMessage.mostRecentFailureText + let sheet = UIAlertController(title: error, message: nil, preferredStyle: .actionSheet) sheet.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil)) sheet.addAction(UIAlertAction(title: "Delete", style: .destructive, handler: { _ in Storage.write { transaction in @@ -480,6 +481,17 @@ extension ConversationVC : InputViewDelegate, MessageCellDelegate, ContextMenuAc MessageSender.send(message, in: thread, using: transaction) } })) + // HACK: Extracting this info from the error string is pretty dodgy + let prefix = "HTTP request failed at destination (Service node " + if error.hasPrefix(prefix) { + let rest = error.substring(from: prefix.count) + if let index = rest.firstIndex(of: ")") { + let snodeAddress = String(rest[rest.startIndex.. Promise { let payload: JSON = [ "method" : method.rawValue, "params" : parameters ] return sendOnionRequest(with: payload, to: Destination.snode(snode)).recover2 { error -> Promise in - guard case OnionRequestAPI.Error.httpRequestFailedAtDestination(let statusCode, let json) = error else { throw error } + guard case OnionRequestAPI.Error.httpRequestFailedAtDestination(let statusCode, let json, _) = error else { throw error } throw SnodeAPI.handleError(withStatusCode: statusCode, json: json, forSnode: snode, associatedWith: publicKey) ?? error } } @@ -397,10 +404,10 @@ public enum OnionRequestAPI { let b = try JSONSerialization.jsonObject(with: bodyAsData, options: [ .fragmentsAllowed ]) as? JSON else { return seal.reject(HTTP.Error.invalidJSON) } body = b } - guard 200...299 ~= statusCode else { return seal.reject(Error.httpRequestFailedAtDestination(statusCode: UInt(statusCode), json: body)) } + guard 200...299 ~= statusCode else { return seal.reject(Error.httpRequestFailedAtDestination(statusCode: UInt(statusCode), json: body, destination: destination)) } seal.fulfill(body) } else { - guard 200...299 ~= statusCode else { return seal.reject(Error.httpRequestFailedAtDestination(statusCode: UInt(statusCode), json: json)) } + guard 200...299 ~= statusCode else { return seal.reject(Error.httpRequestFailedAtDestination(statusCode: UInt(statusCode), json: json, destination: destination)) } seal.fulfill(json) } } catch {