fix(i18n): give render_exists a message and check every server error code

The render upload refusal render_exists had no Server.Error.RenderExists
resource, so clients showed the raw code. ApiErrors builds that key at run
time, which the scan of used keys cannot see: i18n check now maps every
snake_case code literal the server passes to ApiErrors, Outcome.Fail and its
Fail and TooMany helpers to its Server.Error.* key and fails when one is
missing (SRV-6).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
Claude committed 2026-10-12 09:39:44 +00:00
1 parent c11c996bc6
commit eeee99694b
4 files changed
+95 -4

No files matched your search

+40 -4
View File
@@ -7,8 +7,8 @@ using Titano.Core.Localization;
namespace Titano.Tools.Commands;
/// <summary>
/// <c>i18n check</c>: completeness of <c>en</c> and <c>it</c>, placeholder parity, length budgets and that
/// every key used in code/XAML/admin JS exists (BRIEF §16).
/// <c>i18n check</c>: completeness of <c>en</c> and <c>it</c>, placeholder parity, length budgets, that every key
/// used in code/XAML/admin JS exists and that every error code the server answers with has its message (BRIEF §16).
/// <c>i18n export --out dir</c>: one JSON per culture for the web admin.
/// <c>i18n missing --culture xx [--all]</c>: untranslated (or all) keys as TSV for a translator or reviewer.
/// <c>i18n import --culture xx file.tsv</c>: merges translated <c>key&lt;TAB&gt;text</c> rows into the culture's resx files.
@@ -91,6 +91,8 @@ internal sealed partial class I18nCommand : ICommand
if (en.Keys.Any(k => k.StartsWith(key + ".", StringComparison.Ordinal) && PluralSuffix().IsMatch(k[(key.Length + 1)..]))) continue;
errors.Add($"{Path.GetRelativePath(root, file)}: unknown key {key}");
}
foreach (var (file, code, key) in MissingErrorMessages(root, en.Keys.ToHashSet(StringComparer.Ordinal)))
errors.Add($"{Path.GetRelativePath(root, file)}: error code {code} has no message {key}");
foreach (var w in warnings.Take(50)) Console.WriteLine("warning: " + w);
if (warnings.Count > 50) Console.WriteLine($"warning: … {warnings.Count - 50} more");
@@ -253,8 +255,7 @@ internal sealed partial class I18nCommand : ICommand
var src = Path.Combine(root, "src");
foreach (var file in Directory.EnumerateFiles(src, "*.*", SearchOption.AllDirectories))
{
if (file.Contains($"{Path.DirectorySeparatorChar}obj{Path.DirectorySeparatorChar}", StringComparison.Ordinal)
|| file.Contains($"{Path.DirectorySeparatorChar}bin{Path.DirectorySeparatorChar}", StringComparison.Ordinal)) continue;
if (IsBuildOutput(file)) continue;
var ext = Path.GetExtension(file);
Regex? regex = ext switch
{
@@ -274,9 +275,44 @@ internal sealed partial class I18nCommand : ICommand
}
}
/// <summary>
/// Error codes of the server (literals passed to <c>ApiErrors</c>, <c>Outcome.Fail</c> and the local <c>Fail</c> and
/// <c>TooMany</c> helpers) whose message <c>Server.Error.&lt;Code&gt;</c> is not in <paramref name="keys"/>, each once.
/// <c>ApiErrors.Message</c> builds that key at run time, so the scan of used keys cannot see it.
/// </summary>
internal static IEnumerable<(string File, string Code, string Key)> MissingErrorMessages(string root, IReadOnlySet<string> keys)
{
var reported = new HashSet<string>(StringComparer.Ordinal);
var server = Path.Combine(root, "src", "Titano.Server");
foreach (var file in Directory.EnumerateFiles(server, "*.cs", SearchOption.AllDirectories).Order(StringComparer.Ordinal))
{
if (IsBuildOutput(file)) continue;
foreach (var code in ErrorCodes(File.ReadAllText(file)))
{
var key = ErrorKey(code);
if (!keys.Contains(key) && reported.Add(code)) yield return (file, code, key);
}
}
}
/// <summary>The literal error codes (snake_case) a C# source passes to the server's error helpers.</summary>
internal static IEnumerable<string> ErrorCodes(string source) => ErrorCodeRegex().Matches(source).Select(m => m.Groups[1].Value);
/// <summary>The message key of an error code, built like <c>ApiErrors.Message</c>: <c>render_exists</c> → <c>Server.Error.RenderExists</c>.</summary>
internal static string ErrorKey(string code) =>
"Server.Error." + string.Concat(code.Split('_', StringSplitOptions.RemoveEmptyEntries).Select(part => char.ToUpperInvariant(part[0]) + part[1..]));
private static bool IsBuildOutput(string file) =>
file.Contains($"{Path.DirectorySeparatorChar}obj{Path.DirectorySeparatorChar}", StringComparison.Ordinal)
|| file.Contains($"{Path.DirectorySeparatorChar}bin{Path.DirectorySeparatorChar}", StringComparison.Ordinal);
[GeneratedRegex(@"\{(\d+)(?:[,:][^}]*)?\}")]
private static partial Regex PlaceholderRegex();
/// <summary>A call of an error helper up to its first string argument (no statement end in between) when that is a snake_case code.</summary>
[GeneratedRegex(@"(?:\bApiErrors\.(?:Result|WriteAsync|BadRequest|Conflict|Message)|\bFail(?:<[^;()]*?>)?|\bTooMany)\([^;""]*?""([a-z][a-z0-9_]*)""")]
private static partial Regex ErrorCodeRegex();
[GeneratedRegex(@"\b(?:L|Plural|Loc\.Get|LocKey)\(\s*""([A-Z][A-Za-z0-9]*\.[A-Za-z0-9_.]+)""")]
private static partial Regex CsKeyRegex();