Skip to content

Commit

Permalink
Merge branch 'chore/better-message-and-attachment-error-logs' into 'r…
Browse files Browse the repository at this point in the history
…elease/4.16.2'

Better message and attachment error logs

See merge request ProtonMail/protonmail-ios!4262
  • Loading branch information
jacekkra committed Jul 31, 2024
2 parents c6e8c42 + f798f06 commit 00b6cc2
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -907,7 +907,7 @@ class MessageDataService: MessageDataServiceProtocol, LocalMessageDataServicePro
]
let messages = try fetchRequest.execute()
guard let first = messages.first else {
throw NSError(domain: "proton.ch", code: APIErrorCode.resourceDoesNotExist)
throw MessageDataServiceError.messageNotFoundForMessageID(messageID)
}
return first
}
Expand Down Expand Up @@ -1261,3 +1261,14 @@ extension MessageDataService {
let viewModeDataSource: ViewModeDataSource
}
}

enum MessageDataServiceError: LocalizedError {
case messageNotFoundForMessageID(MessageID)

var errorDescription: String? {
switch self {
case .messageNotFoundForMessageID(let messageID):
return "Message not found for MessageID \(messageID.rawValue)"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ final class MessageActionUpdate: MessageActionUpdateUseCase {
}
if rawMessageIDs.isEmpty {
if !ProcessInfo.isRunningUnitTests {
PMAssertionFailure("Raw messageIDs is empty")
PMAssertionFailure("Raw messageIDs is empty, executing action: \(action)")
}
return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ final class UploadAttachment: UploadAttachmentUseCase {

func execute(attachmentURI: String) async throws {
guard let attachment = try dependencies.messageDataService.getAttachmentEntity(for: attachmentURI) else {
postNotification(error: UploadAttachmentError.resourceDoesNotExist, attachmentURI: attachmentURI)
throw UploadAttachmentError.resourceDoesNotExist
let error = UploadAttachmentError.attachmentDoesNotExist(attachmentURI)
postNotification(error: error, attachmentURI: attachmentURI)
throw error
}

let messageEntity = try dependencies.messageDataService.getMessageEntity(for: attachment.messageID)
Expand Down Expand Up @@ -94,7 +95,7 @@ final class UploadAttachment: UploadAttachmentUseCase {
]
let messageURI = message.objectID.rawValue.uriRepresentation().absoluteString
guard let sendingData = dependencies.messageDataService.getMessageSendingData(for: messageURI) else {
throw UploadAttachmentError.resourceDoesNotExist
throw UploadAttachmentError.messageDoesNotExist(messageURI)
}
guard
let addressID = sendingData.cachedSenderAddress?.addressID ??
Expand Down Expand Up @@ -203,15 +204,18 @@ extension UploadAttachment {

typealias UploadingResponse = (response: JSONDictionary, keyPacket: Data)

enum UploadAttachmentError: LocalizedError {
case resourceDoesNotExist
enum UploadAttachmentError: LocalizedError, Equatable {
case attachmentDoesNotExist(String)
case messageDoesNotExist(String)
case duplicatedUploading
case encryptionError

var errorDescription: String? {
switch self {
case .resourceDoesNotExist:
return "resource doesn't exist"
case .attachmentDoesNotExist(let uri):
return "attachment doesn't exist (\(uri))"
case .messageDoesNotExist(let uri):
return "message doesn't exist (\(uri))"
case .duplicatedUploading:
return "duplicated uploading"
case .encryptionError:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ final class UploadDraft: UploadDraftUseCase {
func execute(messageObjectID: String) async throws {
do {
guard let messageData = dependencies.messageDataService.getMessageSendingData(for: messageObjectID) else {
throw UploadDraftError.resourceDoesNotExist
throw UploadDraftError.messageNotFoundForURI(messageObjectID)
}
let jsonResponse = try await sendUploadRequest(messageData: messageData)
try apply(response: jsonResponse, to: messageObjectID)
Expand All @@ -62,7 +62,7 @@ final class UploadDraft: UploadDraftUseCase {
if error.localizedDescription.isEmpty {
let code = error.bestShotAtReasonableErrorCode
PMAssertionFailure("Attempting to display error with empty description, code \(code)")
}else {
} else {
SystemLogger.log(error: error, category: .emptyAlert)
}
await NSError.alertSavingDraftError(details: error.localizedDescription)
Expand Down Expand Up @@ -174,7 +174,14 @@ extension UploadDraft {
}
}

enum UploadDraftError: Error, Equatable {
case resourceDoesNotExist
enum UploadDraftError: LocalizedError, Equatable {
case messageNotFoundForURI(String)

var errorDescription: String? {
switch self {
case .messageNotFoundForURI(let uri):
return "Message not found for URI: \(uri)"
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,6 @@ class MockMessageDataService: MessageDataServiceProtocol {
return defaultUserAddressStub(addressID)
}

@ThrowingFuncStub(MockMessageDataService.getMessage, initialReturn: Message()) var getMessageStub
func getMessage(for messageID: ProtonMail.MessageID) throws -> ProtonMail.Message {
try getMessageStub(messageID)
}

@ThrowingFuncStub(MockMessageDataService.getMessageEntity, initialReturn: MessageEntity(Message())) var getMessageEntityStub
func getMessageEntity(for messageID: ProtonMail.MessageID) throws -> ProtonMail.MessageEntity {
try getMessageEntityStub(messageID)
Expand All @@ -184,9 +179,4 @@ class MockMessageDataService: MessageDataServiceProtocol {
func updateAttachment(by uploadResponse: ProtonMail.UploadAttachment.UploadingResponse, attachmentObjectID: ProtonMail.ObjectID) {
updateAttachmentStub(uploadResponse, attachmentObjectID)
}

@FuncStub(MockMessageDataService.removeAllAttachmentsNotUploaded) var removeAllAttachmentsNotUploadedStub
func removeAllAttachmentsNotUploaded(messageID: ProtonMail.MessageID) {
removeAllAttachmentsNotUploadedStub(messageID)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ final class UploadAttachmentUseCaseTest: XCTestCase {
XCTFail("Shouldn't success")
} catch {
let uploadError = try XCTUnwrap(error as? UploadAttachment.UploadAttachmentError)
XCTAssertEqual(uploadError, .resourceDoesNotExist)
XCTAssertEqual(uploadError, .attachmentDoesNotExist("123"))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ final class UploadDraftUseCaseTest: XCTestCase {
XCTFail("Should throw error")
} catch {
let receivedError = try XCTUnwrap(error as? UploadDraft.UploadDraftError)
XCTAssertEqual(receivedError, .resourceDoesNotExist)
XCTAssertEqual(receivedError, .messageNotFoundForURI(fakeID))
}
}

Expand Down

0 comments on commit 00b6cc2

Please sign in to comment.