Skip to content

Commit b45f416

Browse files
HandyS11claude
andcommitted
fix(samples): exit early when DI app key is still the placeholder
The library's ValidateOnStart only rejects a blank ApiKey, so the shipped "REPLACE_ME" placeholder passed validation and the app started with an invalid key — failing only at the first API call. Detect the placeholder explicitly and print the same configuration guidance, matching the spec's "missing or placeholder -> exit non-zero" intent. README updated to match. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 63d0aa4 commit b45f416

2 files changed

Lines changed: 19 additions & 2 deletions

File tree

samples/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ dotnet user-secrets set "RustMaps:ApiKey" "YOUR_KEY" \
3030
dotnet run --project samples/RustMapsApi.DependencyInjection.ConsoleApp
3131
```
3232

33+
Until a real key is supplied, the app detects the `REPLACE_ME` placeholder (and a
34+
blank key via `ValidateOnStart`), prints the command above, and exits non-zero.
35+
3336
## Menu
3437

3538
```text

samples/RustMapsApi.DependencyInjection.ConsoleApp/Program.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@
1818
// User-secrets overrides the appsettings.json placeholder in Development.
1919
builder.Configuration.AddUserSecrets(typeof(Program).Assembly, optional: true);
2020

21+
// The library only rejects a blank key, so the shipped "REPLACE_ME" placeholder would
22+
// otherwise start the app with an invalid key and fail at the first API call. Catch it here.
23+
const string PlaceholderApiKey = "REPLACE_ME";
24+
if (builder.Configuration["RustMaps:ApiKey"] == PlaceholderApiKey)
25+
{
26+
await WriteConfigurationHelpAsync("The RustMaps API key is still the placeholder.");
27+
return 1;
28+
}
29+
2130
builder.Services.AddRustMapsClientV4(builder.Configuration.GetSection("RustMaps"));
2231

2332
try
@@ -32,9 +41,14 @@
3241
}
3342
catch (OptionsValidationException ex)
3443
{
35-
await Console.Error.WriteLineAsync($"Configuration error: {string.Join("; ", ex.Failures)}");
44+
await WriteConfigurationHelpAsync($"Configuration error: {string.Join("; ", ex.Failures)}");
45+
return 1;
46+
}
47+
48+
static async Task WriteConfigurationHelpAsync(string reason)
49+
{
50+
await Console.Error.WriteLineAsync(reason);
3651
await Console.Error.WriteLineAsync(
3752
"Set the key with: dotnet user-secrets set \"RustMaps:ApiKey\" \"YOUR_KEY\" " +
3853
"--project samples/RustMapsApi.DependencyInjection.ConsoleApp");
39-
return 1;
4054
}

0 commit comments

Comments
 (0)