-
Notifications
You must be signed in to change notification settings - Fork 217
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow to define user indicated errors which can be resolved to a
connectivity status by BaseClientActor Signed-off-by: Yannic Klem <yannic.klem@bosch.io>
- Loading branch information
Showing
19 changed files
with
365 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
...ain/java/org/eclipse/ditto/connectivity/service/messaging/ConnectivityStatusResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright (c) 2021 Contributors to the Eclipse Foundation | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.eclipse.ditto.connectivity.service.messaging; | ||
|
||
import org.eclipse.ditto.connectivity.model.ConnectivityStatus; | ||
import org.eclipse.ditto.connectivity.service.messaging.internal.ConnectionFailure; | ||
|
||
/** | ||
* Resolves the correct {@link ConnectivityStatus} from a given {@link ConnectionFailure}. | ||
*/ | ||
final class ConnectivityStatusResolver { | ||
|
||
private final UserIndicatedErrors userIndicatedErrors; | ||
|
||
private ConnectivityStatusResolver(final UserIndicatedErrors userIndicatedErrors) { | ||
this.userIndicatedErrors = userIndicatedErrors; | ||
} | ||
|
||
/** | ||
* Creates a new instance of {@link ConnectivityStatusResolver}. | ||
* | ||
* @param userIndicatedErrors the list of errors that should be treated as user indicated errors. | ||
* @return the new status resolver. | ||
*/ | ||
static ConnectivityStatusResolver of(final UserIndicatedErrors userIndicatedErrors) { | ||
return new ConnectivityStatusResolver(userIndicatedErrors); | ||
} | ||
|
||
/** | ||
* Resolves the correct {@link ConnectivityStatus} from a given {@link ConnectionFailure}. | ||
* | ||
* @param connectionFailure the failure. | ||
* @return the resolved status. | ||
*/ | ||
ConnectivityStatus resolve(final ConnectionFailure connectionFailure) { | ||
return connectionFailure.getStatus() | ||
.orElseGet(() -> userIndicatedErrors.matches(connectionFailure.getFailure().cause()) ? | ||
ConnectivityStatus.MISCONFIGURED : | ||
ConnectivityStatus.FAILED); | ||
} | ||
|
||
} |
112 changes: 112 additions & 0 deletions
112
...e/src/main/java/org/eclipse/ditto/connectivity/service/messaging/UserIndicatedErrors.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/* | ||
* Copyright (c) 2021 Contributors to the Eclipse Foundation | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.eclipse.ditto.connectivity.service.messaging; | ||
|
||
import java.util.List; | ||
import java.util.regex.Pattern; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.StreamSupport; | ||
|
||
import org.eclipse.ditto.internal.utils.config.DittoConfigError; | ||
|
||
import com.typesafe.config.Config; | ||
import com.typesafe.config.ConfigException; | ||
|
||
/** | ||
* Allows to find out if a given {@link Throwable} matches this list. | ||
*/ | ||
final class UserIndicatedErrors { | ||
|
||
private static final String USER_INDICATED_ERRORS = "ditto.connectivity.user-indicated-errors"; | ||
private final Iterable<ErrorDefinition> errorDefinitions; | ||
|
||
private UserIndicatedErrors(final Iterable<ErrorDefinition> errorDefinitions) { | ||
this.errorDefinitions = errorDefinitions; | ||
} | ||
|
||
/** | ||
* Reads the configured list from the hocon config and creates a new {@link UserIndicatedErrors} based on this config. | ||
* | ||
* @param config the hocon config holding the list of known user indicated errors. | ||
* @return the new error list. | ||
*/ | ||
static UserIndicatedErrors of(final Config config) { | ||
final List<? extends Config> configList = getConfiguredListOrEmpty(config); | ||
final List<ErrorDefinition> definitionList = configList.stream() | ||
.map(Config.class::cast) | ||
.map(ErrorDefinition::of) | ||
.collect(Collectors.toList()); | ||
return new UserIndicatedErrors(definitionList); | ||
} | ||
|
||
private static List<? extends Config> getConfiguredListOrEmpty(final Config config) { | ||
try { | ||
return config.getConfigList(USER_INDICATED_ERRORS); | ||
} catch (final ConfigException.Missing | ConfigException.WrongType e) { | ||
return List.of(); | ||
} | ||
} | ||
|
||
/** | ||
* @param throwable the throwable that should be checked. | ||
* @return True if the throwable matches and {@link ErrorDefinition} contained in this list. | ||
*/ | ||
boolean matches(final Throwable throwable) { | ||
return StreamSupport.stream(errorDefinitions.spliterator(), true) | ||
.anyMatch(definition -> definition.matches(throwable)); | ||
} | ||
|
||
private static final class ErrorDefinition { | ||
|
||
private static final String NAME = "exceptionName"; | ||
private static final String PATTERN = "messagePattern"; | ||
|
||
private final String exceptionName; | ||
private final Pattern messagePattern; | ||
|
||
private ErrorDefinition(final String exceptionName, final Pattern messagePattern) { | ||
this.exceptionName = exceptionName; | ||
this.messagePattern = messagePattern; | ||
} | ||
|
||
/** | ||
* Creates a new {@link UserIndicatedErrors.ErrorDefinition} from a config which is expected to have {@link UserIndicatedErrors.ErrorDefinition#NAME} | ||
* and {@link UserIndicatedErrors.ErrorDefinition#PATTERN} as key. | ||
* | ||
* @param config the config which is expected to have {@link UserIndicatedErrors.ErrorDefinition#NAME} | ||
* and {@link UserIndicatedErrors.ErrorDefinition#PATTERN} as key. | ||
* @return the new blame definition | ||
*/ | ||
private static ErrorDefinition of(final Config config) { | ||
try { | ||
final var exceptionName = config.getString(NAME); | ||
final var regex = config.getString(PATTERN); | ||
final var pattern = Pattern.compile(regex); | ||
return new ErrorDefinition(exceptionName, pattern); | ||
} catch (final ConfigException.Missing | ConfigException.WrongType e) { | ||
throw new DittoConfigError(e); | ||
} | ||
} | ||
|
||
/** | ||
* @param throwable the throwable that should be checked. | ||
* @return True if the throwable matches this definition and false if not. | ||
*/ | ||
boolean matches(final Throwable throwable) { | ||
return exceptionName.equals(throwable.getClass().getName()) && | ||
messagePattern.matcher(String.valueOf(throwable.getMessage())).matches(); | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.