Skip to content

Commit 96cf9c7

Browse files
abhayporwalsAbhayaepfli
authored
docs: add try-catch example for setProviderAndWait usage (#1433)
* added the detailed instructions for the setProviderAndWait Signed-off-by: Abhay <[email protected]> * fixup: checkstyle issues Signed-off-by: Simon Schrottner <[email protected]> --------- Signed-off-by: Abhay <[email protected]> Signed-off-by: Simon Schrottner <[email protected]> Co-authored-by: Abhay <[email protected]> Co-authored-by: Simon Schrottner <[email protected]>
1 parent 99faaf8 commit 96cf9c7

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

README.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,12 @@ public void example(){
104104

105105
// configure a provider
106106
OpenFeatureAPI api = OpenFeatureAPI.getInstance();
107-
api.setProviderAndWait(new InMemoryProvider(myFlags));
107+
try {
108+
api.setProviderAndWait(new InMemoryProvider(myFlags));
109+
} catch (Exception e) {
110+
// handle initialization failure
111+
e.printStackTrace();
112+
}
108113

109114
// create a client
110115
Client client = api.getClient();
@@ -149,7 +154,12 @@ To register a provider in a blocking manner to ensure it is ready before further
149154

150155
```java
151156
OpenFeatureAPI api = OpenFeatureAPI.getInstance();
152-
api.setProviderAndWait(new MyProvider());
157+
try {
158+
api.setProviderAndWait(new MyProvider());
159+
} catch (Exception e) {
160+
// handle initialization failure
161+
e.printStackTrace();
162+
}
153163
```
154164

155165
#### Asynchronous

src/main/java/dev/openfeature/sdk/OpenFeatureAPI.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,13 @@ public void setProvider(String domain, FeatureProvider provider) {
207207
}
208208

209209
/**
210-
* Set the default provider and wait for initialization to finish.
210+
* Sets the default provider and waits for its initialization to complete.
211+
*
212+
* <p>Note: If the provider fails during initialization, an {@link OpenFeatureError} will be thrown.
213+
* It is recommended to wrap this call in a try-catch block to handle potential initialization failures gracefully.
214+
*
215+
* @param provider the {@link FeatureProvider} to set as the default.
216+
* @throws OpenFeatureError if the provider fails during initialization.
211217
*/
212218
public void setProviderAndWait(FeatureProvider provider) throws OpenFeatureError {
213219
try (AutoCloseableLock __ = lock.writeLockAutoCloseable()) {
@@ -224,8 +230,12 @@ public void setProviderAndWait(FeatureProvider provider) throws OpenFeatureError
224230
/**
225231
* Add a provider for a domain and wait for initialization to finish.
226232
*
233+
* <p>Note: If the provider fails during initialization, an {@link OpenFeatureError} will be thrown.
234+
* It is recommended to wrap this call in a try-catch block to handle potential initialization failures gracefully.
235+
*
227236
* @param domain The domain to bind the provider to.
228237
* @param provider The provider to set.
238+
* @throws OpenFeatureError if the provider fails during initialization.
229239
*/
230240
public void setProviderAndWait(String domain, FeatureProvider provider) throws OpenFeatureError {
231241
try (AutoCloseableLock __ = lock.writeLockAutoCloseable()) {

0 commit comments

Comments
 (0)