Files
Chronos/tests/Titano.App.Tests/Infrastructure/ErrorTextTests.cs
T
Claude 6e213d4997 fix(import): say what a proxy's reply means instead of its English reason phrase
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
2026-10-12 16:21:10 +00:00

64 lines
4.4 KiB
C#

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;
/// <summary>An error toast says what happened in one localized sentence; framework and OS messages stay in the log.</summary>
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.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]
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)));
}
}