From f7a7506fe7cfc5fe5e5b4ccaaf0df5e015ac5d93 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 12 Oct 2026 10:13:34 +0000 Subject: [PATCH] fix(app): say why an action failed in localized words, not exception text Error toasts showed the exception's own message, often technical English from .NET or the OS, under a translated title. The editor's failed save, a job posted to the server from the Library, and the card check or delete after an import now say why in one catalog sentence: what the server answered or that it does not answer, that the card left, is locked or a file could not be read, and the archive causes of ArchiveFailures for a local save. The details stay in the log. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC --- i18n/en/Import.resx | 2 + i18n/it/Import.it.resx | 2 + src/Titano.App/Editor/EditorViewModel.cs | 8 +++- src/Titano.App/Import/ImportFailures.cs | 27 ++++++++++++ .../Import/ImportResultViewModel.cs | 7 ++- src/Titano.App/Library/LibraryActions.cs | 2 +- src/Titano.App/Shell/ErrorText.cs | 17 ++++++++ .../Editor/EditorSmokeTests.cs | 31 +++++++++++++ .../Import/ImportFailuresTests.cs | 29 +++++++++++++ .../Infrastructure/ErrorTextTests.cs | 43 +++++++++++++++++++ 10 files changed, 163 insertions(+), 5 deletions(-) create mode 100644 tests/Titano.App.Tests/Infrastructure/ErrorTextTests.cs diff --git a/i18n/en/Import.resx b/i18n/en/Import.resx index 4858fc46..d11cc36f 100644 --- a/i18n/en/Import.resx +++ b/i18n/en/Import.resx @@ -105,6 +105,8 @@ {0:N0} verified on the card {0:N0} verified on the card Titano may not write to the destination.long + {0} is write-protected or holds protected photos.long; {0} card name + A photo or its archive copy could not be read.long The destination is full.long The import stopped before the end.long Retry diff --git a/i18n/it/Import.it.resx b/i18n/it/Import.it.resx index 332a1f1d..840ec7bd 100644 --- a/i18n/it/Import.it.resx +++ b/i18n/it/Import.it.resx @@ -103,6 +103,8 @@ {0:N0} verificata sulla scheda {0:N0} verificate sulla scheda Titano non può scrivere nella destinazione. + {0}: la scheda è protetta da scrittura o contiene foto protette. + Impossibile leggere una foto o la sua copia nell'archivio. La destinazione è piena. L'importazione si è interrotta prima della fine. Riprova diff --git a/src/Titano.App/Editor/EditorViewModel.cs b/src/Titano.App/Editor/EditorViewModel.cs index 52ada090..5260048d 100644 --- a/src/Titano.App/Editor/EditorViewModel.cs +++ b/src/Titano.App/Editor/EditorViewModel.cs @@ -2,6 +2,7 @@ using System.Windows; using System.Windows.Input; using System.Windows.Media; using System.Windows.Threading; +using Titano.App.Archiving; using Titano.App.Infrastructure; using Titano.App.Jobs; using Titano.App.Server; @@ -829,11 +830,14 @@ public sealed class EditorViewModel : LocalizedViewModel, IAsyncDisposable base.OnLanguageChanged(); } - /// The first failure of a run of failed saves (they are retried until one succeeds); Retry tries at once. + /// + /// The first failure of a run of failed saves (they are retried until one succeeds); Retry tries at once. Why it + /// failed is one localized sentence (BRIEF §0.2 rule 6); the exception's own text is in the log. + /// private void OnSaveFailed(Exception ex) => Shell.ShowToast(new Toast { Title = Loc.L("Editor.Save.Failed"), - Message = ex.Message, + Message = Session.IsRemote ? ErrorText.ForServer(ex) : ArchiveFailures.Cause(ex), IsError = true, ActionText = Loc.L("Editor.Save.Retry"), ActionToolTip = Loc.L("Tooltips.Editor.Save.Retry"), diff --git a/src/Titano.App/Import/ImportFailures.cs b/src/Titano.App/Import/ImportFailures.cs index 2d239d5f..e648a40a 100644 --- a/src/Titano.App/Import/ImportFailures.cs +++ b/src/Titano.App/Import/ImportFailures.cs @@ -1,7 +1,9 @@ using System.IO; using System.Net.Http; +using Titano.Core.Archiving; using Titano.Core.Import; using Titano.Core.Localization; +using Titano.Data; using Titano.Protocol.Client; namespace Titano.App.Import; @@ -15,6 +17,7 @@ public static class ImportFailures { private const int DiskFull = unchecked((int)0x80070070); private const int HandleDiskFull = unchecked((int)0x80070027); + private const int WriteProtect = unchecked((int)0x80070013); /// The card or folder as the Devices list names it. public static string Import(Exception ex, string source) => ex switch @@ -33,4 +36,28 @@ public static class ImportFailures IOException or UnauthorizedAccessException => Loc.L("Import.Status.Unreadable", source), _ => Loc.L("Import.Status.PlanError"), }; + + /// Why the check of a card (every card file and its archive copy hashed again) stopped. + /// Where the card is mounted: a card that left explains any read failure. + public static string Check(Exception ex, string source, string root) => ex switch + { + _ when CardGone(ex, root) => Loc.L("Import.Failed.SourceGone", source), + ArchiveOfflineException => ex.Message, + DatabaseException => Loc.L("Archive.Failed.Index"), + IOException or UnauthorizedAccessException => Loc.L("Import.Failed.CheckRead"), + _ => Loc.L("Errors.Unexpected"), + }; + + /// Why deleting verified photos from a card stopped. + /// Where the card is mounted: a card that left explains any write failure. + public static string Erase(Exception ex, string source, string root) => ex switch + { + _ when CardGone(ex, root) => Loc.L("Import.Failed.SourceGone", source), + // A locked card (write-protect switch) or photos the camera marked protected (read-only). + IOException { HResult: WriteProtect } or UnauthorizedAccessException => Loc.L("Import.Failed.CardLocked", source), + _ => Loc.L("Errors.Unexpected"), + }; + + private static bool CardGone(Exception ex, string root) => + ex is DirectoryNotFoundException or DriveNotFoundException or ImportSourceUnavailableException || !Directory.Exists(root); } diff --git a/src/Titano.App/Import/ImportResultViewModel.cs b/src/Titano.App/Import/ImportResultViewModel.cs index a1eb2b37..24b77458 100644 --- a/src/Titano.App/Import/ImportResultViewModel.cs +++ b/src/Titano.App/Import/ImportResultViewModel.cs @@ -177,7 +177,7 @@ public sealed class ImportResultViewModel : LocalizedViewModel catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or DatabaseException) { Log.Warning("import", "Card check failed", ex); - _shell.Notify(Loc.L("Import.Toast.CheckFailed"), ex.Message, isError: true); + _shell.Notify(Loc.L("Import.Toast.CheckFailed"), ImportFailures.Check(ex, CardName(device, device.Root), device.Root), isError: true); } finally { @@ -212,7 +212,7 @@ public sealed class ImportResultViewModel : LocalizedViewModel catch (Exception ex) when (ex is IOException or UnauthorizedAccessException) { Log.Warning("import", "Deleting imported photos failed", ex); - _shell.Notify(Loc.L("Import.Toast.DeleteFailed"), ex.Message, isError: true); + _shell.Notify(Loc.L("Import.Toast.DeleteFailed"), ImportFailures.Erase(ex, CardName(_device, erase.SourceRoot), erase.SourceRoot), isError: true); } finally { @@ -271,6 +271,9 @@ public sealed class ImportResultViewModel : LocalizedViewModel if (outcome != FormatOutcome.Cancelled) _shell.Notify(Loc.L("Import.Toast.Format." + outcome, name), isError: outcome == FormatOutcome.Failed); } + /// The card as a toast names it: its nickname, else its volume label, else where it is mounted. + private string CardName(CardDevice? device, string root) => _card?.Nickname ?? (string.IsNullOrEmpty(device?.Label) ? BidiText.Ltr(root) : device.Label); + /// The first files that were never imported, one per line. private static string OtherFilesDetail(IReadOnlyList files) { diff --git a/src/Titano.App/Library/LibraryActions.cs b/src/Titano.App/Library/LibraryActions.cs index 13b70ca9..e12c27ed 100644 --- a/src/Titano.App/Library/LibraryActions.cs +++ b/src/Titano.App/Library/LibraryActions.cs @@ -214,7 +214,7 @@ public sealed class LibraryActions(ShellViewModel shell) catch (Exception ex) when (ex is TitanoApiException or HttpRequestException or IOException or TaskCanceledException) { Log.Warning("library", $"Posting a {kind} job to {address} failed", ex); - shell.Notify(Loc.L("Library.Post.Failed"), ex.Message); + shell.Notify(Loc.L("Library.Post.Failed"), ErrorText.ForServer(ex), isError: true); } } diff --git a/src/Titano.App/Shell/ErrorText.cs b/src/Titano.App/Shell/ErrorText.cs index 5c9dd313..52fbaaee 100644 --- a/src/Titano.App/Shell/ErrorText.cs +++ b/src/Titano.App/Shell/ErrorText.cs @@ -1,3 +1,5 @@ +using System.IO; +using System.Net.Http; using System.Reflection; using Titano.Core.Archiving; using Titano.Core.Import; @@ -18,6 +20,21 @@ public static class ErrorText { public static string For(Exception ex) => Describe(Unwrap(ex)) ?? Loc.L("Errors.Unexpected"); + /// + /// A request to the signed-in server failed: the server's own sentence when it answered, else that it does not answer + /// (refused, timed out, dropped) or that its certificate is no longer trusted. + /// + public static string ForServer(Exception ex) + { + ex = Unwrap(ex); + return Describe(ex) ?? ex switch + { + UntrustedCertificateException => Loc.L("Server.Error.Untrusted"), + HttpRequestException or IOException or OperationCanceledException or TimeoutException => Loc.L("Server.Error.Unreachable"), + _ => Loc.L("Errors.Unexpected"), + }; + } + private static string? Describe(Exception ex) => ex switch { LeaseUnavailableException or ArchiveOfflineException or NewerFormatException or ImportSourceUnavailableException diff --git a/tests/Titano.App.Tests/Editor/EditorSmokeTests.cs b/tests/Titano.App.Tests/Editor/EditorSmokeTests.cs index 672877b4..b5afc462 100644 --- a/tests/Titano.App.Tests/Editor/EditorSmokeTests.cs +++ b/tests/Titano.App.Tests/Editor/EditorSmokeTests.cs @@ -1,4 +1,6 @@ +using System.Collections.Specialized; using System.Globalization; +using System.Net.Http; using System.Windows; using System.Windows.Controls; using System.Windows.Media; @@ -251,6 +253,35 @@ public sealed class EditorSmokeTests Assert.False(viewer.OptimiseAllCommand.CanExecute(null)); Assert.False(viewer.AnalyseCommand.CanExecute(null)); } + + // A failed save says why in one localized sentence (BRIEF §0.2 rule 6); the exception's own text stays in the log. + var toasts = new List(); + NotifyCollectionChangedEventHandler collect = (_, e) => toasts.AddRange(e.NewItems?.OfType() ?? []); + shell.PendingToasts.CollectionChanged += collect; + try + { + foreach (var (remote, failure, cause) in new (bool, Exception, string)[] + { + (true, new HttpRequestException("No such host is known."), "Server.Error.Unreachable"), + (false, new UnauthorizedAccessException(@"Access to the path 'D:\Archive\.titano\sequence.json' is denied."), "Archive.Failed.Access"), + }) + { + toasts.Clear(); + var saving = new FakeSequenceSource(EditorFixture.Sunset, content) { IsRemote = remote, FailNextSave = failure }; + var failing = new EditorViewModel(shell, new EditorSession(saving, content, []), [], EncoderHost.Client); + await using (failing) + { + failing.Session.Edit(p => p with { HeroFrame = 1 }); + Assert.False(await failing.Session.FlushAsync(), "the save failed"); + var toast = Assert.Contains(toasts, t => t.Title == Loc.L("Editor.Save.Failed")); + Assert.Equal(Loc.L(cause), toast.Message); + } + } + } + finally + { + shell.PendingToasts.CollectionChanged -= collect; + } } finally { diff --git a/tests/Titano.App.Tests/Import/ImportFailuresTests.cs b/tests/Titano.App.Tests/Import/ImportFailuresTests.cs index 84f39780..79d47c92 100644 --- a/tests/Titano.App.Tests/Import/ImportFailuresTests.cs +++ b/tests/Titano.App.Tests/Import/ImportFailuresTests.cs @@ -3,8 +3,10 @@ using System.IO; using System.Net; using System.Net.Http; using Titano.App.Import; +using Titano.Core.Archiving; using Titano.Core.Import; using Titano.Core.Localization; +using Titano.Data; using Titano.Protocol; using Titano.Protocol.Client; using Titano.Testing; @@ -38,4 +40,31 @@ public sealed class ImportFailuresTests Assert.Equal(@"D:\Shoots could not be read.", ImportFailures.Plan(new IOException("The device is not ready."), Folder)); Assert.Equal("The import could not be planned.", ImportFailures.Plan(new InvalidOperationException("Sequence contains no elements"), Folder)); } + + [Test] + public void ACardCheckOrDeleteFailureIsOneSentenceByCause() + { + const string Card = "EOS R5 · card A"; + var root = Directory.CreateTempSubdirectory("titano-card-").FullName; + try + { + Assert.Equal("A photo or its archive copy could not be read.", ImportFailures.Check(new IOException("The semaphore timeout period has expired."), Card, root)); + Assert.Equal("The index could not be read or written.", ImportFailures.Check(new DatabaseConnectionException("database is locked"), Card, root)); + Assert.Equal("The archive D:\\Shoots is offline.", ImportFailures.Check(new ArchiveOfflineException(@"D:\Shoots", ArchiveAvailability.Offline), Card, root)); + Assert.Equal("Something went wrong.", ImportFailures.Check(new InvalidOperationException("Collection was modified."), Card, root)); + Assert.Equal("EOS R5 · card A is write-protected or holds protected photos.", + ImportFailures.Erase(new IOException("The media is write protected.", unchecked((int)0x80070013)), Card, root)); + Assert.Equal("EOS R5 · card A is write-protected or holds protected photos.", + ImportFailures.Erase(new UnauthorizedAccessException(@"Access to the path 'E:\DCIM\IMG_0001.CR3' is denied."), Card, root)); + Assert.Equal("Something went wrong.", ImportFailures.Erase(new IOException("The request could not be performed because of an I/O device error."), Card, root)); + } + finally + { + Directory.Delete(root); + } + // A card that left explains whatever failed while it was read or written. + var gone = Path.Combine(Path.GetTempPath(), "titano-card-" + Guid.NewGuid().ToString("N")); + Assert.Equal("EOS R5 · card A is no longer available.", ImportFailures.Check(new IOException("The device is not ready."), Card, gone)); + Assert.Equal("EOS R5 · card A is no longer available.", ImportFailures.Erase(new UnauthorizedAccessException("Access denied."), Card, gone)); + } } diff --git a/tests/Titano.App.Tests/Infrastructure/ErrorTextTests.cs b/tests/Titano.App.Tests/Infrastructure/ErrorTextTests.cs new file mode 100644 index 00000000..a5759983 --- /dev/null +++ b/tests/Titano.App.Tests/Infrastructure/ErrorTextTests.cs @@ -0,0 +1,43 @@ +using System.Globalization; +using System.IO; +using System.Net; +using System.Net.Http; +using Titano.App.Shell; +using Titano.Core.Archiving; +using Titano.Core.Localization; +using Titano.Protocol; +using Titano.Protocol.Client; +using Titano.Testing; + +namespace Titano.App.Tests.Infrastructure; + +/// An error toast says what happened in one localized sentence; framework and OS messages stay in the log. +public sealed class ErrorTextTests +{ + public ErrorTextTests() => Loc.SetCulture(CultureInfo.GetCultureInfo("en")); + + [Test] + public void AServerFailureIsTheServersSentenceOrThatItDoesNotAnswer() + { + const string Unreachable = "The server does not answer; check the address and the network."; + Assert.Equal("The server is out of disk space.", + ErrorText.ForServer(new TitanoApiException(HttpStatusCode.InsufficientStorage, new ApiError("insufficient_storage", "The server is out of disk space.", null)))); + Assert.Equal(Unreachable, ErrorText.ForServer(new HttpRequestException("No such host is known. (nas.local:8443)"))); + Assert.Equal(Unreachable, ErrorText.ForServer(new TaskCanceledException("A task was canceled."))); + Assert.Equal(Unreachable, ErrorText.ForServer(new IOException("The response ended prematurely."))); + Assert.Equal(Unreachable, ErrorText.ForServer(new AggregateException(new HttpRequestException("Connection refused")))); + Assert.Equal("The certificate was not trusted, so nothing was sent.", ErrorText.ForServer(new UntrustedCertificateException("AB:CD"))); + // Failures that already carry a catalog sentence keep it. + Assert.Equal(@"The archive D:\Shoots is offline.", ErrorText.ForServer(new ArchiveOfflineException(@"D:\Shoots", ArchiveAvailability.Offline))); + Assert.Equal("Something went wrong.", ErrorText.ForServer(new InvalidOperationException("Sequence contains no elements"))); + Assert.Equal("Something went wrong.", ErrorText.ForServer(new TitanoApiException(HttpStatusCode.BadGateway, new ApiError("bad_gateway", " ", null)))); + } + + [Test] + public void AnyOtherFailureNeverShowsTheFrameworkText() + { + Assert.Equal("Something went wrong.", ErrorText.For(new HttpRequestException("No such host is known."))); + Assert.Equal("Something went wrong.", ErrorText.For(new IOException(@"Access to the path 'D:\Archive' is denied."))); + Assert.Equal(@"The archive D:\Shoots is read-only.", ErrorText.For(new ArchiveOfflineException(@"D:\Shoots", ArchiveAvailability.ReadOnly))); + } +}