docs: add try-catch example for setProviderAndWait usage (#1433)

* added the detailed instructions for the setProviderAndWait

Signed-off-by: Abhay <abhayakg123@gmail.com.com>

* fixup: checkstyle issues

Signed-off-by: Simon Schrottner <simon.schrottner@dynatrace.com>

---------

Signed-off-by: Abhay <abhayakg123@gmail.com.com>
Signed-off-by: Simon Schrottner <simon.schrottner@dynatrace.com>
Co-authored-by: Abhay <abhayakg123@gmail.com.com>
Co-authored-by: Simon Schrottner <simon.schrottner@dynatrace.com>
This commit is contained in:
Abhay Porwal 2025-04-28 18:04:19 +05:30 committed by GitHub
parent 99faaf88aa
commit 96cf9c7f54
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 23 additions and 3 deletions

View File

@ -104,7 +104,12 @@ public void example(){
// configure a provider
OpenFeatureAPI api = OpenFeatureAPI.getInstance();
api.setProviderAndWait(new InMemoryProvider(myFlags));
try {
api.setProviderAndWait(new InMemoryProvider(myFlags));
} catch (Exception e) {
// handle initialization failure
e.printStackTrace();
}
// create a client
Client client = api.getClient();
@ -149,7 +154,12 @@ To register a provider in a blocking manner to ensure it is ready before further
```java
OpenFeatureAPI api = OpenFeatureAPI.getInstance();
api.setProviderAndWait(new MyProvider());
try {
api.setProviderAndWait(new MyProvider());
} catch (Exception e) {
// handle initialization failure
e.printStackTrace();
}
```
#### Asynchronous

View File

@ -207,7 +207,13 @@ public class OpenFeatureAPI implements EventBus<OpenFeatureAPI> {
}
/**
* Set the default provider and wait for initialization to finish.
* Sets the default provider and waits for its initialization to complete.
*
* <p>Note: If the provider fails during initialization, an {@link OpenFeatureError} will be thrown.
* It is recommended to wrap this call in a try-catch block to handle potential initialization failures gracefully.
*
* @param provider the {@link FeatureProvider} to set as the default.
* @throws OpenFeatureError if the provider fails during initialization.
*/
public void setProviderAndWait(FeatureProvider provider) throws OpenFeatureError {
try (AutoCloseableLock __ = lock.writeLockAutoCloseable()) {
@ -224,8 +230,12 @@ public class OpenFeatureAPI implements EventBus<OpenFeatureAPI> {
/**
* Add a provider for a domain and wait for initialization to finish.
*
* <p>Note: If the provider fails during initialization, an {@link OpenFeatureError} will be thrown.
* It is recommended to wrap this call in a try-catch block to handle potential initialization failures gracefully.
*
* @param domain The domain to bind the provider to.
* @param provider The provider to set.
* @throws OpenFeatureError if the provider fails during initialization.
*/
public void setProviderAndWait(String domain, FeatureProvider provider) throws OpenFeatureError {
try (AutoCloseableLock __ = lock.writeLockAutoCloseable()) {