diff --git a/src/Titano.App/Shell/ErrorText.cs b/src/Titano.App/Shell/ErrorText.cs index 52fbaaee..5421b8a7 100644 --- a/src/Titano.App/Shell/ErrorText.cs +++ b/src/Titano.App/Shell/ErrorText.cs @@ -1,4 +1,5 @@ using System.IO; +using System.Net; using System.Net.Http; using System.Reflection; using Titano.Core.Archiving; @@ -7,6 +8,7 @@ using Titano.Core.Localization; using Titano.Core.Render; using Titano.Data.Archiving; using Titano.Data.Sync; +using Titano.Protocol; using Titano.Protocol.Client; namespace Titano.App.Shell; @@ -21,8 +23,9 @@ 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. + /// A request to the signed-in server failed: the server's own sentence when it answered, else what the reply's status + /// means (a proxy in front of it), that it does not answer (refused, timed out, dropped) or that its certificate is no + /// longer trusted. /// public static string ForServer(Exception ex) { @@ -40,11 +43,28 @@ public static class ErrorText LeaseUnavailableException or ArchiveOfflineException or NewerFormatException or ImportSourceUnavailableException or CopyVerificationException or StaleRenamePlanException or SyncConflictChangedException => ex.Message, // The server words its errors in the client's language (Accept-Language). - TitanoApiException api when !string.IsNullOrWhiteSpace(api.Error.Message) => api.Error.Message, + TitanoApiException api when FromServer(api.Error) => api.Error.Message, + // Any other reply (a reverse proxy's 502 page while the server restarts, its 413 for a large body, a bare 500) holds + // only the HTTP reason phrase, in English: say what its status means. + TitanoApiException api => api.Status switch + { + HttpStatusCode.RequestEntityTooLarge => Loc.L("Server.Error.TooLarge"), + HttpStatusCode.TooManyRequests => Loc.L("Server.Error.RateLimited"), + _ when api.IsTransient => Loc.L("Server.Error.Unreachable"), + >= HttpStatusCode.InternalServerError => Loc.L("Server.Error.Internal"), + _ => null, + }, RenderException render => Loc.Get(render.ReasonKey, Loc.Culture) is null ? null : Loc.L(render.ReasonKey, [.. render.Args]), _ => null, }; + /// + /// An error the server wrote as its JSON, with its own sentence. TitanoClient codes any other reply + /// http_<status> with the reason phrase as its message, and JSON of another shape has no code. + /// + private static bool FromServer(ApiError error) => + error.Code is { } code && !code.StartsWith("http_", StringComparison.Ordinal) && !string.IsNullOrWhiteSpace(error.Message); + /// Awaited tasks and reflection wrap the real failure in one layer or more. private static Exception Unwrap(Exception ex) { diff --git a/src/Titano.Core/Import/CardScanner.cs b/src/Titano.Core/Import/CardScanner.cs index 603f5958..8c2270cb 100644 --- a/src/Titano.Core/Import/CardScanner.cs +++ b/src/Titano.Core/Import/CardScanner.cs @@ -62,7 +62,10 @@ public static class CardScanner return files; } - /// Reads light metadata of every file; unreadable files become . + /// + /// Reads light metadata of every file; unreadable files become , each with one localized + /// sentence (the system's text goes to the log only). + /// public static IReadOnlyList ReadMetadata(string root, IReadOnlyList files, List problems, IProgress? progress = null, CancellationToken cancellationToken = default) { @@ -78,7 +81,7 @@ public static class CardScanner catch (Exception ex) when (ex is IOException or UnauthorizedAccessException) { Log.Warning("import", $"Cannot read {file.RelativePath}", ex); - problems.Add(new ImportProblem(file.RelativePath, ex.Message)); + problems.Add(new ImportProblem(file.RelativePath, ImportExecutor.FailureText(ex))); } progress?.Report(i + 1); } diff --git a/tests/Titano.App.Tests/Import/ImportFailuresTests.cs b/tests/Titano.App.Tests/Import/ImportFailuresTests.cs index 7821fad2..8c1d5094 100644 --- a/tests/Titano.App.Tests/Import/ImportFailuresTests.cs +++ b/tests/Titano.App.Tests/Import/ImportFailuresTests.cs @@ -43,6 +43,8 @@ public sealed class ImportFailuresTests Assert.Equal("Sign in again", ImportFailures.Stop(new TitanoApiException(HttpStatusCode.Unauthorized, new ApiError("unauthorized", "Sign in again", null)), Card)); Assert.Equal(Loc.L("Server.Error.Unreachable"), ImportFailures.Stop(new HttpRequestException("An error occurred while sending the request."), Card)); + // A reverse proxy answered while the server restarted: its reason phrase is English, never the server's sentence. + Assert.Equal(Loc.L("Server.Error.Unreachable"), ImportFailures.Stop(new TitanoApiException(HttpStatusCode.BadGateway, new ApiError("http_502", "Bad Gateway")), Card)); Assert.Equal(Loc.L("Server.Error.Unreachable"), ImportFailures.Stop(new TaskCanceledException("The operation was canceled."), Card)); Assert.Equal(Loc.L("Server.Error.Untrusted"), ImportFailures.Stop(new UntrustedCertificateException("AB:CD"), Card)); } diff --git a/tests/Titano.App.Tests/Import/ServerImportSinkTests.cs b/tests/Titano.App.Tests/Import/ServerImportSinkTests.cs index 6fc74a94..ff65c7ad 100644 --- a/tests/Titano.App.Tests/Import/ServerImportSinkTests.cs +++ b/tests/Titano.App.Tests/Import/ServerImportSinkTests.cs @@ -81,6 +81,22 @@ public sealed class ServerImportSinkTests(TestContext context) Assert.Empty(Directory.EnumerateFileSystemEntries(staging)); } + [Test] + public async Task AFileAProxyRefusesIsListedWithTheCatalogsSentence() + { + var card = Card(context.CreateTempDirectory("card"), 8, Start); + var staging = context.CreateTempDirectory("staging"); + var server = new FakeUploadServer(chunkSize: 4096) { ProxyRefusesFile = "IMG_0005.JPG" }; + using var client = new TitanoClient(new Uri("https://nas.local:8443/"), new CertificatePinning([]), "tests", server) { Token = "t" }; + var plan = await new UnindexedDestination(ImportDestinationKind.Server, staging).PlanAsync(card, new ImportChoices().ToOptions(), null, null, context.Cancellation); + var sink = new ServerImportSink(client, staging); + + var outcome = await LocalImport.RunAsync(plan, sink, "EOS R5 ยท card A", true, null, context.Cancellation, sink.Report); + Assert.Equal(7, outcome.Copied); + // The proxy's page says "Request Entity Too Large" in English: the photo reads the catalog's sentence instead. + Assert.Equal(new ImportFailure("DCIM/100CANON/IMG_0005.JPG", Titano.Core.Localization.Loc.L("Server.Error.TooLarge")), outcome.Failures.Single()); + } + [Test] public async Task ALostSignInStopsTheImportAndKeepsWhatWasUploaded() { @@ -231,6 +247,9 @@ public sealed class ServerImportSinkTests(TestContext context) /// Flip a byte of every chunk of this file (a damaged transfer). public string? CorruptFile { get; init; } + /// A reverse proxy in front of the server refuses the chunks of this file: 413 with its own HTML page. + public string? ProxyRefusesFile { get; init; } + /// Answer 401 to new uploads once this many were committed (the sign-in expired). public int? SignedOutAfter { get; init; } @@ -276,6 +295,13 @@ public sealed class ServerImportSinkTests(TestContext context) if (request.Method == HttpMethod.Put && path is ["api", "v1", "uploads", var id, "chunks", var n]) { var upload = _open[Guid.Parse(id)]; + if (upload.FileName == ProxyRefusesFile) + { + return new HttpResponseMessage(HttpStatusCode.RequestEntityTooLarge) + { + Content = new StringContent("

