Skip to content

Inconsistent exception handling when AddAzureAppConfiguration is invoked with optional=true #318

Closed

Description

My preference would be to fail no matter what for errors that come from invalid inputs, but that seems to contradict the documentation for the enabled flag.

My thoughts too. I think our "optional" behavior comes from the "optional" behavior of FileConfigurationProviders. For configuration files, this flag suppresses the exception when a file is not found. Anything else, like invalid syntax in file or empty file path still throws an exception when optional is true.
Along similar lines, if optional is true for AppConfig provider, any errors that occur during Build() should not be suppressed (these are always due to invalid inputs). However, any errors that occur during Load() should be suppressed.

cc Zhenlan Wang (@zhenlan)

Originally posted by Avani Gupta (@avanigupta) in #316 (comment)

Activity

  1. zhenlan commented on Apr 26, 2022

    @zhenlan
    Member
  2. avanigupta commented on Apr 26, 2022

    @avanigupta
    MemberAuthor

    The idea is to throw on user input errors. Since the disaster recovery setup kicks in when there's a runtime error, the above doc instructions should still work.

  3. removed this from the v6.0.0 milestone on Mar 15, 2023
  4. Eskibear commented on Apr 26, 2023

    @Eskibear
    Member

    to fail no matter what for errors that come from invalid inputs,

    It looks trivial by removing the check of optional here within Build().
    As far I can tell, FormatException occurs when we fail to parse the connection string for Endpoint part, and InvalidOperationException occurs when SDK fails to parse the connection string when constructing a client.
    I would create a PR to make the change, and probably add more test cases related to guard the behavior. Let me know if you have more context info or any concern.

  5. Eskibear commented on Apr 26, 2023

    @Eskibear
    Member

    any errors that occur during Build() should not be suppressed (these are always due to invalid inputs). However, any errors that occur during Load() should be suppressed.

    It makes total sense.

    BTW, below is an idiot question and maybe a little off-topic.
    While I was browsing the impl, I find that Build() and Load() are always called together by builder.Build() given how we usually use the snippet below. AzureAppConfigurationSource.Build() is called here and AzureAppConfigurationProvider.Load() is called here when constructing ConfigurationRoot.

    var builder = new ConfigurationBuilder().AddAzureAppConfiguration(...);
    var config = builder.Build();

    Given the idea to fail early, for obvious error like wrong format in user input, my gut feeling is to parse and check as early as in AddAzureAppConfiguration(..). Why do we allow user to add an obvious wrong config source? The parsing should not be expensive I guess. Or perhaps to stay consistent with other config providers. Anyway it should not be a big problem and I just curious about it given my limited knowledge about .NET.

  6. avanigupta commented on Apr 26, 2023

    @avanigupta
    MemberAuthor

    Yan Zhang (@Eskibear) that's a valid point. We could fail earlier during AddAzureAppConfiguration for invalid connection strings. However, connection string parsing is not the only user input error we need to handle. There could be other user errors in setting up AzureAppConfigurationOptions, and we lazy initialize these options only if they are needed, ie., when Build() is invoked. So it makes more sense to validate the connection string during Build too.

  7. changed the title [-]Inconsistent exception handling when `AddAzueAppConfiguration` is invoked with `optional=true`[/-] [+]Inconsistent exception handling when `AddAzureAppConfiguration` is invoked with `optional=true`[/+] on Oct 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions