Skip to content

Commit a6efe5b

Browse files
committed
feat(auth): ✨ add customizable error handling
Allow custom error handling for missing authentication. Previously, the app exited immediately if no authentication was configured. Now, an optional error handler can be provided, enabling platform-aware and user-friendly shutdown behavior.
1 parent 293dd09 commit a6efe5b

2 files changed

Lines changed: 11 additions & 3 deletions

File tree

src/EcoFlow.Mqtt.Api/EntryPoint.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,13 @@
2121
builder.Services.AddHttpClient();
2222
builder.Services.ConfigureAnonymousHttpClient();
2323
builder.Services.ConfigureEcoFlowEndpoints();
24-
builder.Services.ConfigureEcoFlowAuthentication();
24+
builder.Services.ConfigureEcoFlowAuthentication(errorHandler: () =>
25+
{
26+
if (OperatingSystem.IsWindows())
27+
Console.ReadLine();
28+
29+
Environment.Exit(1);
30+
});
2531
builder.Services.AddSingleton<InternalHttpApi>();
2632
builder.Services.AddSingleton<InternalMqttApi>();
2733
builder.Services.AddHostedService(serviceProvider => serviceProvider.GetRequiredService<InternalMqttApi>());

src/EcoFlow.Mqtt.Api/Extensions/HostingExtensions.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public IServiceCollection ConfigureEcoFlowEndpoints()
3434
});
3535
}
3636

37-
public IServiceCollection ConfigureEcoFlowAuthentication()
37+
public IServiceCollection ConfigureEcoFlowAuthentication(Action? errorHandler = null)
3838
{
3939
return services.Configure<EcoFlowConfiguration>(configuration =>
4040
{
@@ -55,7 +55,9 @@ public IServiceCollection ConfigureEcoFlowAuthentication()
5555
{
5656
Console.WriteLine("⚠️ No authentication method configured.");
5757
Console.WriteLine($"Set [{EcoFlowPrefix + accessKeyEnvironmentVariable} and {EcoFlowPrefix + secretKeyEnvironmentVariable}] or [{EcoFlowPrefix + usernameEnvironmentVariable} and {EcoFlowPrefix + passwordEnvironmentVariable}] environment variables.");
58-
Environment.Exit(1);
58+
59+
if (errorHandler is not null)
60+
errorHandler();
5961
}
6062
else
6163
{

0 commit comments

Comments
 (0)