413 Request Entity Too Large

", System.Text.Encoding.UTF8, "text/html"), + }; + } var bytes = await request.Content!.ReadAsByteArrayAsync(cancellationToken); if (upload.FileName == CorruptFile) bytes[0] ^= 0xFF; upload.Parts[int.Parse(n, System.Globalization.CultureInfo.InvariantCulture)] = bytes; diff --git a/tests/Titano.App.Tests/Infrastructure/ErrorTextTests.cs b/tests/Titano.App.Tests/Infrastructure/ErrorTextTests.cs index a5759983..7832bf0d 100644 --- a/tests/Titano.App.Tests/Infrastructure/ErrorTextTests.cs +++ b/tests/Titano.App.Tests/Infrastructure/ErrorTextTests.cs @@ -30,7 +30,27 @@ public sealed class ErrorTextTests // 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)))); + Assert.Equal("Something went wrong.", ErrorText.ForServer(new TitanoApiException(HttpStatusCode.Conflict, new ApiError("conflict", " ", null)))); + Assert.Equal(Unreachable, ErrorText.ForServer(new TitanoApiException(HttpStatusCode.BadGateway, new ApiError("bad_gateway", " ", null)))); + } + + [Test] + public void AReplyThatIsNotTheServersOwnSaysWhatItsStatusMeans() + { + // A reverse proxy answers for the server (the NAS setup): its page is no Titano JSON, so TitanoClient keeps only + // the reason phrase, in English, which never reaches the screen. + static TitanoApiException Proxy(HttpStatusCode status, string reason) => new(status, new ApiError("http_" + (int)status, reason)); + const string Unreachable = "The server does not answer; check the address and the network."; + Assert.Equal(Unreachable, ErrorText.ForServer(Proxy(HttpStatusCode.BadGateway, "Bad Gateway"))); + Assert.Equal(Unreachable, ErrorText.ForServer(Proxy(HttpStatusCode.ServiceUnavailable, "Service Unavailable"))); + Assert.Equal(Unreachable, ErrorText.ForServer(Proxy(HttpStatusCode.GatewayTimeout, "Gateway Timeout"))); + Assert.Equal(Unreachable, ErrorText.For(Proxy(HttpStatusCode.BadGateway, "Bad Gateway"))); + Assert.Equal("The request is larger than the server accepts.", ErrorText.ForServer(Proxy(HttpStatusCode.RequestEntityTooLarge, "Request Entity Too Large"))); + Assert.Equal("Too many attempts; wait a minute and try again.", ErrorText.ForServer(Proxy(HttpStatusCode.TooManyRequests, "Too Many Requests"))); + Assert.Equal("Unexpected server error; try again.", ErrorText.ForServer(Proxy(HttpStatusCode.InternalServerError, "Internal Server Error"))); + Assert.Equal("Something went wrong.", ErrorText.ForServer(Proxy(HttpStatusCode.NotFound, "Not Found"))); + // JSON of another shape has no code: its message is not the server's either. + Assert.Equal(Unreachable, ErrorText.ForServer(new TitanoApiException(HttpStatusCode.BadGateway, new ApiError(null!, "upstream connect error")))); } [Test] diff --git a/tests/Titano.Core.Tests/Import/ImportPlannerTests.cs b/tests/Titano.Core.Tests/Import/ImportPlannerTests.cs index ad45c26e..c61cbdb4 100644 --- a/tests/Titano.Core.Tests/Import/ImportPlannerTests.cs +++ b/tests/Titano.Core.Tests/Import/ImportPlannerTests.cs @@ -1,5 +1,6 @@ using Titano.Core.Archiving; using Titano.Core.Import; +using Titano.Core.Localization; using Titano.Core.Tests.Archiving; using Titano.Testing; using static Titano.Core.Tests.Import.ImportTestKit; @@ -35,6 +36,25 @@ public sealed class ImportPlannerTests(TestContext context) Assert.Empty(plan.Problems); } + [Test] + public void AFileThatCannotBeReadIsAProblemWithOneLocalizedSentence() + { + var card = context.CreateTempDirectory("card"); + TestPhotos.WriteSeries(Path.Combine(card, "DCIM", "100CANON"), Start, 3, Interval); + var files = CardScanner.Find(card, includeSubfolders: true); + // A photo leaves the card between listing and reading; another name is a folder the system refuses to read as a file. + File.Delete(Path.Combine(card, files[1].RelativePath)); + Directory.CreateDirectory(Path.Combine(card, "DCIM", "100CANON", "IMG_9999.JPG")); + var problems = new List(); + + var photos = CardScanner.ReadMetadata(card, [.. files, new CardFile("DCIM/100CANON/IMG_9999.JPG", 0, Start)], problems); + Assert.Equal(2, photos.Count); + Assert.SequenceEqual([files[1].RelativePath, "DCIM/100CANON/IMG_9999.JPG"], problems.Select(p => p.RelativePath)); + Assert.Equal(Loc.L("Import.Failed.Photo"), problems[0].Message); + Assert.Contains(problems[1].Message, new[] { Loc.L("Import.Failed.Photo"), Loc.L("Import.Failed.PhotoAccess") }); + Assert.False(problems.Any(p => p.Message.Contains(card, StringComparison.Ordinal)), "the system's text, with the card's path, stays in the log"); + } + [Test] public async Task AFlatFolderIncludesSubfoldersOnlyWhenAsked() {