Skip to content

Commit

Permalink
thow unified RuntimeException in case of configuration error
Browse files Browse the repository at this point in the history
  • Loading branch information
Akretsch committed Mar 5, 2024
1 parent 0b8a26b commit 092b0ef
Showing 1 changed file with 17 additions and 19 deletions.
36 changes: 17 additions & 19 deletions src/main/java/com/siemens/pki/cmpracomponent/util/ConfigLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
*/
public class ConfigLogger {

private static final String DYNAMIC_CONFIGURATION_EXCEPTION = "DynamicConfigurationException: ";
private static final Logger LOGGER = LoggerFactory.getLogger(ConfigLogger.class);

private static <T> void doInnerLogging(
Expand Down Expand Up @@ -106,23 +107,22 @@ public static <T> T log(
try {
ret = accessFunction.apply(certProfile, bodyType);
} catch (final Throwable ex) {
LOGGER.error(
"exception calling " + accessFunctionName + "(" + certProfile + ", " + typeToString(bodyType)
+ ") for \"" + interfaceName + "\"",
ex);
final String errorMsg = "exception calling " + accessFunctionName + "(" + certProfile + ", "
+ typeToString(bodyType) + ") for \"" + interfaceName + "\"";
LOGGER.error(errorMsg, ex);
Throwable cause = ex.getCause();
while (cause != null) {
LOGGER.error("cause", cause);
cause = cause.getCause();
}
throw ex;
throw new RuntimeException(DYNAMIC_CONFIGURATION_EXCEPTION + errorMsg, ex);
}
doInnerLogging(interfaceName, accessFunctionName, certProfile, bodyType, ret);
if (ret == null) {
final String logMsg = "calling " + accessFunctionName + "(" + certProfile + ", " + typeToString(bodyType)
+ ") for \"" + interfaceName + "\" returns null, but this configuration item is mandatory";
LOGGER.error(logMsg);
throw new NullPointerException(logMsg);
throw new RuntimeException(DYNAMIC_CONFIGURATION_EXCEPTION + logMsg);
}
return ret;
}
Expand All @@ -142,20 +142,21 @@ public static <T> T log(String interfaceName, String accessFunctionName, Supplie
try {
ret = accessFunction.get();
} catch (final Throwable ex) {
LOGGER.error("exception calling " + accessFunctionName + " for \"" + interfaceName + "\"", ex);
final String errorMsg = "exception calling " + accessFunctionName + " for \"" + interfaceName + "\"";
LOGGER.error(errorMsg, ex);
Throwable cause = ex.getCause();
while (cause != null) {
LOGGER.error("cause", cause);
cause = cause.getCause();
}
throw ex;
throw new RuntimeException(DYNAMIC_CONFIGURATION_EXCEPTION + errorMsg, ex);
}
doInnerLogging(interfaceName, accessFunctionName, ret);
if (ret == null) {
final String logMsg = "calling " + accessFunctionName + " for \"" + interfaceName
+ "\" returns null, but this configuration item is mandatory";
LOGGER.error(logMsg);
throw new NullPointerException(logMsg);
throw new RuntimeException(DYNAMIC_CONFIGURATION_EXCEPTION + logMsg);
}
return ret;
}
Expand Down Expand Up @@ -185,16 +186,15 @@ public static <T> T logOptional(
try {
ret = accessFunction.apply(certProfile, bodyType);
} catch (final Throwable ex) {
LOGGER.error(
"exception calling " + accessFunctionName + "(" + certProfile + ", " + typeToString(bodyType)
+ ") for \"" + interfaceName + "\", proceed without configuration",
ex);
final String errorMsg = "exception calling " + accessFunctionName + "(" + certProfile + ", "
+ typeToString(bodyType) + ") for \"" + interfaceName + "\"";
LOGGER.error(errorMsg, ex);
Throwable cause = ex.getCause();
while (cause != null) {
LOGGER.error("cause", cause);
cause = cause.getCause();
}
return null;
throw new RuntimeException(DYNAMIC_CONFIGURATION_EXCEPTION + errorMsg, ex);
}
doInnerLogging(interfaceName, accessFunctionName, certProfile, bodyType, ret);
return ret;
Expand All @@ -216,16 +216,14 @@ public static <T> T logOptional(String interfaceName, String accessFunctionName,
try {
ret = accessFunction.get();
} catch (final Throwable ex) {
LOGGER.error(
"exception while calling " + accessFunctionName + " for \"" + interfaceName
+ "\", proceed without configuration",
ex);
final String errorMsg = "exception while calling " + accessFunctionName + " for \"" + interfaceName + "\"";
LOGGER.error(errorMsg, ex);
Throwable cause = ex.getCause();
while (cause != null) {
LOGGER.error("cause", cause);
cause = cause.getCause();
}
return null;
throw new RuntimeException(DYNAMIC_CONFIGURATION_EXCEPTION + errorMsg, ex);
}
doInnerLogging(interfaceName, accessFunctionName, ret);
return ret;
Expand Down

0 comments on commit 092b0ef

Please sign in to comment.