A reply that is not the server's JSON (a reverse proxy's 502 page while the container restarts, its 413 for a large body, a bare 500) reached the Import screen as the HTTP reason phrase, in English, beside every photo. ErrorText now uses an ApiError's message only when the server wrote it (a code that is not TitanoClient's http_<status> fallback) and otherwise says what the status means: too large, rate limited, not answering (gateway and timeout statuses) or a server error. A card file that cannot be read while planning is listed with the executor's catalog sentence instead of the system's text and full path. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
89 lines
6.1 KiB
C#
89 lines
6.1 KiB
C#
using System.Globalization;
|
|
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;
|
|
|
|
namespace Titano.App.Tests.Import;
|
|
|
|
/// <summary>A stopped import or plan says what happened in one localized sentence, never the exception's raw text.</summary>
|
|
public sealed class ImportFailuresTests
|
|
{
|
|
public ImportFailuresTests() => Loc.SetCulture(CultureInfo.GetCultureInfo("en"));
|
|
|
|
[Test]
|
|
public void AnImportFailureIsOneSentenceByCause()
|
|
{
|
|
const string Card = "EOS R5 · card A";
|
|
Assert.Equal("EOS R5 · card A is no longer available.", ImportFailures.Import(new ImportSourceUnavailableException(@"C:\Users\me\AppData\Local\Temp\card\"), Card));
|
|
Assert.Equal("The destination is full.", ImportFailures.Import(new IOException("There is not enough space on the disk.", unchecked((int)0x80070070)), Card));
|
|
Assert.Equal("Titano may not write to the destination.", ImportFailures.Import(new UnauthorizedAccessException(@"Access to D:\Shoots is denied."), Card));
|
|
Assert.Equal("The server did not accept the photos.", ImportFailures.Import(new HttpRequestException("No such host is known."), Card));
|
|
Assert.Equal("The server did not accept the photos.",
|
|
ImportFailures.Import(new TitanoApiException(HttpStatusCode.InsufficientStorage, new ApiError("quota", "Quota exceeded", null)), Card));
|
|
Assert.Equal("The import stopped before the end.", ImportFailures.Import(new InvalidDataException("Bad sidecar. Second sentence."), Card));
|
|
}
|
|
|
|
[Test]
|
|
public void TheCopiesAStoppedImportNeverReachedSayWhyInOneSentence()
|
|
{
|
|
const string Card = "EOS R5 · card A";
|
|
Assert.Equal("EOS R5 · card A is no longer available.", ImportFailures.Stop(new ImportSourceUnavailableException(@"E:\"), Card));
|
|
Assert.Equal("The destination is full.", ImportFailures.Stop(new IOException("There is not enough space on the disk.", unchecked((int)0x80070070)), Card));
|
|
Assert.Equal("Titano may not write to the destination.", ImportFailures.Stop(new UnauthorizedAccessException(@"Access to D:\Shoots is denied."), Card));
|
|
Assert.Equal("The import stopped before the end.", ImportFailures.Stop(new IOException("The specified network name is no longer available."), Card));
|
|
// The server's own sentence, else why it could not be asked.
|
|
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));
|
|
}
|
|
|
|
[Test]
|
|
public void APlanningFailureNamesTheSource()
|
|
{
|
|
const string Folder = @"D:\Shoots";
|
|
Assert.Equal(@"D:\Shoots is no longer available.", ImportFailures.Plan(new DirectoryNotFoundException(@"Could not find a part of the path 'D:\Shoots\DCIM'."), Folder));
|
|
Assert.Equal(@"D:\Shoots could not be read.", ImportFailures.Plan(new UnauthorizedAccessException("Access denied."), Folder));
|
|
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));
|
|
}
|
|
}
|