Skip to content

Commit

Permalink
Improved implementation of KapuaException and KapuaRuntimeException
Browse files Browse the repository at this point in the history
Signed-off-by: coduz <alberto.codutti@eurotech.com>
  • Loading branch information
Coduz committed Jul 1, 2019
1 parent 1ff80b4 commit de52a30
Show file tree
Hide file tree
Showing 7 changed files with 246 additions and 276 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2018 Eurotech and/or its affiliates and others
* Copyright (c) 2018, 2019 Eurotech and/or its affiliates and others
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
Expand All @@ -15,16 +15,6 @@

public class RestApiRuntimeException extends KapuaRuntimeException {

/**
* Constructor
*
* @param message
* @param throwable
*/
private RestApiRuntimeException(String message, Throwable throwable) {
super(message, throwable);
}

/**
* Builds a new {@link RestApiRuntimeException} instance based on the supplied {@link RestApiErrorCodes}.
*
Expand Down Expand Up @@ -54,8 +44,6 @@ public RestApiRuntimeException(RestApiErrorCodes code, Object... arguments) {
* @param arguments
*/
public RestApiRuntimeException(RestApiErrorCodes code, Throwable cause, Object... arguments) {
super(cause);
this.code = code;
args = arguments;
super(code, cause, arguments);
}
}
4 changes: 4 additions & 0 deletions service/api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
<artifactId>swagger-annotations</artifactId>
<version>${swagger.version}</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*******************************************************************************
* Copyright (c) 2016, 2019 Eurotech and/or its affiliates and others
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Eurotech - initial API and implementation
*******************************************************************************/
package org.eclipse.kapua;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.validation.constraints.NotNull;
import java.text.MessageFormat;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import java.util.StringJoiner;

/**
* Utilities to print localized error messages for {@link KapuaException}s.
*
* @since 1.1.0
*/
public class ExceptionMessageUtils {

private static final Logger LOG = LoggerFactory.getLogger(ExceptionMessageUtils.class);

private static final String KAPUA_GENERIC_MESSAGE = "Error: {0}";

private ExceptionMessageUtils() {
}

/**
* Gets the {@link Locale}ised message for the given {@link KapuaErrorCode}.
* <p>
* If the {@link ResourceBundle} or the {@link KapuaErrorCode} are not found, a generic message is printed.
*
* @param resourceBundleName The {@link ResourceBundle} name from which to search the {@link KapuaErrorCode}.
* @param locale The {@link Locale} of the {@link ResourceBundle}.
* @param code The {@link KapuaErrorCode} to search in the {@link ResourceBundle}.
* @param args The arguments to use to populate the message pattern.
* @return The localized message for the given {@link KapuaErrorCode}.
* @since 1.0.0
*/
public static String getLocalizedMessage(String resourceBundleName, Locale locale, KapuaErrorCode code, Object[] args) {

String pattern = getMessagePattern(resourceBundleName, locale, code);
if (pattern != null) {
return MessageFormat.format(pattern, args);
} else {
StringJoiner joiner = new StringJoiner(", ");
if (args != null && args.length > 0) {
for (Object arg : args) {
joiner.add(String.valueOf(arg));
}
}

return MessageFormat.format(KAPUA_GENERIC_MESSAGE, joiner.toString());
}
}

/**
* Loads the message pattern from the resource bundle.
*
* @param resourceBundleName The {@link ResourceBundle} name from which to search the {@link KapuaErrorCode}.
* @param locale The {@link Locale} of the {@link ResourceBundle}.
* @param code The {@link KapuaErrorCode} to search in the {@link ResourceBundle}.
* @return The message pattern of the given {@link KapuaErrorCode} if found, {@code null} otherwise.
* @since 1.0.0
*/
private static String getMessagePattern(@NotNull String resourceBundleName, @NotNull Locale locale, @NotNull KapuaErrorCode code) {

String messagePattern = null;
ResourceBundle resourceBundle;
try {
resourceBundle = ResourceBundle.getBundle(resourceBundleName, locale);
} catch (MissingResourceException mre) {
LOG.warn("Could not load exception messages for resource: {} in locale: {}. A generic error message will be printed.", resourceBundleName, locale);
return null;
}
try {
messagePattern = resourceBundle.getString(code.name());
} catch (MissingResourceException mre) {
LOG.warn("Could not load exception messages for code: {}. A generic error message will be printed.", code.name());
}

return messagePattern;
}
}
17 changes: 11 additions & 6 deletions service/api/src/main/java/org/eclipse/kapua/KapuaErrorCode.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2011, 2016 Eurotech and/or its affiliates and others
* Copyright (c) 2016, 2019 Eurotech and/or its affiliates and others
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
Expand All @@ -11,17 +11,22 @@
*******************************************************************************/
package org.eclipse.kapua;

import java.io.Serializable;

/**
* Kapua error code definition.
* {@link KapuaErrorCode} definition.
*
* @since 1.0
* @since 1.0.0
*/
public interface KapuaErrorCode {
public interface KapuaErrorCode extends Serializable {

/**
* Get the error code name
* Gets the error code name.
* <p>
* The name can be used to search the exception message in the {@link java.util.ResourceBundle}.
*
* @return
* @return The error code name.
* @since 1.0.0
*/
String name();
}
Loading

0 comments on commit de52a30

Please sign in to comment.