Repository navigation
feat: user friendly error messages #4397
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mpivchev
wants to merge
6
commits into
master
Choose a base branch
from
friendly-errors
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+389
−73
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
Nextcloud.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
118 changes: 118 additions & 0 deletions
118
Tests/NextcloudUnitTests/NKErrorUserFriendlyMessageTests.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| // SPDX-FileCopyrightText: Nextcloud GmbH | ||
| // SPDX-FileCopyrightText: 2026 Milen Pivchev | ||
| // SPDX-License-Identifier: GPL-3.0-or-later | ||
|
|
||
| import Foundation | ||
| import NextcloudKit | ||
| import Testing | ||
| @testable import Nextcloud | ||
|
|
||
| @Suite("Error messages shown to people") | ||
| struct NKErrorUserFriendlyMessageTests { | ||
| static let shownKinds = NKError.Kind.allCases.filter { $0 != .cancelled } | ||
|
|
||
| private func localized(_ key: String) -> String { | ||
| NSLocalizedString(key, comment: "") | ||
| } | ||
|
|
||
| @Test("Nothing is shown for success or cancellation") | ||
| func silentErrors() { | ||
| #expect(NKError.success.userFriendlyMessage() == nil) | ||
| #expect(NKError.cancelled.userFriendlyMessage(for: .upload(fileName: "a.jpg")) == nil) | ||
| #expect(NKError(errorCode: NCGlobal.shared.errorTaskCancelled, errorDescription: "Task cancelled").userFriendlyMessage() == nil) | ||
| } | ||
|
|
||
| @Test("Keeps text the app wrote itself") | ||
| func appMessages() { | ||
| #expect(NKError(errorCode: NCGlobal.shared.errorOfflineNotAllowed, errorDescription: "_offline_not_allowed_").userFriendlyMessage() == localized("_offline_not_allowed_")) | ||
| #expect(NKError(errorCode: NCGlobal.shared.errorE2EELock, errorDescription: "Already translated").userFriendlyMessage() == "Already translated") | ||
| } | ||
|
|
||
| @Test("Replaces technical server text") | ||
| func technicalServerText() throws { | ||
| let error = NKError(errorCode: 409, errorDescription: "Parent node does not exist") | ||
| let generic = try #require(error.userFriendlyMessage()) | ||
|
|
||
| #expect(error.userFriendlyMessage(for: .createFolder(folderName: "a/b")) == localized("_error_create_folder_no_parent_")) | ||
| #expect(generic != error.errorDescription) | ||
| #expect(!generic.contains("409")) | ||
| } | ||
|
|
||
| @Test("Uses the generic message unless the operation needs its own") | ||
| func operationMessages() { | ||
| let forbidden = NKError(errorCode: 403, errorDescription: "403: Forbidden") | ||
| let generic = forbidden.userFriendlyMessage() | ||
|
|
||
| #expect(generic != nil) | ||
| #expect(forbidden.userFriendlyMessage(for: .delete(fileName: "a.jpg")) == generic) | ||
| #expect(forbidden.userFriendlyMessage(for: .rename) == generic) | ||
| #expect(forbidden.userFriendlyMessage(for: .upload(fileName: "a.jpg")) == generic) | ||
| } | ||
|
|
||
| @Test("Recognizes uploads rejected by the virus scanner") | ||
| func virusDetected() { | ||
| let error = NKError(errorCode: 415, errorDescription: "Virus Eicar-Signature is detected in the file. Upload cannot be completed.") | ||
|
|
||
| #expect(error.userFriendlyMessage(for: .upload(fileName: "eicar.com")) == localized("_virus_detect_")) | ||
| } | ||
|
|
||
| @Test("Words different kinds of failure differently") | ||
| func kindMessages() { | ||
| let timeout = NKError(errorCode: NSURLErrorTimedOut, errorDescription: "The request timed out.").userFriendlyMessage() | ||
| let quota = NKError(errorCode: 507, errorDescription: "507: Storage quota is reached").userFriendlyMessage() | ||
| let unknown = NKError(errorCode: 418, errorDescription: "418: I'm a teapot").userFriendlyMessage() | ||
|
|
||
| #expect(Set([timeout, quota, unknown]).count == 3) | ||
| #expect(quota != "507: Storage quota is reached") | ||
| } | ||
|
|
||
| @Test("Every kind has a translated message", arguments: NKErrorUserFriendlyMessageTests.shownKinds) | ||
| func everyKind(kind: NKError.Kind) { | ||
| let message = NKError(errorCode: -1, errorDescription: "Technical", kind: kind).userFriendlyMessage() | ||
|
|
||
| #expect(message != nil) | ||
| #expect(message != "Technical") | ||
| } | ||
|
|
||
| @Test("Maps transfer events and saved statuses to operations") | ||
| func contexts() { | ||
| let global = NCGlobal.shared | ||
|
|
||
| #expect(NCErrorContext(networkingStatus: global.networkingStatusUploaded, fileName: "a.jpg") == .upload(fileName: "a.jpg")) | ||
| #expect(NCErrorContext(networkingStatus: global.networkingStatusCopyMove, fileName: "a.jpg") == .moveOrCopy(fileName: "a.jpg")) | ||
| #expect(NCErrorContext(networkingStatus: global.networkingStatusReloadAvatar, fileName: "") == .generic) | ||
| #expect(NCErrorContext(metadataStatus: global.metadataStatusWaitCreateFolder, fileName: "Photos") == .createFolder(folderName: "Photos")) | ||
| #expect(NCErrorContext(metadataStatus: global.metadataStatusDownloadError, fileName: "a.jpg") == .download(fileName: "a.jpg")) | ||
| } | ||
|
|
||
| @Test("Titles say what failed") | ||
| func failureTitles() throws { | ||
| let contexts: [NCErrorContext] = [.upload(fileName: "f"), .download(fileName: "f"), .delete(fileName: "f"), .rename, .moveOrCopy(fileName: "f"), .createFolder(folderName: "f"), .favorite(fileName: "f")] | ||
|
|
||
| #expect(NCErrorContext.generic.failureTitle == nil) | ||
| #expect(NCErrorContext.upload(fileName: "IMG_0042.jpg").failureTitle?.contains("IMG_0042.jpg") == true) | ||
|
|
||
| for context in contexts { | ||
| let title = try #require(context.failureTitle) | ||
|
|
||
| #expect(!title.hasPrefix("_")) | ||
| } | ||
| } | ||
|
|
||
| @Test("Words saved transfer errors") | ||
| func savedErrors() { | ||
| let metadata = tableMetadata() | ||
| metadata.fileNameView = "a.jpg" | ||
| metadata.status = NCGlobal.shared.metadataStatusUploadError | ||
| metadata.errorCode = 507 | ||
| metadata.sessionError = "507: Storage quota is reached" | ||
|
|
||
| #expect(metadata.sessionErrorMessage == NKError(errorCode: 507, errorDescription: "").userFriendlyMessage()) | ||
|
|
||
| metadata.errorCode = NSURLErrorCancelled | ||
| metadata.sessionError = "Transfer was cancelled." | ||
|
|
||
| #expect(metadata.sessionErrorMessage != "Transfer was cancelled.") | ||
| #expect(!metadata.sessionErrorMessage.isEmpty) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.