Skip to content

Commit

Permalink
Add ability to get default health check registry without an exception (
Browse files Browse the repository at this point in the history
…#1152)

Currently, there exists only one method for getting the default health
check registry - `getDefault`. Unfortunately, it throws an
`IllegalStateException` if the registry is not set. End users need to
check for this condition. Some users would like avoid using
exceptions for the control flow and would prefer to make a null check
instead catching an exception. For them we provide the `tryGetDefault`
method which does exactly that.
  • Loading branch information
arteam authored Jun 28, 2017
1 parent 7df4d97 commit e2238aa
Showing 1 changed file with 14 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,23 @@ public static HealthCheckRegistry setDefault(String name, HealthCheckRegistry he
* @throws IllegalStateException if the default has not been set
*/
public static HealthCheckRegistry getDefault() {
final HealthCheckRegistry healthCheckRegistry = tryGetDefault();
if (healthCheckRegistry != null) {
return healthCheckRegistry;
}
throw new IllegalStateException("Default registry name has not been set.");
}

/**
* Same as {@link #getDefault()} except returns null when the default registry has not been set.
*
* @return the default registry or null
*/
public static HealthCheckRegistry tryGetDefault() {
final String name = defaultRegistryName.get();
if (name != null) {
return getOrCreate(name);
}
throw new IllegalStateException("Default registry name has not been set.");
return null;
}
}

0 comments on commit e2238aa

Please sign in to comment.